forked from dhruvnoobpromax/A7-Starter-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_call.py
49 lines (37 loc) · 1.16 KB
/
api_call.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
import openai
import sys
if __name__ == '__main__':
# python3 <filename> API_KEY num_paragraphs query.txt
if len(sys.argv) < 4:
print("Usage: python3 api_call.py API_KEY num_paragraphs query.txt")
sys.exit(1)
# Read the API key from the command line
openai.api_key = sys.argv[1]
num_paragraphs = int(sys.argv[2])
print(num_paragraphs)
# Read the paragraphs from the files
paragraphs = []
for i in range(num_paragraphs):
filename = 'paragraph_' + str(i) + '.txt'
print(filename)
with open(filename, 'r') as f:
paragraphs.append(f.read())
paragraphs.append('\n')
# add query
query_file = sys.argv[3]
with open(query_file, 'r') as f:
query = f.read()
paragraphs.append(query)
paragraphs.append('\n')
# convert paragraphs to a single string
paragraphs = '\n'.join(paragraphs)
print(paragraphs)
query = {
"role": "user", "content": paragraphs
}
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[query]
)
reply = chat.choices[0].message.content
print(reply)