-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathassistant.py
185 lines (143 loc) · 6.03 KB
/
assistant.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import time
import json
from openai import OpenAI
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer
ELASTIC_URL = os.getenv("ELASTIC_URL", "http://elasticsearch:9200")
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://ollama:11434/v1/")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "your-api-key-here")
es_client = Elasticsearch(ELASTIC_URL)
ollama_client = OpenAI(base_url=OLLAMA_URL, api_key="ollama")
openai_client = OpenAI(api_key=OPENAI_API_KEY)
model = SentenceTransformer("multi-qa-MiniLM-L6-cos-v1")
def elastic_search_text(query, course, index_name="course-questions"):
search_query = {
"size": 5,
"query": {
"bool": {
"must": {
"multi_match": {
"query": query,
"fields": ["question^3", "text", "section"],
"type": "best_fields",
}
},
"filter": {"term": {"course": course}},
}
},
}
response = es_client.search(index=index_name, body=search_query)
return [hit["_source"] for hit in response["hits"]["hits"]]
def elastic_search_knn(field, vector, course, index_name="course-questions"):
knn = {
"field": field,
"query_vector": vector,
"k": 5,
"num_candidates": 10000,
"filter": {"term": {"course": course}},
}
search_query = {
"knn": knn,
"_source": ["text", "section", "question", "course", "id"],
}
es_results = es_client.search(index=index_name, body=search_query)
return [hit["_source"] for hit in es_results["hits"]["hits"]]
def build_prompt(query, search_results):
prompt_template = """
You're a course teaching assistant. Answer the QUESTION based on the CONTEXT from the FAQ database.
Use only the facts from the CONTEXT when answering the QUESTION.
QUESTION: {question}
CONTEXT:
{context}
""".strip()
context = "\n\n".join(
[
f"section: {doc['section']}\nquestion: {doc['question']}\nanswer: {doc['text']}"
for doc in search_results
]
)
return prompt_template.format(question=query, context=context).strip()
def llm(prompt, model_choice):
start_time = time.time()
if model_choice.startswith('ollama/'):
response = ollama_client.chat.completions.create(
model=model_choice.split('/')[-1],
messages=[{"role": "user", "content": prompt}]
)
answer = response.choices[0].message.content
tokens = {
'prompt_tokens': response.usage.prompt_tokens,
'completion_tokens': response.usage.completion_tokens,
'total_tokens': response.usage.total_tokens
}
elif model_choice.startswith('openai/'):
response = openai_client.chat.completions.create(
model=model_choice.split('/')[-1],
messages=[{"role": "user", "content": prompt}]
)
answer = response.choices[0].message.content
tokens = {
'prompt_tokens': response.usage.prompt_tokens,
'completion_tokens': response.usage.completion_tokens,
'total_tokens': response.usage.total_tokens
}
else:
raise ValueError(f"Unknown model choice: {model_choice}")
end_time = time.time()
response_time = end_time - start_time
return answer, tokens, response_time
def evaluate_relevance(question, answer):
prompt_template = """
You are an expert evaluator for a Retrieval-Augmented Generation (RAG) system.
Your task is to analyze the relevance of the generated answer to the given question.
Based on the relevance of the generated answer, you will classify it
as "NON_RELEVANT", "PARTLY_RELEVANT", or "RELEVANT".
Here is the data for evaluation:
Question: {question}
Generated Answer: {answer}
Please analyze the content and context of the generated answer in relation to the question
and provide your evaluation in parsable JSON without using code blocks:
{{
"Relevance": "NON_RELEVANT" | "PARTLY_RELEVANT" | "RELEVANT",
"Explanation": "[Provide a brief explanation for your evaluation]"
}}
""".strip()
prompt = prompt_template.format(question=question, answer=answer)
evaluation, tokens, _ = llm(prompt, 'openai/gpt-4o-mini')
try:
json_eval = json.loads(evaluation)
return json_eval['Relevance'], json_eval['Explanation'], tokens
except json.JSONDecodeError:
return "UNKNOWN", "Failed to parse evaluation", tokens
def calculate_openai_cost(model_choice, tokens):
openai_cost = 0
if model_choice == 'openai/gpt-3.5-turbo':
openai_cost = (tokens['prompt_tokens'] * 0.0015 + tokens['completion_tokens'] * 0.002) / 1000
elif model_choice in ['openai/gpt-4o', 'openai/gpt-4o-mini']:
openai_cost = (tokens['prompt_tokens'] * 0.03 + tokens['completion_tokens'] * 0.06) / 1000
return openai_cost
def get_answer(query, course, model_choice, search_type):
if search_type == 'Vector':
vector = model.encode(query)
search_results = elastic_search_knn('question_text_vector', vector, course)
else:
search_results = elastic_search_text(query, course)
prompt = build_prompt(query, search_results)
answer, tokens, response_time = llm(prompt, model_choice)
relevance, explanation, eval_tokens = evaluate_relevance(query, answer)
openai_cost = calculate_openai_cost(model_choice, tokens)
return {
'answer': answer,
'response_time': response_time,
'relevance': relevance,
'relevance_explanation': explanation,
'model_used': model_choice,
'prompt_tokens': tokens['prompt_tokens'],
'completion_tokens': tokens['completion_tokens'],
'total_tokens': tokens['total_tokens'],
'eval_prompt_tokens': eval_tokens['prompt_tokens'],
'eval_completion_tokens': eval_tokens['completion_tokens'],
'eval_total_tokens': eval_tokens['total_tokens'],
'openai_cost': openai_cost
}