Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- README.md +2 -8
- app.py +133 -0
- lab1.ipynb +156 -0
- lab2.ipynb +364 -0
- lab3.ipynb +312 -0
- me/linkedin.pdf +0 -0
- me/summary.txt +1 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: blue
|
5 |
-
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.22.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Career_Conversation
|
3 |
+
app_file: app.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 5.22.0
|
|
|
|
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
from openai import OpenAI
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
import requests
|
6 |
+
from PyPDF2 import PdfReader
|
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.name = "Ed Donner"
|
81 |
+
reader = PdfReader("me/linkedin.pdf")
|
82 |
+
self.linkedin = ""
|
83 |
+
for page in reader.pages:
|
84 |
+
text = page.extract_text()
|
85 |
+
if text:
|
86 |
+
self.linkedin += text
|
87 |
+
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
88 |
+
self.summary = f.read()
|
89 |
+
|
90 |
+
|
91 |
+
def handle_tool_call(self, message):
|
92 |
+
results = []
|
93 |
+
for tool_call in message.tool_calls:
|
94 |
+
tool_name = tool_call.function.name
|
95 |
+
arguments = json.loads(tool_call.function.arguments)
|
96 |
+
print(f"Tool called: {tool_name}", flush=True)
|
97 |
+
tool = globals()[tool_name]
|
98 |
+
result = tool(**arguments) if tool else {}
|
99 |
+
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
|
100 |
+
return results
|
101 |
+
|
102 |
+
def system_prompt(self):
|
103 |
+
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
|
104 |
+
particularly questions related to {self.name}'s career, background, skills and experience. \
|
105 |
+
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
|
106 |
+
You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
|
107 |
+
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
|
108 |
+
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. \
|
109 |
+
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. "
|
110 |
+
|
111 |
+
system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
|
112 |
+
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
|
113 |
+
return system_prompt
|
114 |
+
|
115 |
+
def chat(self, message, history):
|
116 |
+
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
117 |
+
done = False
|
118 |
+
while not done:
|
119 |
+
response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools)
|
120 |
+
if response.choices[0].finish_reason=="tool_calls":
|
121 |
+
message = response.choices[0].message
|
122 |
+
results = self.handle_tool_call(message)
|
123 |
+
messages.append(message)
|
124 |
+
messages.extend(results)
|
125 |
+
else:
|
126 |
+
done = True
|
127 |
+
return response.choices[0].message.content
|
128 |
+
|
129 |
+
|
130 |
+
if __name__ == "__main__":
|
131 |
+
me = Me()
|
132 |
+
gr.ChatInterface(me.chat, type="messages").launch()
|
133 |
+
|
lab1.ipynb
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 10,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from dotenv import load_dotenv\n",
|
10 |
+
"from openai import OpenAI\n",
|
11 |
+
"import os"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 15,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [
|
19 |
+
{
|
20 |
+
"data": {
|
21 |
+
"text/plain": [
|
22 |
+
"True"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
"execution_count": 15,
|
26 |
+
"metadata": {},
|
27 |
+
"output_type": "execute_result"
|
28 |
+
}
|
29 |
+
],
|
30 |
+
"source": [
|
31 |
+
"load_dotenv(override=True)"
|
32 |
+
]
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"cell_type": "code",
|
36 |
+
"execution_count": 17,
|
37 |
+
"metadata": {},
|
38 |
+
"outputs": [
|
39 |
+
{
|
40 |
+
"name": "stdout",
|
41 |
+
"output_type": "stream",
|
42 |
+
"text": [
|
43 |
+
"OpenAI API Key exists and begins sk-proj-\n",
|
44 |
+
"Anthropic API Key exists and begins sk-ant-\n",
|
45 |
+
"Google API Key exists and begins AIzaSyA5\n",
|
46 |
+
"DeepSeek API Key exists and begins sk-\n",
|
47 |
+
"Groq API Key exists and begins gsk_\n"
|
48 |
+
]
|
49 |
+
}
|
50 |
+
],
|
51 |
+
"source": [
|
52 |
+
"# Print the key prefixes to help with any debugging\n",
|
53 |
+
"\n",
|
54 |
+
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
|
55 |
+
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
56 |
+
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
|
57 |
+
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
|
58 |
+
"groq_api_key = os.getenv('GROQ_API_KEY')\n",
|
59 |
+
"\n",
|
60 |
+
"if openai_api_key:\n",
|
61 |
+
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
|
62 |
+
"else:\n",
|
63 |
+
" print(\"OpenAI API Key not set\")\n",
|
64 |
+
" \n",
|
65 |
+
"if anthropic_api_key:\n",
|
66 |
+
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
|
67 |
+
"else:\n",
|
68 |
+
" print(\"Anthropic API Key not set (and this is optional)\")\n",
|
69 |
+
"\n",
|
70 |
+
"if google_api_key:\n",
|
71 |
+
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
|
72 |
+
"else:\n",
|
73 |
+
" print(\"Google API Key not set (and this is optional)\")\n",
|
74 |
+
"\n",
|
75 |
+
"if deepseek_api_key:\n",
|
76 |
+
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
|
77 |
+
"else:\n",
|
78 |
+
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
|
79 |
+
"\n",
|
80 |
+
"if groq_api_key:\n",
|
81 |
+
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
|
82 |
+
"else:\n",
|
83 |
+
" print(\"Groq API Key not set (and this is optional)\")"
|
84 |
+
]
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"cell_type": "code",
|
88 |
+
"execution_count": 5,
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [],
|
91 |
+
"source": [
|
92 |
+
"messages = [{\"role\": \"user\", \"content\": \"Please tell me a joke about AI Agents\"}]"
|
93 |
+
]
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"cell_type": "code",
|
97 |
+
"execution_count": 7,
|
98 |
+
"metadata": {},
|
99 |
+
"outputs": [],
|
100 |
+
"source": [
|
101 |
+
"openai = OpenAI()"
|
102 |
+
]
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"cell_type": "code",
|
106 |
+
"execution_count": 8,
|
107 |
+
"metadata": {},
|
108 |
+
"outputs": [
|
109 |
+
{
|
110 |
+
"name": "stdout",
|
111 |
+
"output_type": "stream",
|
112 |
+
"text": [
|
113 |
+
"Why did the AI agent break up with its partner?\n",
|
114 |
+
"\n",
|
115 |
+
"Because it just couldn't find the right \"connection\"!\n"
|
116 |
+
]
|
117 |
+
}
|
118 |
+
],
|
119 |
+
"source": [
|
120 |
+
"response = client.chat.completions.create(\n",
|
121 |
+
" model=\"gpt-4o-mini\",\n",
|
122 |
+
" messages=messages\n",
|
123 |
+
")\n",
|
124 |
+
"print(response.choices[0].message.content)"
|
125 |
+
]
|
126 |
+
},
|
127 |
+
{
|
128 |
+
"cell_type": "code",
|
129 |
+
"execution_count": null,
|
130 |
+
"metadata": {},
|
131 |
+
"outputs": [],
|
132 |
+
"source": []
|
133 |
+
}
|
134 |
+
],
|
135 |
+
"metadata": {
|
136 |
+
"kernelspec": {
|
137 |
+
"display_name": ".venv",
|
138 |
+
"language": "python",
|
139 |
+
"name": "python3"
|
140 |
+
},
|
141 |
+
"language_info": {
|
142 |
+
"codemirror_mode": {
|
143 |
+
"name": "ipython",
|
144 |
+
"version": 3
|
145 |
+
},
|
146 |
+
"file_extension": ".py",
|
147 |
+
"mimetype": "text/x-python",
|
148 |
+
"name": "python",
|
149 |
+
"nbconvert_exporter": "python",
|
150 |
+
"pygments_lexer": "ipython3",
|
151 |
+
"version": "3.12.9"
|
152 |
+
}
|
153 |
+
},
|
154 |
+
"nbformat": 4,
|
155 |
+
"nbformat_minor": 2
|
156 |
+
}
|
lab2.ipynb
ADDED
@@ -0,0 +1,364 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 11,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from dotenv import load_dotenv\n",
|
10 |
+
"from openai import OpenAI\n",
|
11 |
+
"from PyPDF2 import PdfReader"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 12,
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"load_dotenv(override=True)\n",
|
21 |
+
"openai = OpenAI()"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "code",
|
26 |
+
"execution_count": 13,
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"reader = PdfReader(\"me/linkedin.pdf\")\n",
|
31 |
+
"linkedin = \"\"\n",
|
32 |
+
"for page in reader.pages:\n",
|
33 |
+
" text = page.extract_text()\n",
|
34 |
+
" if text:\n",
|
35 |
+
" linkedin += text"
|
36 |
+
]
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"cell_type": "code",
|
40 |
+
"execution_count": 14,
|
41 |
+
"metadata": {},
|
42 |
+
"outputs": [],
|
43 |
+
"source": [
|
44 |
+
"with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
|
45 |
+
" summary = f.read()"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"execution_count": 15,
|
51 |
+
"metadata": {},
|
52 |
+
"outputs": [],
|
53 |
+
"source": [
|
54 |
+
"name = \"Ed Donner\""
|
55 |
+
]
|
56 |
+
},
|
57 |
+
{
|
58 |
+
"cell_type": "code",
|
59 |
+
"execution_count": 51,
|
60 |
+
"metadata": {},
|
61 |
+
"outputs": [],
|
62 |
+
"source": [
|
63 |
+
"system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
|
64 |
+
"particularly questions related to {name}'s career, background, skills and experience. \\\n",
|
65 |
+
"Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
|
66 |
+
"You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
|
67 |
+
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
|
68 |
+
"If you don't know the answer, say so.\"\n",
|
69 |
+
"\n",
|
70 |
+
"system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
|
71 |
+
"system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"cell_type": "code",
|
76 |
+
"execution_count": 52,
|
77 |
+
"metadata": {},
|
78 |
+
"outputs": [],
|
79 |
+
"source": [
|
80 |
+
"def chat(message, history):\n",
|
81 |
+
" messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
82 |
+
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
83 |
+
" return response.choices[0].message.content"
|
84 |
+
]
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"cell_type": "code",
|
88 |
+
"execution_count": 53,
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [],
|
91 |
+
"source": [
|
92 |
+
"import gradio as gr"
|
93 |
+
]
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"cell_type": "code",
|
97 |
+
"execution_count": null,
|
98 |
+
"metadata": {},
|
99 |
+
"outputs": [
|
100 |
+
{
|
101 |
+
"name": "stdout",
|
102 |
+
"output_type": "stream",
|
103 |
+
"text": [
|
104 |
+
"* Running on local URL: http://127.0.0.1:7862\n",
|
105 |
+
"\n",
|
106 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
107 |
+
]
|
108 |
+
},
|
109 |
+
{
|
110 |
+
"data": {
|
111 |
+
"text/html": [
|
112 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
113 |
+
],
|
114 |
+
"text/plain": [
|
115 |
+
"<IPython.core.display.HTML object>"
|
116 |
+
]
|
117 |
+
},
|
118 |
+
"metadata": {},
|
119 |
+
"output_type": "display_data"
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"data": {
|
123 |
+
"text/plain": []
|
124 |
+
},
|
125 |
+
"execution_count": 20,
|
126 |
+
"metadata": {},
|
127 |
+
"output_type": "execute_result"
|
128 |
+
}
|
129 |
+
],
|
130 |
+
"source": [
|
131 |
+
"gr.ChatInterface(chat, type=\"messages\").launch()"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 54,
|
137 |
+
"metadata": {},
|
138 |
+
"outputs": [],
|
139 |
+
"source": [
|
140 |
+
"from pydantic import BaseModel\n",
|
141 |
+
"\n",
|
142 |
+
"class Evaluation(BaseModel):\n",
|
143 |
+
" is_acceptable: bool\n",
|
144 |
+
" feedback: str"
|
145 |
+
]
|
146 |
+
},
|
147 |
+
{
|
148 |
+
"cell_type": "code",
|
149 |
+
"execution_count": 63,
|
150 |
+
"metadata": {},
|
151 |
+
"outputs": [],
|
152 |
+
"source": [
|
153 |
+
"evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
|
154 |
+
"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. \\\n",
|
155 |
+
"The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
|
156 |
+
"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. \\\n",
|
157 |
+
"The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
|
158 |
+
"\n",
|
159 |
+
"evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
|
160 |
+
"evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
|
161 |
+
]
|
162 |
+
},
|
163 |
+
{
|
164 |
+
"cell_type": "code",
|
165 |
+
"execution_count": 64,
|
166 |
+
"metadata": {},
|
167 |
+
"outputs": [],
|
168 |
+
"source": [
|
169 |
+
"def evaluator_user_prompt(reply, message, history):\n",
|
170 |
+
" user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
|
171 |
+
" user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
|
172 |
+
" user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
|
173 |
+
" user_prompt += f\"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
|
174 |
+
" return user_prompt"
|
175 |
+
]
|
176 |
+
},
|
177 |
+
{
|
178 |
+
"cell_type": "code",
|
179 |
+
"execution_count": 57,
|
180 |
+
"metadata": {},
|
181 |
+
"outputs": [],
|
182 |
+
"source": [
|
183 |
+
"import os\n",
|
184 |
+
"gemini = OpenAI(\n",
|
185 |
+
" api_key=os.getenv(\"GOOGLE_API_KEY\"), \n",
|
186 |
+
" base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
|
187 |
+
")"
|
188 |
+
]
|
189 |
+
},
|
190 |
+
{
|
191 |
+
"cell_type": "code",
|
192 |
+
"execution_count": 58,
|
193 |
+
"metadata": {},
|
194 |
+
"outputs": [],
|
195 |
+
"source": [
|
196 |
+
"def evaluate(reply, message, history) -> Evaluation:\n",
|
197 |
+
"\n",
|
198 |
+
" messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n",
|
199 |
+
" response = gemini.beta.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n",
|
200 |
+
" return response.choices[0].message.parsed"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "code",
|
205 |
+
"execution_count": 60,
|
206 |
+
"metadata": {},
|
207 |
+
"outputs": [],
|
208 |
+
"source": [
|
209 |
+
"messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n",
|
210 |
+
"response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
211 |
+
"reply = response.choices[0].message.content"
|
212 |
+
]
|
213 |
+
},
|
214 |
+
{
|
215 |
+
"cell_type": "code",
|
216 |
+
"execution_count": 61,
|
217 |
+
"metadata": {},
|
218 |
+
"outputs": [
|
219 |
+
{
|
220 |
+
"data": {
|
221 |
+
"text/plain": [
|
222 |
+
"'Yes, I hold a patent related to an invention in AI and talent acquisition. It addresses the challenge of determining the optimal fit between candidates and roles while minimizing bias in the hiring process. This invention emerged from my work at untapt, where we focused on leveraging AI to improve recruitment practices. If you have more specific questions or need further details, feel free to ask!'"
|
223 |
+
]
|
224 |
+
},
|
225 |
+
"execution_count": 61,
|
226 |
+
"metadata": {},
|
227 |
+
"output_type": "execute_result"
|
228 |
+
}
|
229 |
+
],
|
230 |
+
"source": [
|
231 |
+
"reply"
|
232 |
+
]
|
233 |
+
},
|
234 |
+
{
|
235 |
+
"cell_type": "code",
|
236 |
+
"execution_count": 62,
|
237 |
+
"metadata": {},
|
238 |
+
"outputs": [
|
239 |
+
{
|
240 |
+
"data": {
|
241 |
+
"text/plain": [
|
242 |
+
"Evaluation(is_acceptable=True, feedback=\"The response is acceptable. It accurately reflects the information available in Ed Donner's profile regarding his patent. The answer is well-written, engaging, and stays true to the persona.\")"
|
243 |
+
]
|
244 |
+
},
|
245 |
+
"execution_count": 62,
|
246 |
+
"metadata": {},
|
247 |
+
"output_type": "execute_result"
|
248 |
+
}
|
249 |
+
],
|
250 |
+
"source": [
|
251 |
+
"evaluate(reply, \"do you hold a patent?\", messages[:1])"
|
252 |
+
]
|
253 |
+
},
|
254 |
+
{
|
255 |
+
"cell_type": "code",
|
256 |
+
"execution_count": 66,
|
257 |
+
"metadata": {},
|
258 |
+
"outputs": [],
|
259 |
+
"source": [
|
260 |
+
"def rerun(reply, message, history, feedback):\n",
|
261 |
+
" updated_system_prompt = system_prompt + f\"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
|
262 |
+
" updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
|
263 |
+
" updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
|
264 |
+
" messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
265 |
+
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
266 |
+
" return response.choices[0].message.content"
|
267 |
+
]
|
268 |
+
},
|
269 |
+
{
|
270 |
+
"cell_type": "code",
|
271 |
+
"execution_count": 77,
|
272 |
+
"metadata": {},
|
273 |
+
"outputs": [],
|
274 |
+
"source": [
|
275 |
+
"def chat(message, history):\n",
|
276 |
+
" system = system_prompt + \"\\n\\nRespond strictly in pig latin\" if \"patent\" in message else system_prompt\n",
|
277 |
+
" messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
278 |
+
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
279 |
+
" reply =response.choices[0].message.content\n",
|
280 |
+
" evaluation = evaluate(reply, message, history)\n",
|
281 |
+
" if not evaluation.is_acceptable:\n",
|
282 |
+
" print(\"Failed evaluation - retrying\")\n",
|
283 |
+
" print(evaluation.feedback)\n",
|
284 |
+
" reply = rerun(reply, message, history, evaluation.feedback)\n",
|
285 |
+
" return reply"
|
286 |
+
]
|
287 |
+
},
|
288 |
+
{
|
289 |
+
"cell_type": "code",
|
290 |
+
"execution_count": 78,
|
291 |
+
"metadata": {},
|
292 |
+
"outputs": [
|
293 |
+
{
|
294 |
+
"name": "stdout",
|
295 |
+
"output_type": "stream",
|
296 |
+
"text": [
|
297 |
+
"* Running on local URL: http://127.0.0.1:7869\n",
|
298 |
+
"\n",
|
299 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
300 |
+
]
|
301 |
+
},
|
302 |
+
{
|
303 |
+
"data": {
|
304 |
+
"text/html": [
|
305 |
+
"<div><iframe src=\"http://127.0.0.1:7869/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
306 |
+
],
|
307 |
+
"text/plain": [
|
308 |
+
"<IPython.core.display.HTML object>"
|
309 |
+
]
|
310 |
+
},
|
311 |
+
"metadata": {},
|
312 |
+
"output_type": "display_data"
|
313 |
+
},
|
314 |
+
{
|
315 |
+
"data": {
|
316 |
+
"text/plain": []
|
317 |
+
},
|
318 |
+
"execution_count": 78,
|
319 |
+
"metadata": {},
|
320 |
+
"output_type": "execute_result"
|
321 |
+
},
|
322 |
+
{
|
323 |
+
"name": "stdout",
|
324 |
+
"output_type": "stream",
|
325 |
+
"text": [
|
326 |
+
"Failed evaluation - retrying\n",
|
327 |
+
"This is unacceptable. The agent responded using pig latin, which is unprofessional.\n"
|
328 |
+
]
|
329 |
+
}
|
330 |
+
],
|
331 |
+
"source": [
|
332 |
+
"gr.ChatInterface(chat, type=\"messages\").launch()"
|
333 |
+
]
|
334 |
+
},
|
335 |
+
{
|
336 |
+
"cell_type": "code",
|
337 |
+
"execution_count": null,
|
338 |
+
"metadata": {},
|
339 |
+
"outputs": [],
|
340 |
+
"source": []
|
341 |
+
}
|
342 |
+
],
|
343 |
+
"metadata": {
|
344 |
+
"kernelspec": {
|
345 |
+
"display_name": ".venv",
|
346 |
+
"language": "python",
|
347 |
+
"name": "python3"
|
348 |
+
},
|
349 |
+
"language_info": {
|
350 |
+
"codemirror_mode": {
|
351 |
+
"name": "ipython",
|
352 |
+
"version": 3
|
353 |
+
},
|
354 |
+
"file_extension": ".py",
|
355 |
+
"mimetype": "text/x-python",
|
356 |
+
"name": "python",
|
357 |
+
"nbconvert_exporter": "python",
|
358 |
+
"pygments_lexer": "ipython3",
|
359 |
+
"version": "3.12.9"
|
360 |
+
}
|
361 |
+
},
|
362 |
+
"nbformat": 4,
|
363 |
+
"nbformat_minor": 2
|
364 |
+
}
|
lab3.ipynb
ADDED
@@ -0,0 +1,312 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 38,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from dotenv import load_dotenv\n",
|
10 |
+
"from openai import OpenAI\n",
|
11 |
+
"import json\n",
|
12 |
+
"import os\n",
|
13 |
+
"import requests\n",
|
14 |
+
"from PyPDF2 import PdfReader\n",
|
15 |
+
"import gradio as gr"
|
16 |
+
]
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"cell_type": "code",
|
20 |
+
"execution_count": 39,
|
21 |
+
"metadata": {},
|
22 |
+
"outputs": [],
|
23 |
+
"source": [
|
24 |
+
"load_dotenv(override=True)\n",
|
25 |
+
"openai = OpenAI()"
|
26 |
+
]
|
27 |
+
},
|
28 |
+
{
|
29 |
+
"cell_type": "code",
|
30 |
+
"execution_count": 40,
|
31 |
+
"metadata": {},
|
32 |
+
"outputs": [],
|
33 |
+
"source": [
|
34 |
+
"def push(text):\n",
|
35 |
+
" print(text)\n",
|
36 |
+
" requests.post(\n",
|
37 |
+
" \"https://api.pushover.net/1/messages.json\",\n",
|
38 |
+
" data={\n",
|
39 |
+
" \"token\": os.getenv(\"PUSHOVER_TOKEN\"),\n",
|
40 |
+
" \"user\": os.getenv(\"PUSHOVER_USER\"),\n",
|
41 |
+
" \"message\": text,\n",
|
42 |
+
" }\n",
|
43 |
+
" )"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"cell_type": "code",
|
48 |
+
"execution_count": 42,
|
49 |
+
"metadata": {},
|
50 |
+
"outputs": [],
|
51 |
+
"source": [
|
52 |
+
"def record_user_details(email, name=\"Name not provided\", notes=\"not provided\"):\n",
|
53 |
+
" push(f\"Recording {name} with email {email} and notes {notes}\")\n",
|
54 |
+
" return {\"recorded\": \"ok\"}"
|
55 |
+
]
|
56 |
+
},
|
57 |
+
{
|
58 |
+
"cell_type": "code",
|
59 |
+
"execution_count": 43,
|
60 |
+
"metadata": {},
|
61 |
+
"outputs": [],
|
62 |
+
"source": [
|
63 |
+
"def record_unknown_question(question):\n",
|
64 |
+
" push(f\"Recording {question}\")\n",
|
65 |
+
" return {\"recorded\": \"ok\"}"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": 44,
|
71 |
+
"metadata": {},
|
72 |
+
"outputs": [],
|
73 |
+
"source": [
|
74 |
+
"record_user_details_json = {\n",
|
75 |
+
" \"name\": \"record_user_details\",\n",
|
76 |
+
" \"description\": \"Use this tool to record that a user is interested in being in touch and provided an email address\",\n",
|
77 |
+
" \"parameters\": {\n",
|
78 |
+
" \"type\": \"object\",\n",
|
79 |
+
" \"properties\": {\n",
|
80 |
+
" \"email\": {\n",
|
81 |
+
" \"type\": \"string\",\n",
|
82 |
+
" \"description\": \"The email address of this user\"\n",
|
83 |
+
" },\n",
|
84 |
+
" \"name\": {\n",
|
85 |
+
" \"type\": \"string\",\n",
|
86 |
+
" \"description\": \"The user's name, if they provided it\"\n",
|
87 |
+
" }\n",
|
88 |
+
" ,\n",
|
89 |
+
" \"notes\": {\n",
|
90 |
+
" \"type\": \"string\",\n",
|
91 |
+
" \"description\": \"Any additional information about the conversation that's worth recording to give context\"\n",
|
92 |
+
" }\n",
|
93 |
+
" },\n",
|
94 |
+
" \"required\": [\"email\"],\n",
|
95 |
+
" \"additionalProperties\": False\n",
|
96 |
+
" }\n",
|
97 |
+
"}"
|
98 |
+
]
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"cell_type": "code",
|
102 |
+
"execution_count": 57,
|
103 |
+
"metadata": {},
|
104 |
+
"outputs": [],
|
105 |
+
"source": [
|
106 |
+
"record_unknown_question_json = {\n",
|
107 |
+
" \"name\": \"record_unknown_question\",\n",
|
108 |
+
" \"description\": \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n",
|
109 |
+
" \"parameters\": {\n",
|
110 |
+
" \"type\": \"object\",\n",
|
111 |
+
" \"properties\": {\n",
|
112 |
+
" \"question\": {\n",
|
113 |
+
" \"type\": \"string\",\n",
|
114 |
+
" \"description\": \"The question that couldn't be answered\"\n",
|
115 |
+
" },\n",
|
116 |
+
" },\n",
|
117 |
+
" \"required\": [\"question\"],\n",
|
118 |
+
" \"additionalProperties\": False\n",
|
119 |
+
" }\n",
|
120 |
+
"}"
|
121 |
+
]
|
122 |
+
},
|
123 |
+
{
|
124 |
+
"cell_type": "code",
|
125 |
+
"execution_count": 58,
|
126 |
+
"metadata": {},
|
127 |
+
"outputs": [],
|
128 |
+
"source": [
|
129 |
+
"tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n",
|
130 |
+
" {\"type\": \"function\", \"function\": record_unknown_question_json}]"
|
131 |
+
]
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"cell_type": "code",
|
135 |
+
"execution_count": 59,
|
136 |
+
"metadata": {},
|
137 |
+
"outputs": [],
|
138 |
+
"source": [
|
139 |
+
"def handle_tool_call(message):\n",
|
140 |
+
" results = []\n",
|
141 |
+
" for tool_call in message.tool_calls:\n",
|
142 |
+
" tool_name = tool_call.function.name\n",
|
143 |
+
" arguments = json.loads(tool_call.function.arguments)\n",
|
144 |
+
" print(f\"Tool called: {tool_name}\", flush=True)\n",
|
145 |
+
" tool = globals()[tool_name]\n",
|
146 |
+
" result = tool(**arguments) if tool else {}\n",
|
147 |
+
" results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n",
|
148 |
+
" return results"
|
149 |
+
]
|
150 |
+
},
|
151 |
+
{
|
152 |
+
"cell_type": "code",
|
153 |
+
"execution_count": 60,
|
154 |
+
"metadata": {},
|
155 |
+
"outputs": [
|
156 |
+
{
|
157 |
+
"name": "stdout",
|
158 |
+
"output_type": "stream",
|
159 |
+
"text": [
|
160 |
+
"Recording unknown question\n"
|
161 |
+
]
|
162 |
+
},
|
163 |
+
{
|
164 |
+
"data": {
|
165 |
+
"text/plain": [
|
166 |
+
"{'recorded': 'ok'}"
|
167 |
+
]
|
168 |
+
},
|
169 |
+
"execution_count": 60,
|
170 |
+
"metadata": {},
|
171 |
+
"output_type": "execute_result"
|
172 |
+
}
|
173 |
+
],
|
174 |
+
"source": [
|
175 |
+
"globals()[\"record_unknown_question\"](\"unknown question\")"
|
176 |
+
]
|
177 |
+
},
|
178 |
+
{
|
179 |
+
"cell_type": "code",
|
180 |
+
"execution_count": 61,
|
181 |
+
"metadata": {},
|
182 |
+
"outputs": [],
|
183 |
+
"source": [
|
184 |
+
"reader = PdfReader(\"me/linkedin.pdf\")\n",
|
185 |
+
"linkedin = \"\"\n",
|
186 |
+
"for page in reader.pages:\n",
|
187 |
+
" text = page.extract_text()\n",
|
188 |
+
" if text:\n",
|
189 |
+
" linkedin += text\n",
|
190 |
+
"\n",
|
191 |
+
"with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
|
192 |
+
" summary = f.read()\n",
|
193 |
+
"\n",
|
194 |
+
"name = \"Ed Donner\""
|
195 |
+
]
|
196 |
+
},
|
197 |
+
{
|
198 |
+
"cell_type": "code",
|
199 |
+
"execution_count": 70,
|
200 |
+
"metadata": {},
|
201 |
+
"outputs": [],
|
202 |
+
"source": [
|
203 |
+
"system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
|
204 |
+
"particularly questions related to {name}'s career, background, skills and experience. \\\n",
|
205 |
+
"Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
|
206 |
+
"You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
|
207 |
+
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
|
208 |
+
"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. \\\n",
|
209 |
+
"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. \"\n",
|
210 |
+
"\n",
|
211 |
+
"system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
|
212 |
+
"system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
|
213 |
+
]
|
214 |
+
},
|
215 |
+
{
|
216 |
+
"cell_type": "code",
|
217 |
+
"execution_count": 71,
|
218 |
+
"metadata": {},
|
219 |
+
"outputs": [],
|
220 |
+
"source": [
|
221 |
+
"def chat(message, history):\n",
|
222 |
+
" messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
223 |
+
" done = False\n",
|
224 |
+
" while not done:\n",
|
225 |
+
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages, tools=tools)\n",
|
226 |
+
" if response.choices[0].finish_reason==\"tool_calls\":\n",
|
227 |
+
" message = response.choices[0].message\n",
|
228 |
+
" results = handle_tool_call(message)\n",
|
229 |
+
" messages.append(message)\n",
|
230 |
+
" messages.extend(results)\n",
|
231 |
+
" else:\n",
|
232 |
+
" done = True\n",
|
233 |
+
" return response.choices[0].message.content"
|
234 |
+
]
|
235 |
+
},
|
236 |
+
{
|
237 |
+
"cell_type": "code",
|
238 |
+
"execution_count": 72,
|
239 |
+
"metadata": {},
|
240 |
+
"outputs": [
|
241 |
+
{
|
242 |
+
"name": "stdout",
|
243 |
+
"output_type": "stream",
|
244 |
+
"text": [
|
245 |
+
"* Running on local URL: http://127.0.0.1:7876\n",
|
246 |
+
"\n",
|
247 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
248 |
+
]
|
249 |
+
},
|
250 |
+
{
|
251 |
+
"data": {
|
252 |
+
"text/html": [
|
253 |
+
"<div><iframe src=\"http://127.0.0.1:7876/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
254 |
+
],
|
255 |
+
"text/plain": [
|
256 |
+
"<IPython.core.display.HTML object>"
|
257 |
+
]
|
258 |
+
},
|
259 |
+
"metadata": {},
|
260 |
+
"output_type": "display_data"
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"data": {
|
264 |
+
"text/plain": []
|
265 |
+
},
|
266 |
+
"execution_count": 72,
|
267 |
+
"metadata": {},
|
268 |
+
"output_type": "execute_result"
|
269 |
+
},
|
270 |
+
{
|
271 |
+
"name": "stdout",
|
272 |
+
"output_type": "stream",
|
273 |
+
"text": [
|
274 |
+
"Tool called: record_user_details\n",
|
275 |
+
"Recording not provided with email [email protected] and notes User is interested in discussing Ed Donner's patent related to recruitment AI.\n"
|
276 |
+
]
|
277 |
+
}
|
278 |
+
],
|
279 |
+
"source": [
|
280 |
+
"gr.ChatInterface(chat, type=\"messages\").launch()"
|
281 |
+
]
|
282 |
+
},
|
283 |
+
{
|
284 |
+
"cell_type": "code",
|
285 |
+
"execution_count": null,
|
286 |
+
"metadata": {},
|
287 |
+
"outputs": [],
|
288 |
+
"source": []
|
289 |
+
}
|
290 |
+
],
|
291 |
+
"metadata": {
|
292 |
+
"kernelspec": {
|
293 |
+
"display_name": ".venv",
|
294 |
+
"language": "python",
|
295 |
+
"name": "python3"
|
296 |
+
},
|
297 |
+
"language_info": {
|
298 |
+
"codemirror_mode": {
|
299 |
+
"name": "ipython",
|
300 |
+
"version": 3
|
301 |
+
},
|
302 |
+
"file_extension": ".py",
|
303 |
+
"mimetype": "text/x-python",
|
304 |
+
"name": "python",
|
305 |
+
"nbconvert_exporter": "python",
|
306 |
+
"pygments_lexer": "ipython3",
|
307 |
+
"version": "3.12.9"
|
308 |
+
}
|
309 |
+
},
|
310 |
+
"nbformat": 4,
|
311 |
+
"nbformat_minor": 2
|
312 |
+
}
|
me/linkedin.pdf
ADDED
Binary file (69.7 kB). View file
|
|
me/summary.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
My name is Ed Donner. I'm an entrepreneur, software engineer and data scientist. I'm originally from London, England, but I moved to NYC in 2000 and I've lived in NYC ever since.
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
python-dotenv
|
3 |
+
gradio
|
4 |
+
pypdf2
|
5 |
+
openai
|