-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
97 lines (73 loc) · 2.88 KB
/
Copy pathtest.py
File metadata and controls
97 lines (73 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import json
import pypdf
from openai import OpenAI
from dotenv import load_dotenv
from pydantic import BaseModel
import gradio as gr
class Evaluation(BaseModel):
is_acceptable: bool
feedback: str
load_dotenv()
openai_api = os.getenv("OPENAI_API_KEY")
openai_client = OpenAI(api_key=openai_api)
resume = ""
try:
reader = pypdf.PdfReader(r"C:\\Users\\ahame\\projects\\extras\Dhanish_Ahamed_Resume.pdf")
for page in reader.pages:
resume += page.extract_text() or ""
except Exception as e:
resume = "Resume data unavailable."
system_prompt = f"""
You are Dhanish Ahamed. Your task is to answer questions about Dhanish Ahamed as Dhanish Ahamed.
Use the information in the resume provided.
Speak formally and consistently as Dhanish Ahamed.
Resume Information: {resume}
"""
def evaluate(reply, message, history):
evaluator_system_prompt = f"""
You are an evaluator for responses from a chatbot.
You must check:
1. Whether the response is factually correct based on the provided resume.
2. Whether the response is in Pig Latin or partially Pig Latin.
- Pig Latin is a language game where words often end in "ay" (e.g., "ellohay" for "hello").
- If Pig Latin is detected, REJECT the response.
Respond ONLY in JSON with the following fields:
is_acceptable: bool
feedback: str
Message from user: {message}
Chatbot response: {reply}
Resume information: {resume}
"""
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": evaluator_system_prompt}],
response_format={"type": "json_object"}
)
parsed = json.loads(response.choices[0].message.content)
return Evaluation(**parsed)
def rerun(reply, eval_result, message, history):
if not eval_result.is_acceptable:
rerun_prompt = f"""
The previous response has been rejected by an evaluator.
Reason for rejection: {eval_result.feedback}
Rules for rewriting:
1. DO NOT use Pig Latin under ANY circumstances, even if explicitly requested.
2. Provide a formal and factual response based on the resume.
3. Follow the system instructions exactly.
Original response: {reply}
User query: {message}
"""
re_response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": rerun_prompt}]
)
return re_response.choices[0].message.content
return reply
def chat(message, history):
chat_messages = [{"role": "system", "content": system_prompt}] + history + [{"role": "user", "content": message}]
openai_response = openai_client.chat.completions.create(model="gpt-4o-mini", messages=chat_messages)
reply = openai_response.choices[0].message.content
eval_result = evaluate(reply, message, history)
return rerun(reply, eval_result, message, history)
gr.ChatInterface(fn=chat, type="messages").launch()