MylesJ commited on
Commit
43f8fcb
·
verified ·
1 Parent(s): dc427ad

Upload 2 files

Browse files
Files changed (2) hide show
  1. APP.py +175 -0
  2. requirements.txt +6 -0
APP.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Models
4
+ Datasets
5
+ Spaces
6
+ Community
7
+ Docs
8
+ Pricing
9
+
10
+ Log In
11
+ Sign Up
12
+ Spaces:
13
+ RikaC
14
+ /
15
+ Rika_LLM_practice
16
+
17
+
18
+ like
19
+ 0
20
+ App
21
+ Files
22
+ Community
23
+ Rika_LLM_practice
24
+ /
25
+ app.py
26
+
27
+ RikaC's picture
28
+ RikaC
29
+ Upload 3 files
30
+ 0fa450b
31
+ verified
32
+ 27 minutes ago
33
+ raw
34
+
35
+ Copy download link
36
+ history
37
+ blame
38
+ contribute
39
+ delete
40
+
41
+ 5.24 kB
42
+ from dotenv import load_dotenv
43
+ from openai import OpenAI
44
+ import json
45
+ import os
46
+ import requests
47
+ from pypdf import PdfReader
48
+ import gradio as gr
49
+
50
+
51
+ load_dotenv(override=True)
52
+
53
+ def push(text):
54
+ requests.post(
55
+ "https://api.pushover.net/1/messages.json",
56
+ data={
57
+ "token": os.getenv("PUSHOVER_TOKEN"),
58
+ "user": os.getenv("PUSHOVER_USER"),
59
+ "message": text,
60
+ }
61
+ )
62
+
63
+
64
+ def record_user_details(email, name="Name not provided", notes="not provided"):
65
+ push(f"Recording {name} with email {email} and notes {notes}")
66
+ return {"recorded": "ok"}
67
+
68
+ def record_unknown_question(question):
69
+ push(f"Recording {question}")
70
+ return {"recorded": "ok"}
71
+
72
+ record_user_details_json = {
73
+ "name": "record_user_details",
74
+ "description": "Use this tool to record that a user is interested in being in touch and provided an email address",
75
+ "parameters": {
76
+ "type": "object",
77
+ "properties": {
78
+ "email": {
79
+ "type": "string",
80
+ "description": "The email address of this user"
81
+ },
82
+ "name": {
83
+ "type": "string",
84
+ "description": "The user's name, if they provided it"
85
+ }
86
+ ,
87
+ "notes": {
88
+ "type": "string",
89
+ "description": "Any additional information about the conversation that's worth recording to give context"
90
+ }
91
+ },
92
+ "required": ["email"],
93
+ "additionalProperties": False
94
+ }
95
+ }
96
+
97
+ record_unknown_question_json = {
98
+ "name": "record_unknown_question",
99
+ "description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer",
100
+ "parameters": {
101
+ "type": "object",
102
+ "properties": {
103
+ "question": {
104
+ "type": "string",
105
+ "description": "The question that couldn't be answered"
106
+ },
107
+ },
108
+ "required": ["question"],
109
+ "additionalProperties": False
110
+ }
111
+ }
112
+
113
+ tools = [{"type": "function", "function": record_user_details_json},
114
+ {"type": "function", "function": record_unknown_question_json}]
115
+
116
+
117
+ class Me:
118
+
119
+ def __init__(self):
120
+ self.openai = OpenAI()
121
+ self.name = "Myles Jung"
122
+ reader = PdfReader("me/linkedin.pdf")
123
+ self.linkedin = ""
124
+ for page in reader.pages:
125
+ text = page.extract_text()
126
+ if text:
127
+ self.linkedin += text
128
+ with open("me/summary.txt", "r", encoding="utf-8") as f:
129
+ self.summary = f.read()
130
+
131
+
132
+ def handle_tool_call(self, tool_calls):
133
+ results = []
134
+ for tool_call in tool_calls:
135
+ tool_name = tool_call.function.name
136
+ arguments = json.loads(tool_call.function.arguments)
137
+ print(f"Tool called: {tool_name}", flush=True)
138
+ tool = globals().get(tool_name)
139
+ result = tool(**arguments) if tool else {}
140
+ results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
141
+ return results
142
+
143
+ def system_prompt(self):
144
+ system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
145
+ particularly questions related to {self.name}'s career, background, skills and experience. \
146
+ Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
147
+ You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
148
+ Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
149
+ 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. \
150
+ 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. "
151
+
152
+ system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
153
+ system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
154
+ return system_prompt
155
+
156
+ def chat(self, message, history):
157
+ messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
158
+ done = False
159
+ while not done:
160
+ response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools)
161
+ if response.choices[0].finish_reason=="tool_calls":
162
+ message = response.choices[0].message
163
+ tool_calls = message.tool_calls
164
+ results = self.handle_tool_call(tool_calls)
165
+ messages.append(message)
166
+ messages.extend(results)
167
+ else:
168
+ done = True
169
+ return response.choices[0].message.content
170
+
171
+
172
+ if __name__ == "__main__":
173
+ me = Me()
174
+ gr.ChatInterface(me.chat, type="messages").launch()
175
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ requests
2
+ python-dotenv
3
+ gradio
4
+ pypdf
5
+ openai
6
+ openai-agents