-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
29 lines (27 loc) · 1.04 KB
/
generator.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
import requests
class Generator:
def __init__(self, model, api_key, temperature=0.7, max_tokens=1536):
self.model = model
self.api_key = api_key
self.temperature = temperature
self.max_tokens = max_tokens
def generate(self, direction, summary):
url = 'https://api.openai.com/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + self.api_key
}
data = {
'model': self.model,
'messages': [{"role": "system", "content": direction }] + summary,
'max_tokens': self.max_tokens,
'temperature': self.temperature
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
completions = response.json()['choices']
message = completions[0]['message']['content'].strip()
return message
else:
print('Error generating text:', response.text)
return None