Update app.py
Browse files
app.py
CHANGED
@@ -191,35 +191,35 @@ class ChatState:
|
|
191 |
return "System prompt updated successfully!"
|
192 |
|
193 |
def generate_response(self, message: str) -> str:
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
|
224 |
def clear_history(self) -> None:
|
225 |
"""Clear the conversation history"""
|
|
|
191 |
return "System prompt updated successfully!"
|
192 |
|
193 |
def generate_response(self, message: str) -> str:
|
194 |
+
"""Generate a response using the Claude API"""
|
195 |
+
if not self.client:
|
196 |
+
return "Please enter a valid API key first."
|
197 |
+
|
198 |
+
try:
|
199 |
+
# Convert history to message format
|
200 |
+
messages = []
|
201 |
+
for human_msg, assistant_msg in self.history:
|
202 |
+
messages.extend([
|
203 |
+
{"role": "user", "content": human_msg},
|
204 |
+
{"role": "assistant", "content": assistant_msg}
|
205 |
+
])
|
206 |
+
|
207 |
+
# Add the current message
|
208 |
+
messages.append({"role": "user", "content": message})
|
209 |
+
|
210 |
+
# Make API call with system parameter
|
211 |
+
response = self.client.messages.create(
|
212 |
+
model="claude-3-5-sonnet-20241022",
|
213 |
+
max_tokens=MAX_TOKENS,
|
214 |
+
temperature=TEMPERATURE,
|
215 |
+
system=self.system_prompt, # Pass system prompt as a separate parameter
|
216 |
+
messages=messages
|
217 |
+
)
|
218 |
+
|
219 |
+
return response.content[0].text
|
220 |
+
|
221 |
+
except Exception as e:
|
222 |
+
return f"Error generating response: {str(e)}"
|
223 |
|
224 |
def clear_history(self) -> None:
|
225 |
"""Clear the conversation history"""
|