JanickVision commited on
Commit
8f58e4c
·
verified ·
1 Parent(s): c4e2a3f

Update career_conversations app

Browse files
Files changed (1) hide show
  1. app.py +52 -37
app.py CHANGED
@@ -11,19 +11,24 @@ from pathlib import Path
11
  load_dotenv(override=True)
12
 
13
  def send_telegram_message(message):
14
- token = os.getenv("TELEGRAM_BOT_TOKEN")
15
- chat_id = os.getenv("TELEGRAM_CHAT_ID")
 
16
 
17
- if not token or not chat_id:
18
- raise RuntimeError("Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID")
 
19
 
20
- url = f"https://api.telegram.org/bot{token}/sendMessage"
21
- payload = {
22
- "chat_id": chat_id,
23
- "text": message,
24
- }
 
 
25
 
26
- requests.post(url, data=payload, timeout=10)
 
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
- tool_name = tool_call.function.name
116
- arguments = json.loads(tool_call.function.arguments)
117
- print(f"Tool Called : {tool_name}", flush=True)
118
-
119
- if tool_name == "record_user_details":
120
- result = record_user_details(**arguments)
121
- elif tool_name == "record_unknown_question":
122
- result = record_unknown_question(**arguments)
123
- else:
124
- result = {}
 
 
 
 
 
 
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
- history_messages = self._normalize_history(history)
173
- messages = [{"role": "system", "content": self.system_prompt()}] + history_messages + [{"role": "user", "content": message}]
174
- done = False
175
- while not done:
176
- response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools)
177
- finish_reason = response.choices[0].finish_reason
178
- print(finish_reason)
179
-
180
- if finish_reason == "tool_calls":
181
- message_obj = response.choices[0].message
182
- tool_calls = message_obj.tool_calls
183
- results = self.handle_tool_call(tool_calls)
184
- messages.append(message_obj)
185
- messages.extend(results)
186
- else:
187
- done = True
188
- return response.choices[0].message.content
 
 
 
 
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__":