-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI_openai.py
More file actions
128 lines (105 loc) · 4.94 KB
/
API_openai.py
File metadata and controls
128 lines (105 loc) · 4.94 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import openai
import requests
import json
def load_agent(filepath):
with open(filepath, 'r') as file:
return json.load(file)
# Open AI API request
def openai_api_request(model="gpt-4o",
messages=None,
temperature=1,
top_p=1,
n=1,
presence_penalty=0,
frequency_penalty=0,
logprobs=True,
max_completion_tokens=300):
url = "https://api.openai.com/v1/chat/completions"
api_key = os.getenv("OPENAI_API_KEY")
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {api_key}",
}
data = {
"model": model,
"messages": messages,
"temperature": temperature,
"top_p": top_p,
"n": n,
"presence_penalty": presence_penalty,
"frequency_penalty": frequency_penalty,
"logprobs": logprobs,
"max_tokens": max_completion_tokens
}
response = requests.post(url, headers=headers, json=data)
response_json = response.json()
# Extract token usage and logprob information
token_usage = response_json.get('usage', {})
prompt_tokens = token_usage.get('prompt_tokens', 0)
completion_tokens = token_usage.get('completion_tokens', 0)
total_tokens = token_usage.get('total_tokens', 0)
# Extract the logprobs of each token
choices = response_json.get('choices', [])
if choices:
logprobs_list = [logprob['logprob'] for logprob in choices[0].get('logprobs', {}).get('content', [])]
else:
logprobs_list = []
# Debugging information
if not logprobs_list:
print("Logprobs are empty. Response JSON:", response_json)
return response_json, prompt_tokens, completion_tokens, total_tokens, logprobs_list
class API_Call_openai():
def __init__(self, agent=None):
openai.api_key = os.getenv("OPENAI_API_KEY")
if agent is None:
self.agent_data = load_agent("agents/default.json")
else:
self.agent_data = load_agent(f"agents/{agent}.json")
def update_agent(self, filename):
self.agent_data = load_agent(filename)
def thinkAbout(self, message, conversation, model=None, debug=False):
model = self.agent_data.get("model", "gpt-4o")
if model is None:
model = self.agent_data.get("model", "gpt-4o")
if not any(msg["role"] == "system" for msg in conversation):
system_prompt = self.agent_data.get("PrePrompt", "")
conversation.insert(0, {"role": "system", "content": system_prompt})
FormattedMessage = {"role": "user", "content": message}
conversation.append(FormattedMessage)
try:
response, prompt_tokens, completion_tokens, total_tokens, logprobs_list = openai_api_request(
model=model,
messages=conversation,
temperature=self.agent_data["temperature"],
frequency_penalty=self.agent_data["frequency_penalty"],
presence_penalty=self.agent_data["presence_penalty"],
top_p=self.agent_data["top_p"],
max_completion_tokens=self.agent_data["max_completion_tokens"]
)
except:
try:
response, prompt_tokens, completion_tokens, total_tokens, logprobs_list = openai_api_request(
model="gpt-4o",
messages=conversation,
temperature=self.agent_data["temperature"],
frequency_penalty=self.agent_data["frequency_penalty"],
presence_penalty=self.agent_data["presence_penalty"],
top_p=self.agent_data["top_p"],
max_completion_tokens=300
)
response['choices'][0]['message']['content'] += "(Generated by gpt-4o as you do not have access to other assigned model)"
except openai.error.AuthenticationError as e:
response = {'choices': [{'message': {'content': e.__str__()}}], 'usage': {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0}}
prompt_tokens, completion_tokens, total_tokens, logprobs_list = 0, 0, 0, []
if "error" in response.keys():
conversation.append({"role": "assistant", "content": response["error"]["message"]})
return conversation, 0, 0, 0, [] # Ensure it returns five values
conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
if debug:
with open("logs.txt", "w", encoding="utf-8") as file:
for i in conversation:
file.write(str(i) + "\n")
if not logprobs_list:
print("Logprobs are empty. Response JSON:", response)
return conversation, prompt_tokens, completion_tokens, total_tokens, logprobs_list