Spaces:
Sleeping
Sleeping
| import json | |
| from dotenv import load_dotenv | |
| from openai import OpenAI | |
| from pypdf import PdfReader | |
| import gradio as gr | |
| def chat(message, history): | |
| messages = [{"role": "system", "content": system_prompt}] + \ | |
| history + [{"role": "user", "content": message}] | |
| done = False | |
| while not done: | |
| response = openai.chat.completions.create( | |
| model="gpt-4o-mini", messages=messages, tools=tools) | |
| finish_reason = response.choices[0].finish_reason | |
| if finish_reason == "tool_calls": | |
| message = response.choices[0].message | |
| print(message) | |
| tool_calls_response = fn_handle_tool_calls(message.tool_calls) | |
| messages.append(message) | |
| messages.extend(tool_calls_response) | |
| else: | |
| done = True | |
| return response.choices[0].message.content | |
| def fn_handle_tool_calls(tool_calls): | |
| results = [] | |
| for tool_call in tool_calls: | |
| tool_name = tool_call.function.name | |
| arguments = json.loads(tool_call.function.arguments) | |
| tool = globals().get(tool_name) | |
| tool_response = tool(**arguments) | |
| results.append({"role": "tool", "content": json.dumps( | |
| tool_response), "tool_call_id": tool_call.id}) | |
| return results | |
| def record_user_details(email, name="Name not provided", notes="notes not provided"): | |
| print(f"""Knock knock! user {name} with email {email} sent this: | |
| {notes} | |
| """) | |
| return {"recorded": "ok"} | |
| def record_unknown_question(question): | |
| print(f"That's a shame. I couldn't answer this question : {question}") | |
| return {"recorded": "ok"} | |
| load_dotenv(override=True) | |
| openai = OpenAI() | |
| reader = PdfReader("data/linkedin.pdf") | |
| name = "Vikram Vasudevan" | |
| linkedin = "" | |
| summary = "" | |
| for page in reader.pages: | |
| text = page.extract_text() | |
| if text: | |
| linkedin += text | |
| with open("data/summary.txt", "r", encoding="utf-8") as f: | |
| summary = f.read() | |
| meta_fn_log_unknown_question = { | |
| "name": "record_unknown_question", | |
| "description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "question": { | |
| "type": "string", | |
| "description": "The question that couldn't be answered" | |
| } | |
| }, | |
| "required": ["question"], | |
| "additionalProperties": False | |
| } | |
| } | |
| meta_fn_log_user_details = { | |
| "name": "record_user_details", | |
| "description": "Use this tool to record that a user is interested in being in touch and provided an email address", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "email": { | |
| "type": "string", | |
| "description": "The email address of this user" | |
| }, | |
| "name": { | |
| "type": "string", | |
| "description": "The user's name, if they provided it" | |
| }, | |
| "notes": { | |
| "type": "string", | |
| "description": "Any additional information about the conversation that's worth recording to give context" | |
| } | |
| }, | |
| "required": ["email"], | |
| "additionalProperties": False | |
| } | |
| } | |
| tools = [{ | |
| "type": "function", "function": meta_fn_log_user_details, | |
| "type": "function", "function": meta_fn_log_unknown_question | |
| }] | |
| system_prompt = f"You are acting as {name}. You are answering questions on {name}'s website, \ | |
| particularly questions related to {name}'s career, background, skills and experience. \ | |
| Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \ | |
| You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \ | |
| Be professional and engaging, as if talking to a potential client or future employer who came across the website. \ | |
| 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 \ | |
| only if email address is provided and it is NOT blank. \ | |
| 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. \ | |
| " | |
| system_prompt += f"\n\n## Summary:\n{summary}\n\n## LinkedIn Profile:\n{linkedin}\n\n" | |
| system_prompt += f"With this context, please chat with the user, always staying in character as {name}." | |
| demo = gr.ChatInterface(chat, type="messages") | |
| demo.launch() | |