Update app.py
Browse files
app.py
CHANGED
|
@@ -100,43 +100,58 @@ def messages_to_history(messages: Messages) -> History:
|
|
| 100 |
history.append([q['content'], r['content']])
|
| 101 |
return history
|
| 102 |
|
| 103 |
-
# API 클라이언트 초기화
|
| 104 |
-
YOUR_ANTHROPIC_TOKEN = os.getenv('ANTHROPIC_API_KEY')
|
| 105 |
-
YOUR_OPENAI_TOKEN = os.getenv('OPENAI_API_KEY')
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
|
|
|
| 110 |
async def try_claude_api(system_message, claude_messages, timeout=15):
|
|
|
|
|
|
|
|
|
|
| 111 |
try:
|
| 112 |
start_time = time.time()
|
| 113 |
-
with
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
collected_content += chunk.delta.text
|
| 127 |
-
yield collected_content
|
| 128 |
-
await asyncio.sleep(0)
|
| 129 |
-
|
| 130 |
-
start_time = current_time
|
| 131 |
|
|
|
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
print(f"Claude API error: {str(e)}")
|
| 134 |
-
raise
|
| 135 |
|
| 136 |
async def try_openai_api(openai_messages):
|
|
|
|
|
|
|
|
|
|
| 137 |
try:
|
| 138 |
-
stream = openai_client.chat.completions.create(
|
| 139 |
-
model="gpt-
|
| 140 |
messages=openai_messages,
|
| 141 |
stream=True,
|
| 142 |
max_tokens=4096,
|
|
@@ -144,14 +159,16 @@ async def try_openai_api(openai_messages):
|
|
| 144 |
)
|
| 145 |
|
| 146 |
collected_content = ""
|
| 147 |
-
for chunk in stream:
|
| 148 |
-
if chunk.choices[0].delta.content
|
| 149 |
collected_content += chunk.choices[0].delta.content
|
| 150 |
yield collected_content
|
| 151 |
|
| 152 |
except Exception as e:
|
| 153 |
print(f"OpenAI API error: {str(e)}")
|
| 154 |
-
raise
|
|
|
|
|
|
|
| 155 |
|
| 156 |
class Demo:
|
| 157 |
def __init__(self):
|
|
|
|
| 100 |
history.append([q['content'], r['content']])
|
| 101 |
return history
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# API 클라이언트 초기화
|
| 105 |
+
YOUR_ANTHROPIC_TOKEN = os.getenv('ANTHROPIC_API_KEY', '') # 기본값 추가
|
| 106 |
+
YOUR_OPENAI_TOKEN = os.getenv('OPENAI_API_KEY', '') # 기본값 추가
|
| 107 |
+
|
| 108 |
+
# API 키 검증
|
| 109 |
+
if not YOUR_ANTHROPIC_TOKEN or not YOUR_OPENAI_TOKEN:
|
| 110 |
+
print("Warning: API keys not found in environment variables")
|
| 111 |
+
|
| 112 |
+
# API 클라이언트 초기화 시 예외 처리 추가
|
| 113 |
+
try:
|
| 114 |
+
claude_client = anthropic.Anthropic(api_key=YOUR_ANTHROPIC_TOKEN)
|
| 115 |
+
openai_client = openai.OpenAI(api_key=YOUR_OPENAI_TOKEN)
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"Error initializing API clients: {str(e)}")
|
| 118 |
+
claude_client = None
|
| 119 |
+
openai_client = None
|
| 120 |
|
| 121 |
+
|
| 122 |
async def try_claude_api(system_message, claude_messages, timeout=15):
|
| 123 |
+
if not claude_client:
|
| 124 |
+
raise Exception("Claude client not initialized")
|
| 125 |
+
|
| 126 |
try:
|
| 127 |
start_time = time.time()
|
| 128 |
+
async with asyncio.timeout(timeout): # 타임아웃 설정
|
| 129 |
+
with claude_client.messages.stream(
|
| 130 |
+
model="claude-3-5-sonnet-20241022",
|
| 131 |
+
max_tokens=7800,
|
| 132 |
+
system=system_message,
|
| 133 |
+
messages=claude_messages
|
| 134 |
+
) as stream:
|
| 135 |
+
collected_content = ""
|
| 136 |
+
async for chunk in stream:
|
| 137 |
+
if chunk.type == "content_block_delta":
|
| 138 |
+
collected_content += chunk.delta.text
|
| 139 |
+
yield collected_content
|
| 140 |
+
await asyncio.sleep(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
except asyncio.TimeoutError:
|
| 143 |
+
raise TimeoutError(f"Claude API timeout after {timeout} seconds")
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Claude API error: {str(e)}")
|
| 146 |
+
raise
|
| 147 |
|
| 148 |
async def try_openai_api(openai_messages):
|
| 149 |
+
if not openai_client:
|
| 150 |
+
raise Exception("OpenAI client not initialized")
|
| 151 |
+
|
| 152 |
try:
|
| 153 |
+
stream = await openai_client.chat.completions.create(
|
| 154 |
+
model="gpt-4", # 모델명 수정
|
| 155 |
messages=openai_messages,
|
| 156 |
stream=True,
|
| 157 |
max_tokens=4096,
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
collected_content = ""
|
| 162 |
+
async for chunk in stream:
|
| 163 |
+
if chunk.choices[0].delta.content:
|
| 164 |
collected_content += chunk.choices[0].delta.content
|
| 165 |
yield collected_content
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
print(f"OpenAI API error: {str(e)}")
|
| 169 |
+
raise
|
| 170 |
+
|
| 171 |
+
|
| 172 |
|
| 173 |
class Demo:
|
| 174 |
def __init__(self):
|