Update app.py
Browse files
app.py
CHANGED
|
@@ -71,10 +71,6 @@ record_unknown_question_json = {
|
|
| 71 |
tools = [{"type": "function", "function": record_user_details_json},
|
| 72 |
{"type": "function", "function": record_unknown_question_json}]
|
| 73 |
|
| 74 |
-
# Create a Pydantic model for the Evaluation
|
| 75 |
-
class Evaluation(BaseModel):
|
| 76 |
-
is_acceptable: bool
|
| 77 |
-
feedback: str
|
| 78 |
|
| 79 |
class Me:
|
| 80 |
|
|
@@ -89,11 +85,7 @@ class Me:
|
|
| 89 |
self.openrouter = OpenAI(
|
| 90 |
base_url="https://openrouter.ai/api/v1",
|
| 91 |
api_key= os.getenv('OPEN_ROUTER_API_KEY') ) # open_router_api_key
|
| 92 |
-
|
| 93 |
-
self.gemini = OpenAI(
|
| 94 |
-
api_key=os.getenv("GOOGLE_API_KEY"),
|
| 95 |
-
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 96 |
-
)
|
| 97 |
self.name = "Chaoran Zhou"
|
| 98 |
reader = PdfReader("me/linkedin.pdf")
|
| 99 |
self.linkedin = ""
|
|
@@ -129,72 +121,6 @@ If the user is engaging in discussion, try to steer them towards getting in touc
|
|
| 129 |
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
| 130 |
return system_prompt
|
| 131 |
|
| 132 |
-
def system_prompt(self):
|
| 133 |
-
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
|
| 134 |
-
particularly questions related to {self.name}'s career, background, skills and experience. \
|
| 135 |
-
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
|
| 136 |
-
You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
|
| 137 |
-
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
|
| 138 |
-
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. \
|
| 139 |
-
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. "
|
| 140 |
-
|
| 141 |
-
system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
|
| 142 |
-
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
| 143 |
-
return system_prompt
|
| 144 |
-
|
| 145 |
-
def evaluator_system_prompt(self):
|
| 146 |
-
evaluator_system_prompt = f"You are an evaluator that decides whether a response to a question is acceptable. \
|
| 147 |
-
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. \
|
| 148 |
-
The Agent is playing the role of {self.name} and is representing {self.name} on their website. \
|
| 149 |
-
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. \
|
| 150 |
-
The Agent has been provided with context on {self.name} in the form of their summary and LinkedIn details. Here's the information:"
|
| 151 |
-
|
| 152 |
-
evaluator_system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
|
| 153 |
-
evaluator_system_prompt += f"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback."
|
| 154 |
-
return evaluator_system_prompt
|
| 155 |
-
|
| 156 |
-
def evaluator_user_prompt(self, reply, message, history):
|
| 157 |
-
user_prompt = f"Here's the conversation between the User and the Agent: \n\n{history}\n\n"
|
| 158 |
-
user_prompt += f"Here's the latest message from the User: \n\n{message}\n\n"
|
| 159 |
-
user_prompt += f"Here's the latest response from the Agent: \n\n{reply}\n\n"
|
| 160 |
-
user_prompt += f"Please evaluate the response, replying with whether it is acceptable and your feedback."
|
| 161 |
-
return user_prompt
|
| 162 |
-
|
| 163 |
-
def evaluate(self, reply, message, history) -> Evaluation:
|
| 164 |
-
messages = [
|
| 165 |
-
{"role": "system", "content": self.evaluator_system_prompt()},
|
| 166 |
-
{"role": "user", "content": self.evaluator_user_prompt(reply, message, history)}
|
| 167 |
-
]
|
| 168 |
-
response = self.gemini.beta.chat.completions.parse(
|
| 169 |
-
model="gemini-2.5-flash-preview-05-20",
|
| 170 |
-
messages=messages,
|
| 171 |
-
response_format=Evaluation
|
| 172 |
-
)
|
| 173 |
-
return response.choices[0].message.parsed
|
| 174 |
-
|
| 175 |
-
def rerun(self, reply, message, history, feedback):
|
| 176 |
-
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"
|
| 177 |
-
updated_system_prompt += f"## Your attempted answer:\n{reply}\n\n"
|
| 178 |
-
updated_system_prompt += f"## Reason for rejection:\n{feedback}\n\n"
|
| 179 |
-
messages = [{"role": "system", "content": updated_system_prompt}] + history + [{"role": "user", "content": message}]
|
| 180 |
-
|
| 181 |
-
done = False
|
| 182 |
-
while not done:
|
| 183 |
-
response = self.gemini.chat.completions.create(
|
| 184 |
-
model="gemini-2.5-flash-preview-05-20",
|
| 185 |
-
messages=messages,
|
| 186 |
-
tools=tools
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
if response.choices[0].finish_reason == "tool_calls":
|
| 190 |
-
message_obj = response.choices[0].message
|
| 191 |
-
tool_calls = message_obj.tool_calls
|
| 192 |
-
results = self.handle_tool_call(tool_calls)
|
| 193 |
-
messages.append(message_obj)
|
| 194 |
-
messages.extend(results)
|
| 195 |
-
else:
|
| 196 |
-
done = True
|
| 197 |
-
return response.choices[0].message.content
|
| 198 |
|
| 199 |
def chat(self, message, history):
|
| 200 |
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
|
@@ -230,23 +156,7 @@ The Agent has been provided with context on {self.name} in the form of their sum
|
|
| 230 |
# print(f"Error during OpenAI API call: {e}")
|
| 231 |
# return "Sorry, there was an error processing your request. Please check your API key and try again."
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
# Evaluate the response
|
| 236 |
-
try:
|
| 237 |
-
evaluation = self.evaluate(reply, message, history)
|
| 238 |
-
|
| 239 |
-
if evaluation.is_acceptable:
|
| 240 |
-
print("Passed evaluation - returning reply")
|
| 241 |
-
else:
|
| 242 |
-
print("Failed evaluation - retrying")
|
| 243 |
-
print(f"Feedback: {evaluation.feedback}")
|
| 244 |
-
reply = self.rerun(reply, message, history, evaluation.feedback)
|
| 245 |
-
except Exception as e:
|
| 246 |
-
print(f"Evaluation failed with error: {e}")
|
| 247 |
-
print("Proceeding with original reply")
|
| 248 |
-
|
| 249 |
-
return reply
|
| 250 |
|
| 251 |
|
| 252 |
|
|
|
|
| 71 |
tools = [{"type": "function", "function": record_user_details_json},
|
| 72 |
{"type": "function", "function": record_unknown_question_json}]
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
class Me:
|
| 76 |
|
|
|
|
| 85 |
self.openrouter = OpenAI(
|
| 86 |
base_url="https://openrouter.ai/api/v1",
|
| 87 |
api_key= os.getenv('OPEN_ROUTER_API_KEY') ) # open_router_api_key
|
| 88 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
self.name = "Chaoran Zhou"
|
| 90 |
reader = PdfReader("me/linkedin.pdf")
|
| 91 |
self.linkedin = ""
|
|
|
|
| 121 |
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
| 122 |
return system_prompt
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
def chat(self, message, history):
|
| 126 |
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
|
|
|
| 156 |
# print(f"Error during OpenAI API call: {e}")
|
| 157 |
# return "Sorry, there was an error processing your request. Please check your API key and try again."
|
| 158 |
|
| 159 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
|