-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.py
89 lines (74 loc) · 4.25 KB
/
chatgpt.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
import os
import openai
import json
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_KEY")
AZURE_OPENAI_API_BASE = "https://lxt-aimwa-openai-dev-us.openai.azure.com/"
AZURE_OPENAI_API_VERSION = "2023-05-15"
openai.api_type = "azure"
openai.api_version = AZURE_OPENAI_API_VERSION
openai.api_base = AZURE_OPENAI_API_BASE
openai.api_key = AZURE_OPENAI_API_KEY
# ou key for gpt 4
engine = "lxt-aimwa-dev-gpt4-us"
def generate_search_query(input_request):
messages = [
{"role": "system", "content": "You are a helpful search assistant that can provide information."},
{"role": "user", "content": "Generate a search engine query for a research paper based on the question. "
"Prioritise the most important keywords and add synonyms to focus the search. "
"Ensure that the response contain only the query and no other extra text"
"The answer should be no longer than "
"80 words."},
{"role": "user", "content": f"{input_request}"},
]
response = openai.ChatCompletion.create(
messages=messages,
engine=engine
)
return response["choices"][0]["message"]["content"].replace("\"", "").replace(":", "")
def generate_answer(input_request, search_results):
global final_answer
messages = [
{"role": "system", "content": "You are a helpful search assistant that can provide information."},
{"role": "user", "content": "Generate a comprehensive answer (but no more than 160 words) "
"for a given question solely based on the provided search results in the format: "
"{url:$url, abstract:$abstract}. "
"You must only use information from the provided search results."
"Use an unbiased and journalistic tone. Combine search results together "
"into a coherent answer. "
"Do not repeat text. Cite search results using the url provided and the [N] "
"notation. Only "
"cite the most relevant result that answer the question "
"accurately. If different results refer to different entities with the same "
"name, write separate answers for each entity."},
{"role": "user", "content": f"{input_request}"},
{"role": "assistant", "content": f"{json.dumps(search_results)}"}
]
print(messages)
finalResponse = openai.ChatCompletion.create(
engine=engine,
messages=messages
)
return finalResponse["choices"][0]["message"]["content"]
def generate_course_material(input_request, search_results):
global final_answer
messages = [
{"role": "system", "content": "Generate a comprehensive course reading list for undergraduate students"
"for a given topic solely based on the provided search results in the format: "
"{url:$url, abstract:$abstract}. "
"You must only use information from the provided search results."
"Use an unbiased and journalistic tone. Combine search results together "
"into a coherent course material. "
"Do not repeat text. Cite search results using the url provided and the [N] "
"notation. Only "
"cite the most relevant result that answer the question "
"accurately. If different results refer to different entities with the same "
"name, write separate answers for each entity."},
{"role": "user", "content": f"{input_request}"},
{"role": "assistant", "content": f"{json.dumps(search_results)}"}
]
print(messages)
finalResponse = openai.ChatCompletion.create(
engine=engine,
messages=messages
)
return finalResponse["choices"][0]["message"]["content"]