Spaces:
Sleeping
Sleeping
Update career_conversations app
Browse files
app.py
CHANGED
|
@@ -11,19 +11,24 @@ from pathlib import Path
|
|
| 11 |
load_dotenv(override=True)
|
| 12 |
|
| 13 |
def send_telegram_message(message):
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
def record_user_details(email, name="Name not provided", notes="not provided"):
|
| 29 |
send_telegram_message(f"Recording {name} with email {email} and notes {notes}")
|
|
@@ -112,16 +117,22 @@ class Me:
|
|
| 112 |
def handle_tool_call(self, tool_calls):
|
| 113 |
results = []
|
| 114 |
for tool_call in tool_calls:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
| 127 |
return results
|
|
@@ -169,23 +180,27 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 169 |
return normalized
|
| 170 |
|
| 171 |
def chat(self, message, history):
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
|
|
|
| 11 |
load_dotenv(override=True)
|
| 12 |
|
| 13 |
def send_telegram_message(message):
|
| 14 |
+
try:
|
| 15 |
+
token = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 16 |
+
chat_id = os.getenv("TELEGRAM_CHAT_ID")
|
| 17 |
|
| 18 |
+
if not token or not chat_id:
|
| 19 |
+
print("Telegram token or chat_id missing", flush=True)
|
| 20 |
+
return
|
| 21 |
|
| 22 |
+
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
| 23 |
+
payload = {
|
| 24 |
+
"chat_id": chat_id,
|
| 25 |
+
"text": message,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
requests.post(url, data=payload, timeout=10)
|
| 29 |
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Telegram error: {e}", flush=True)
|
| 32 |
|
| 33 |
def record_user_details(email, name="Name not provided", notes="not provided"):
|
| 34 |
send_telegram_message(f"Recording {name} with email {email} and notes {notes}")
|
|
|
|
| 117 |
def handle_tool_call(self, tool_calls):
|
| 118 |
results = []
|
| 119 |
for tool_call in tool_calls:
|
| 120 |
+
try:
|
| 121 |
+
|
| 122 |
+
tool_name = tool_call.function.name
|
| 123 |
+
arguments = json.loads(tool_call.function.arguments)
|
| 124 |
+
print(f"Tool Called : {tool_name}", flush=True)
|
| 125 |
+
|
| 126 |
+
if tool_name == "record_user_details":
|
| 127 |
+
result = record_user_details(**arguments)
|
| 128 |
+
elif tool_name == "record_unknown_question":
|
| 129 |
+
result = record_unknown_question(**arguments)
|
| 130 |
+
else:
|
| 131 |
+
result = {}
|
| 132 |
+
print(f"Unknown tool called: {tool_name}", flush=True)
|
| 133 |
+
except Exception as e:
|
| 134 |
+
print(f"Error handling tool call: {e}", flush=True)
|
| 135 |
+
result = {"error": str(e)}
|
| 136 |
|
| 137 |
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
| 138 |
return results
|
|
|
|
| 180 |
return normalized
|
| 181 |
|
| 182 |
def chat(self, message, history):
|
| 183 |
+
try:
|
| 184 |
+
history_messages = self._normalize_history(history)
|
| 185 |
+
messages = [{"role": "system", "content": self.system_prompt()}] + history_messages + [{"role": "user", "content": message}]
|
| 186 |
+
done = False
|
| 187 |
+
while not done:
|
| 188 |
+
response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools)
|
| 189 |
+
finish_reason = response.choices[0].finish_reason
|
| 190 |
+
print(finish_reason)
|
| 191 |
+
|
| 192 |
+
if finish_reason == "tool_calls":
|
| 193 |
+
message_obj = response.choices[0].message
|
| 194 |
+
tool_calls = message_obj.tool_calls
|
| 195 |
+
results = self.handle_tool_call(tool_calls)
|
| 196 |
+
messages.append(message_obj)
|
| 197 |
+
messages.extend(results)
|
| 198 |
+
else:
|
| 199 |
+
done = True
|
| 200 |
+
return response.choices[0].message.content
|
| 201 |
+
except Exception as e:
|
| 202 |
+
print(f"Error in chat: {e}", flush=True)
|
| 203 |
+
return "Sorry, something went wrong while processing your message."
|
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|