-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (62 loc) · 2.14 KB
/
main.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
import os
from dotenv import load_dotenv
import openai
import requests
import json
from tableau.tableau_integration import TEST
load_dotenv()
openai.api_key = os.getenv('OPEN_AI_KEY')
# OPENAI_KEY = os.getenv('OPEN_AI_KEY')
# r = requests.get('https://api.openai.com/v1/engines', auth=f'Bearer {OPENAI_KEY}')
api_url = 'https://api.openai.com/v1/engines/davinci-codex/completions'
ENGINES_ENDPOINT = 'https://api.openai.com/v1/engines'
MODELS_ENDPOINT = 'https://api.openai.com/v1/models'
def get_models():
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai.api_key}'
}
response = requests.get(MODELS_ENDPOINT, headers=headers)
return response.json()
def get_engines():
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai.api_key}'
}
response = requests.get(ENGINES_ENDPOINT, headers=headers)
return response.json()
def generate_chat_response(prompt):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai.api_key}'
}
response = openai.Completion.create(model="text-davinci-003",
prompt=prompt,
max_tokens=50, # = length of the answer
temperature=1
)
return response
# data = {
# 'prompt': prompt,
# 'model': 'gpt-3.5-turbo',
# # 'content': 'Say this is a test!',
# 'max_tokens': 100,
# 'temperature': 0.7,
# 'n': 1,
# 'stop': None,
# }
# response = requests.post(api_url, headers=headers, data=json.dumps(data))
# print(response.content)
# response_json = response.json()
# return response_json['choices'][0]['text'].strip()
if __name__ == '__main__':
print(TEST)
print('Enter a prompt:')
prompt = input()
print(f'the prompt is "{prompt}"')
res = generate_chat_response(prompt)
with open('gpt_responses.txt', 'w') as f:
f.write(f'{res}')
print(res)
# print(get_models())
# print(get_engines())