-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_tickets.py
More file actions
80 lines (66 loc) · 2.57 KB
/
evaluate_tickets.py
File metadata and controls
80 lines (66 loc) · 2.57 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
import os
import time
import pandas as pd
from dotenv import load_dotenv
from openai import OpenAI, OpenAIError
load_dotenv()
client = OpenAI()
# Construye el prompt
def build_prompt(ticket, reply):
return f"""
Evaluate the following AI-generated reply to a customer support ticket.
Ticket:
\"\"\"{ticket}\"\"\"
Reply:
\"\"\"{reply}\"\"\"
Please provide:
1. A content_score from 1 to 5 (based on relevance, correctness, and completeness).
2. A short content_explanation.
3. A format_score from 1 to 5 (based on clarity, structure, and grammar).
4. A short format_explanation.
Use the format:
Content Score: <score>
Content Explanation: <short explanation>
Format Score: <score>
Format Explanation: <short explanation>
"""
# Analiza el texto devuelto por el modelo
def parse_response(response_text):
lines = response_text.strip().split("\n")
try:
content_score = int([l for l in lines if "Content Score:" in l][0].split(":")[1].strip())
content_expl = [l for l in lines if "Content Explanation:" in l][0].split(":", 1)[1].strip()
format_score = int([l for l in lines if "Format Score:" in l][0].split(":")[1].strip())
format_expl = [l for l in lines if "Format Explanation:" in l][0].split(":", 1)[1].strip()
return content_score, content_expl, format_score, format_expl
except Exception as e:
print("Error parsing response:\n", response_text)
return None, "Parse error", None, "Parse error"
# Llama al modelo GPT
def evaluate_ticket(ticket, reply):
prompt = build_prompt(ticket, reply)
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0.3,
)
return parse_response(response.choices[0].message.content)
except OpenAIError as e:
print(f"API error: {e}")
return None, "API error", None, "API error"
# Ejecución principal
def main():
df = pd.read_csv("tickets.csv")
results = []
for i, row in df.iterrows():
print(f"Evaluating ticket {i + 1}/{len(df)}")
ticket, reply = row["ticket"], row["reply"]
content_score, content_expl, format_score, format_expl = evaluate_ticket(ticket, reply)
results.append((content_score, content_expl, format_score, format_expl))
time.sleep(1)
df[["content_score", "content_explanation", "format_score", "format_explanation"]] = pd.DataFrame(results)
df.to_csv("tickets_evaluated.csv", index=False)
print("Done. Output saved to tickets_evaluated.csv")
if __name__ == "__main__":
main()