-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPT_api.py
55 lines (47 loc) · 1.69 KB
/
ChatGPT_api.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
import os
import openai
import traceback
class LLM:
def __init__(self,
api_key='',
api_base=None,
model='gpt-3.5-turbo',
temperature=0.,
max_tokens=1000,
top_p=2,
frequency_penalty=1.2,
) -> None:
openai.api_key = api_key
openai.api_base = api_base
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
self.frequency_penalty = frequency_penalty
self.top_p = top_p
def query(self, messages, tools=None, tool_choice='auto'):
try:
if tools is None:
# regular call
response = openai.ChatCompletion.create(
model=self.model,
messages=messages,
max_tokens=self.max_tokens,
temperature=self.temperature,
top_p=self.top_p,
frequency_penalty=self.frequency_penalty
)
else: # call with tools
response = openai.ChatCompletion.create(
model=self.model,
messages=messages,
max_tokens=self.max_tokens,
temperature=self.temperature,
top_p=self.top_p,
frequency_penalty=self.frequency_penalty,
functions=tools,
functionCall=tool_choice
)
except Exception as e:
traceback.print_exc()
return str(e)
return response