-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt-old.py
85 lines (74 loc) · 3.46 KB
/
chatgpt-old.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
import openai
import json
from config import *
openai.api_key = getOpenaiKey()
# 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 valid SPARQL query to search a knowledge graph of historical listening "
"experiences that answers the question. Prioritize the most important keywords and "
"add synonyms to focus the search. Ensure that the response contain only the query and no other extra text. "
"A search for the term 'childhood' is shown below. Use this as a template for the query:"
"""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX led: <http://led.kmi.open.ac.uk/term/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX pl: <http://purl.org/NET/c4dm/event.owl#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?s ?p ?o
FROM <http://data.open.ac.uk/context/led>
WHERE {
?o bds:search "childhood" .
?s rdf:value ?o
}
"""},
{"role": "user", "content": f"{input_request}"},
# {"role": "user", "content": "Show me children's experiences of listening to music"}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
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 the "
"question based solely on the search results given. 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."},
{"role": "user", "content": f"{input_request}"},
# {"role": "assistant", "content": f"{json.dumps(search_results)}"}
{"role": "assistant", "content": f"{search_results}"}
]
print(messages)
finalResponse = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
# engine=engine
)
return finalResponse["choices"][0]["message"]["content"]
def keywordExpansion (inputTerms):
messages = [
# {"role": "system", "content": "You are a helpful search assistant that can provide information."},
{"role": "user", "content": "Expand each of the terms listed below into synonyms "
"and related terms. Only use single words for the "
"related terms or synonyms."},
{"role": "user", "content": f"{inputTerms}"},
]
finalResponse = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
# engine=engine
)
return finalResponse["choices"][0]["message"]["content"]