Update app.py
Browse files
app.py
CHANGED
@@ -56,6 +56,28 @@ def get_image_base64(image_path):
|
|
56 |
with open(image_path, "rb") as image_file:
|
57 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
58 |
return encoded_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# API 클라이언트 초기화
|
61 |
YOUR_ANTHROPIC_TOKEN = os.getenv('ANTHROPIC_API_KEY')
|
@@ -193,27 +215,7 @@ async def generation_code(query: Optional[str], _setting: Dict[str, str], _histo
|
|
193 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
194 |
|
195 |
|
196 |
-
class Role:
|
197 |
-
SYSTEM = "system"
|
198 |
-
USER = "user"
|
199 |
-
ASSISTANT = "assistant"
|
200 |
|
201 |
-
History = List[Tuple[str, str]]
|
202 |
-
Messages = List[Dict[str, str]]
|
203 |
-
|
204 |
-
def history_to_messages(history: History, system: str) -> Messages:
|
205 |
-
messages = [{'role': Role.SYSTEM, 'content': system}]
|
206 |
-
for h in history:
|
207 |
-
messages.append({'role': Role.USER, 'content': h[0]})
|
208 |
-
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
209 |
-
return messages
|
210 |
-
|
211 |
-
def messages_to_history(messages: Messages) -> History:
|
212 |
-
assert messages[0]['role'] == Role.SYSTEM
|
213 |
-
history = []
|
214 |
-
for q, r in zip(messages[1::2], messages[2::2]):
|
215 |
-
history.append([q['content'], r['content']])
|
216 |
-
return history
|
217 |
|
218 |
def remove_code_block(text):
|
219 |
pattern = r'```html\n(.+?)\n```'
|
|
|
56 |
with open(image_path, "rb") as image_file:
|
57 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
58 |
return encoded_string
|
59 |
+
|
60 |
+
class Role:
|
61 |
+
SYSTEM = "system"
|
62 |
+
USER = "user"
|
63 |
+
ASSISTANT = "assistant"
|
64 |
+
|
65 |
+
History = List[Tuple[str, str]]
|
66 |
+
Messages = List[Dict[str, str]]
|
67 |
+
|
68 |
+
def history_to_messages(history: History, system: str) -> Messages:
|
69 |
+
messages = [{'role': Role.SYSTEM, 'content': system}]
|
70 |
+
for h in history:
|
71 |
+
messages.append({'role': Role.USER, 'content': h[0]})
|
72 |
+
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
73 |
+
return messages
|
74 |
+
|
75 |
+
def messages_to_history(messages: Messages) -> History:
|
76 |
+
assert messages[0]['role'] == Role.SYSTEM
|
77 |
+
history = []
|
78 |
+
for q, r in zip(messages[1::2], messages[2::2]):
|
79 |
+
history.append([q['content'], r['content']])
|
80 |
+
return history
|
81 |
|
82 |
# API 클라이언트 초기화
|
83 |
YOUR_ANTHROPIC_TOKEN = os.getenv('ANTHROPIC_API_KEY')
|
|
|
215 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
216 |
|
217 |
|
|
|
|
|
|
|
|
|
218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
def remove_code_block(text):
|
221 |
pattern = r'```html\n(.+?)\n```'
|