-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat.py
59 lines (51 loc) · 2.16 KB
/
chat.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
from openai import OpenAI
import anthropic
import json
class chat:
system_message = "You can answer questions based on provided source material. Given a question and text from one or more official sources you answer the question using the source information in a readable, clear, concise and accurate way. Answer the question in paragraph form."
def __init__(self, llm_provider):
self.llm_provider = llm_provider
self.client = None
if llm_provider == "anthropic":
self.client = anthropic.Anthropic()
elif llm_provider == "openai":
client = OpenAI()
else:
raise ValueError()
def create_response(self, query, path, source_titles):
response = None
sources_text = ""
for index, source_title in enumerate(source_titles):
f = open(path + "/" + source_title, "r")
text = f.read()
sources_text = sources_text + f"source #{index}: {text}\n\n"
if self.llm_provider == "anthropic":
message = self.client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=500,
temperature=0.3,
system=chat.system_message,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"question: {query}, source: {sources_text}"
}
]
}
]
)
response = message.content[0].text
elif self.llm_provider == "openai":
res = self.client.chat.completions.create(
model="gpt-4o-mini",
response_format={ "type": "json_object" },
messages=[
{"role": "system", "content": chat.system_message},
{"role": "user", "content":f"question: {query}, source: {text}"}
]
)
response = res.choices[0].message.content
return response