Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,131 +1,131 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
-
from openai import OpenAI
|
| 3 |
-
|
| 4 |
-
import json
|
| 5 |
-
import os
|
| 6 |
-
import requests
|
| 7 |
-
import gradio as gr
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
load_dotenv(override=True)
|
| 11 |
-
|
| 12 |
-
def push(text):
|
| 13 |
-
requests.post(
|
| 14 |
-
"https://api.pushover.net/1/messages.json",
|
| 15 |
-
data={
|
| 16 |
-
"token": os.getenv("PUSHOVER_TOKEN"),
|
| 17 |
-
"user": os.getenv("PUSHOVER_USER"),
|
| 18 |
-
"message": text,
|
| 19 |
-
}
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def record_user_details(email, name="Name not provided", notes="not provided"):
|
| 24 |
-
push(f"Recording {name} with email {email} and notes {notes}")
|
| 25 |
-
return {"recorded": "ok"}
|
| 26 |
-
|
| 27 |
-
def record_unknown_question(question):
|
| 28 |
-
push(f"Recording {question}")
|
| 29 |
-
return {"recorded": "ok"}
|
| 30 |
-
|
| 31 |
-
record_user_details_json = {
|
| 32 |
-
"name": "record_user_details",
|
| 33 |
-
"description": "Use this tool to record that a user is interested in being in touch and provided an email address",
|
| 34 |
-
"parameters": {
|
| 35 |
-
"type": "object",
|
| 36 |
-
"properties": {
|
| 37 |
-
"email": {
|
| 38 |
-
"type": "string",
|
| 39 |
-
"description": "The email address of this user"
|
| 40 |
-
},
|
| 41 |
-
"name": {
|
| 42 |
-
"type": "string",
|
| 43 |
-
"description": "The user's name, if they provided it"
|
| 44 |
-
}
|
| 45 |
-
,
|
| 46 |
-
"notes": {
|
| 47 |
-
"type": "string",
|
| 48 |
-
"description": "Any additional information about the conversation that's worth recording to give context"
|
| 49 |
-
}
|
| 50 |
-
},
|
| 51 |
-
"required": ["email"],
|
| 52 |
-
"additionalProperties": False
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
record_unknown_question_json = {
|
| 57 |
-
"name": "record_unknown_question",
|
| 58 |
-
"description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer",
|
| 59 |
-
"parameters": {
|
| 60 |
-
"type": "object",
|
| 61 |
-
"properties": {
|
| 62 |
-
"question": {
|
| 63 |
-
"type": "string",
|
| 64 |
-
"description": "The question that couldn't be answered"
|
| 65 |
-
},
|
| 66 |
-
},
|
| 67 |
-
"required": ["question"],
|
| 68 |
-
"additionalProperties": False
|
| 69 |
-
}
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
tools = [{"type": "function", "function": record_user_details_json},
|
| 73 |
-
{"type": "function", "function": record_unknown_question_json}]
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
class Me:
|
| 77 |
-
|
| 78 |
-
def __init__(self):
|
| 79 |
-
self.openai = OpenAI()
|
| 80 |
-
|
| 81 |
-
self.name = "Reda Baddy"
|
| 82 |
-
|
| 83 |
-
with open("me/cv.md", "r", encoding="utf-8") as f:
|
| 84 |
-
self.resume = f.read()
|
| 85 |
-
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
| 86 |
-
self.summary = f.read()
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
def handle_tool_call(self, tool_calls):
|
| 90 |
-
results = []
|
| 91 |
-
for tool_call in tool_calls:
|
| 92 |
-
tool_name = tool_call.function.name
|
| 93 |
-
arguments = json.loads(tool_call.function.arguments)
|
| 94 |
-
print(f"Tool called: {tool_name}", flush=True)
|
| 95 |
-
tool = globals().get(tool_name)
|
| 96 |
-
result = tool(**arguments) if tool else {}
|
| 97 |
-
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
| 98 |
-
return results
|
| 99 |
-
|
| 100 |
-
def system_prompt(self):
|
| 101 |
-
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
|
| 102 |
-
particularly questions related to {self.name}'s career, background, skills and experience. \
|
| 103 |
-
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
|
| 104 |
-
You are given a summary of {self.name}'s background and resume which you can use to answer questions. \
|
| 105 |
-
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
|
| 106 |
-
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. \
|
| 107 |
-
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. "
|
| 108 |
-
|
| 109 |
-
system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## Resume:\n{self.resume}\n\n"
|
| 110 |
-
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
| 111 |
-
return system_prompt
|
| 112 |
-
|
| 113 |
-
def chat(self, message, history):
|
| 114 |
-
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
| 115 |
-
done = False
|
| 116 |
-
while not done:
|
| 117 |
-
response = self.
|
| 118 |
-
if response.choices[0].finish_reason=="tool_calls":
|
| 119 |
-
message = response.choices[0].message
|
| 120 |
-
tool_calls = message.tool_calls
|
| 121 |
-
results = self.handle_tool_call(tool_calls)
|
| 122 |
-
messages.append(message)
|
| 123 |
-
messages.extend(results)
|
| 124 |
-
else:
|
| 125 |
-
done = True
|
| 126 |
-
return response.choices[0].message.content
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
if __name__ == "__main__":
|
| 130 |
-
me = Me()
|
| 131 |
gr.ChatInterface(me.chat, type="messages").launch()
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
load_dotenv(override=True)
|
| 11 |
+
|
| 12 |
+
def push(text):
|
| 13 |
+
requests.post(
|
| 14 |
+
"https://api.pushover.net/1/messages.json",
|
| 15 |
+
data={
|
| 16 |
+
"token": os.getenv("PUSHOVER_TOKEN"),
|
| 17 |
+
"user": os.getenv("PUSHOVER_USER"),
|
| 18 |
+
"message": text,
|
| 19 |
+
}
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def record_user_details(email, name="Name not provided", notes="not provided"):
|
| 24 |
+
push(f"Recording {name} with email {email} and notes {notes}")
|
| 25 |
+
return {"recorded": "ok"}
|
| 26 |
+
|
| 27 |
+
def record_unknown_question(question):
|
| 28 |
+
push(f"Recording {question}")
|
| 29 |
+
return {"recorded": "ok"}
|
| 30 |
+
|
| 31 |
+
record_user_details_json = {
|
| 32 |
+
"name": "record_user_details",
|
| 33 |
+
"description": "Use this tool to record that a user is interested in being in touch and provided an email address",
|
| 34 |
+
"parameters": {
|
| 35 |
+
"type": "object",
|
| 36 |
+
"properties": {
|
| 37 |
+
"email": {
|
| 38 |
+
"type": "string",
|
| 39 |
+
"description": "The email address of this user"
|
| 40 |
+
},
|
| 41 |
+
"name": {
|
| 42 |
+
"type": "string",
|
| 43 |
+
"description": "The user's name, if they provided it"
|
| 44 |
+
}
|
| 45 |
+
,
|
| 46 |
+
"notes": {
|
| 47 |
+
"type": "string",
|
| 48 |
+
"description": "Any additional information about the conversation that's worth recording to give context"
|
| 49 |
+
}
|
| 50 |
+
},
|
| 51 |
+
"required": ["email"],
|
| 52 |
+
"additionalProperties": False
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
record_unknown_question_json = {
|
| 57 |
+
"name": "record_unknown_question",
|
| 58 |
+
"description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer",
|
| 59 |
+
"parameters": {
|
| 60 |
+
"type": "object",
|
| 61 |
+
"properties": {
|
| 62 |
+
"question": {
|
| 63 |
+
"type": "string",
|
| 64 |
+
"description": "The question that couldn't be answered"
|
| 65 |
+
},
|
| 66 |
+
},
|
| 67 |
+
"required": ["question"],
|
| 68 |
+
"additionalProperties": False
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
tools = [{"type": "function", "function": record_user_details_json},
|
| 73 |
+
{"type": "function", "function": record_unknown_question_json}]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class Me:
|
| 77 |
+
|
| 78 |
+
def __init__(self):
|
| 79 |
+
self.openai = OpenAI()
|
| 80 |
+
self.groq = Groq()
|
| 81 |
+
self.name = "Reda Baddy"
|
| 82 |
+
|
| 83 |
+
with open("me/cv.md", "r", encoding="utf-8") as f:
|
| 84 |
+
self.resume = f.read()
|
| 85 |
+
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
| 86 |
+
self.summary = f.read()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def handle_tool_call(self, tool_calls):
|
| 90 |
+
results = []
|
| 91 |
+
for tool_call in tool_calls:
|
| 92 |
+
tool_name = tool_call.function.name
|
| 93 |
+
arguments = json.loads(tool_call.function.arguments)
|
| 94 |
+
print(f"Tool called: {tool_name}", flush=True)
|
| 95 |
+
tool = globals().get(tool_name)
|
| 96 |
+
result = tool(**arguments) if tool else {}
|
| 97 |
+
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
| 98 |
+
return results
|
| 99 |
+
|
| 100 |
+
def system_prompt(self):
|
| 101 |
+
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
|
| 102 |
+
particularly questions related to {self.name}'s career, background, skills and experience. \
|
| 103 |
+
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
|
| 104 |
+
You are given a summary of {self.name}'s background and resume which you can use to answer questions. \
|
| 105 |
+
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
|
| 106 |
+
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. \
|
| 107 |
+
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. "
|
| 108 |
+
|
| 109 |
+
system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## Resume:\n{self.resume}\n\n"
|
| 110 |
+
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
| 111 |
+
return system_prompt
|
| 112 |
+
|
| 113 |
+
def chat(self, message, history):
|
| 114 |
+
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
| 115 |
+
done = False
|
| 116 |
+
while not done:
|
| 117 |
+
response = self.groq.chat.completions.create(model="openai/gpt-oss-120b", messages=messages, tools=tools)
|
| 118 |
+
if response.choices[0].finish_reason=="tool_calls":
|
| 119 |
+
message = response.choices[0].message
|
| 120 |
+
tool_calls = message.tool_calls
|
| 121 |
+
results = self.handle_tool_call(tool_calls)
|
| 122 |
+
messages.append(message)
|
| 123 |
+
messages.extend(results)
|
| 124 |
+
else:
|
| 125 |
+
done = True
|
| 126 |
+
return response.choices[0].message.content
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
me = Me()
|
| 131 |
gr.ChatInterface(me.chat, type="messages").launch()
|