ranranrunforit commited on
Commit
6caace1
·
verified ·
1 Parent(s): 6690171

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -2
app.py CHANGED
@@ -69,10 +69,16 @@ record_unknown_question_json = {
69
  tools = [{"type": "function", "function": record_user_details_json},
70
  {"type": "function", "function": record_unknown_question_json}]
71
 
72
-
 
 
 
 
73
  class Me:
74
 
75
  def __init__(self):
 
 
76
  # open_router_api_key = os.getenv('OPEN_ROUTER_API_KEY')
77
  # if open_router_api_key:
78
  # print(f"Checking Keys: Open router API Key exists and begins {open_router_api_key[:8]}")
@@ -81,6 +87,11 @@ class Me:
81
  self.client = OpenAI(
82
  base_url="https://openrouter.ai/api/v1",
83
  api_key= os.getenv('OPEN_ROUTER_API_KEY') ) # open_router_api_key
 
 
 
 
 
84
  self.name = "Chaoran Zhou"
85
  reader = PdfReader("me/linkedin.pdf")
86
  self.linkedin = ""
@@ -115,6 +126,73 @@ If the user is engaging in discussion, try to steer them towards getting in touc
115
  system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
116
  system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
117
  return system_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  def chat(self, message, history):
120
  messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
@@ -148,7 +226,24 @@ If the user is engaging in discussion, try to steer them towards getting in touc
148
  # print(f"Error during OpenAI API call: {e}")
149
  # return "Sorry, there was an error processing your request. Please check your API key and try again."
150
 
151
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
 
154
  if __name__ == "__main__":
 
69
  tools = [{"type": "function", "function": record_user_details_json},
70
  {"type": "function", "function": record_unknown_question_json}]
71
 
72
+ # Create a Pydantic model for the Evaluation
73
+ class Evaluation(BaseModel):
74
+ is_acceptable: bool
75
+ feedback: str
76
+
77
  class Me:
78
 
79
  def __init__(self):
80
+ # when saving secret in HF space, don't use "" :-)
81
+ # Initialize Open Router client using OpenAI format
82
  # open_router_api_key = os.getenv('OPEN_ROUTER_API_KEY')
83
  # if open_router_api_key:
84
  # print(f"Checking Keys: Open router API Key exists and begins {open_router_api_key[:8]}")
 
87
  self.client = OpenAI(
88
  base_url="https://openrouter.ai/api/v1",
89
  api_key= os.getenv('OPEN_ROUTER_API_KEY') ) # open_router_api_key
90
+ # Initialize Gemini client using OpenAI format
91
+ self.gemini = OpenAI(
92
+ api_key=os.getenv("GOOGLE_API_KEY"),
93
+ base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
94
+ )
95
  self.name = "Chaoran Zhou"
96
  reader = PdfReader("me/linkedin.pdf")
97
  self.linkedin = ""
 
126
  system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
127
  system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
128
  return system_prompt
129
+
130
+ def system_prompt(self):
131
+ system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
132
+ particularly questions related to {self.name}'s career, background, skills and experience. \
133
+ Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
134
+ You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
135
+ Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
136
+ If you don't know the answer to any question, use your record_unknown_question tool to record the question that you couldn't answer, even if it's about something trivial or unrelated to career. \
137
+ If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and record it using your record_user_details tool. "
138
+
139
+ system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
140
+ system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
141
+ return system_prompt
142
+
143
+ def evaluator_system_prompt(self):
144
+ evaluator_system_prompt = f"You are an evaluator that decides whether a response to a question is acceptable. \
145
+ You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \
146
+ The Agent is playing the role of {self.name} and is representing {self.name} on their website. \
147
+ The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \
148
+ The Agent has been provided with context on {self.name} in the form of their summary and LinkedIn details. Here's the information:"
149
+
150
+ evaluator_system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
151
+ evaluator_system_prompt += f"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback."
152
+ return evaluator_system_prompt
153
+
154
+ def evaluator_user_prompt(self, reply, message, history):
155
+ user_prompt = f"Here's the conversation between the User and the Agent: \n\n{history}\n\n"
156
+ user_prompt += f"Here's the latest message from the User: \n\n{message}\n\n"
157
+ user_prompt += f"Here's the latest response from the Agent: \n\n{reply}\n\n"
158
+ user_prompt += f"Please evaluate the response, replying with whether it is acceptable and your feedback."
159
+ return user_prompt
160
+
161
+ def evaluate(self, reply, message, history) -> Evaluation:
162
+ messages = [
163
+ {"role": "system", "content": self.evaluator_system_prompt()},
164
+ {"role": "user", "content": self.evaluator_user_prompt(reply, message, history)}
165
+ ]
166
+ response = self.gemini.beta.chat.completions.parse(
167
+ model="gemini-2.5-flash-preview-05-20",
168
+ messages=messages,
169
+ response_format=Evaluation
170
+ )
171
+ return response.choices[0].message.parsed
172
+
173
+ def rerun(self, reply, message, history, feedback):
174
+ updated_system_prompt = self.system_prompt() + f"\n\n## Previous answer rejected\nYou just tried to reply, but the quality control rejected your reply\n"
175
+ updated_system_prompt += f"## Your attempted answer:\n{reply}\n\n"
176
+ updated_system_prompt += f"## Reason for rejection:\n{feedback}\n\n"
177
+ messages = [{"role": "system", "content": updated_system_prompt}] + history + [{"role": "user", "content": message}]
178
+
179
+ done = False
180
+ while not done:
181
+ response = self.gemini.chat.completions.create(
182
+ model="gemini-2.5-flash-preview-05-20",
183
+ messages=messages,
184
+ tools=tools
185
+ )
186
+
187
+ if response.choices[0].finish_reason == "tool_calls":
188
+ message_obj = response.choices[0].message
189
+ tool_calls = message_obj.tool_calls
190
+ results = self.handle_tool_call(tool_calls)
191
+ messages.append(message_obj)
192
+ messages.extend(results)
193
+ else:
194
+ done = True
195
+ return response.choices[0].message.content
196
 
197
  def chat(self, message, history):
198
  messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
 
226
  # print(f"Error during OpenAI API call: {e}")
227
  # return "Sorry, there was an error processing your request. Please check your API key and try again."
228
 
229
+ reply = response.choices[0].message.content
230
+
231
+ # Evaluate the response
232
+ try:
233
+ evaluation = self.evaluate(reply, message, history)
234
+
235
+ if evaluation.is_acceptable:
236
+ print("Passed evaluation - returning reply")
237
+ else:
238
+ print("Failed evaluation - retrying")
239
+ print(f"Feedback: {evaluation.feedback}")
240
+ reply = self.rerun(reply, message, history, evaluation.feedback)
241
+ except Exception as e:
242
+ print(f"Evaluation failed with error: {e}")
243
+ print("Proceeding with original reply")
244
+
245
+ return reply
246
+
247
 
248
 
249
  if __name__ == "__main__":