-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenai.py
222 lines (191 loc) · 9.74 KB
/
openai.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from openai import OpenAI, AsyncOpenAI
from typing import Dict, Any, List, cast
from openai.types.completion_usage import CompletionUsage
from openai.types.chat.chat_completion import Choice, ChatCompletion
from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageToolCall, ChatCompletionChunk
from openai.types.chat.chat_completion_message_tool_call import Function
from openai.resources.chat.completions import Completions, AsyncCompletions
from openai._streaming import Stream, AsyncStream
import json
import re
class CustomLLMResponseAdapter:
arg_pattern = re.compile(r'(\w+)=((?:\'(?:[^\']|\'\')*?\'|"(?:[^"]|"")*?"|\S+?)(?:,|$))')
@classmethod
def adapt_response(cls, response: str, completion_kwargs: Dict[str, Any] = {}) -> ChatCompletion:
def parse_function_args(function_args_str: str) -> Dict[str, Any]:
function_args = {}
matches = cls.arg_pattern.findall(function_args_str)
for match in matches:
arg_name, arg_value = match
arg_value = arg_value.strip("'\",")
arg_value = arg_value.replace("''", "'").replace('""', '"')
try:
parsed_value = json.loads(arg_value)
function_args[arg_name] = parsed_value
except json.JSONDecodeError:
function_args[arg_name] = arg_value
return function_args
completion_kwargs = completion_kwargs or {}
function_calls: List[ChatCompletionMessageToolCall] = []
if "<<function>>" in response:
function_parts = response.split("<<function>>")
for part in function_parts[1:]:
if "(" in part:
function_name, function_args_str = part.split("(", 1)
function_args_str = function_args_str.rstrip(")")
function_args = parse_function_args(function_args_str)
function_calls.append(ChatCompletionMessageToolCall(
id=completion_kwargs.get("tool_call_id", "1"),
type="function",
function=Function(
name=function_name.strip(),
arguments= json.dumps(function_args)
)
))
usage = CompletionUsage(
prompt_tokens=completion_kwargs.get("usage", {}).get("prompt_tokens", 0),
completion_tokens=completion_kwargs.get("usage", {}).get("completion_tokens", 0),
total_tokens=completion_kwargs.get("usage", {}).get("total_tokens", 0)
)
if len(function_calls) > 0:
return ChatCompletion(
id=completion_kwargs.get("id", "chatcmpl-default-id"),
object="chat.completion",
created=completion_kwargs.get("created", 0),
model=completion_kwargs.get("model", "default-model"),
choices=[
Choice(
finish_reason="tool_calls",
index=0,
logprobs=None,
message=ChatCompletionMessage(
role="assistant",
content="",
function_call=None,
tool_calls=function_calls
)
)
],
usage=usage
)
else:
return ChatCompletion(
id=completion_kwargs.get("id", "chatcmpl-default-id"),
object="chat.completion",
created=completion_kwargs.get("created", 0),
model=completion_kwargs.get("model", "default-model"),
choices=[
Choice(
finish_reason=completion_kwargs.get("finish_reason", "stop"),
index=0,
logprobs=None,
message=ChatCompletionMessage(
role="assistant",
content=response,
function_call=None,
tool_calls=function_calls
)
)
],
usage=usage
)
class CustomChatCompletions:
def __init__(self, completions:Completions, debug:bool):
self._original_completions:Completions = completions
self._debug = debug
def create(self, *args, **kwargs) -> ChatCompletion | Stream[ChatCompletionChunk]:
messages = kwargs.get("messages", None)
if messages is None:
for arg in args:
if isinstance(arg, list) and len(arg) > 0 and isinstance(arg[0], dict) and "role" in arg[0]:
messages = arg
break
tools = kwargs.get("tools", None)
if tools is None:
for arg in args:
if isinstance(arg, list) and len(arg) > 0 and isinstance(arg[0], dict) and "type" in arg[0]:
tools = arg
break
# check for stream or not
stream = kwargs.get('stream', True)
if stream and tools:
raise(Exception("Stream and function calling is not yet supported."))
if not stream and tools:
print('warning: we do not collect token generation metrics here CustomChatCompletions')
# TODO we do not collect token generation metrics here
if messages is not None and tools is not None:
functions_string = json.dumps(tools)
updated_messages = self.insert_function_and_question(messages, functions_string)
args = tuple(updated_messages if arg is messages else arg for arg in args)
kwargs["messages"] = updated_messages
if self._debug: print(f'sending to llm: {updated_messages}')
response = self._original_completions.create(*args, **kwargs)
adapted_response = CustomLLMResponseAdapter.adapt_response(cast(str, response.choices[0].message.content)) # return a new ChatCompletion with function callings inside
if self._debug: print(f'generated by llm: {adapted_response}')
return adapted_response
else:
return self._original_completions.create(*args, **kwargs)
@staticmethod
def insert_function_and_question(messages, functions_string):
user_message = None
for message in reversed(messages):
if message["role"] == "user":
user_message = message
break
if user_message:
user_message["content"] = f"<<function>>{functions_string}\n<<question>>{user_message['content']}"
return messages
class CustomOpenAIClient(OpenAI):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.chat.completions = cast(Completions, CustomChatCompletions(self.chat.completions, debug= False)) # type: ignore
class AsyncCustomChatCompletions:
def __init__(self, completions:AsyncCompletions, debug:bool):
self._original_completions:AsyncCompletions = completions
self._debug = debug
async def create(self, *args, **kwargs) -> ChatCompletion | AsyncStream[ChatCompletionChunk]:
messages = kwargs.get("messages", None)
if messages is None:
for arg in args:
if isinstance(arg, list) and len(arg) > 0 and isinstance(arg[0], dict) and "role" in arg[0]:
messages = arg
break
tools = kwargs.get("tools", None)
if tools is None:
for arg in args:
if isinstance(arg, list) and len(arg) > 0 and isinstance(arg[0], dict) and "type" in arg[0]:
tools = arg
break
# check for stream or not
stream = kwargs.get('stream', True)
if stream and tools:
raise(Exception("Stream and function calling is not yet supported."))
if not stream and tools:
# TODO we do not collect token generation metrics here
print('warning: we do not collect token generation metrics here AsyncCustomChatCompletions')
if messages is not None and tools is not None:
functions_string = json.dumps(tools)
updated_messages = self.insert_function_and_question(messages, functions_string)
args = tuple(updated_messages if arg is messages else arg for arg in args)
kwargs["messages"] = updated_messages
if self._debug: print(f'sending to llm: {updated_messages}')
response = await self._original_completions.create(*args, **kwargs)
adapted_response = CustomLLMResponseAdapter.adapt_response(cast(str, response.choices[0].message.content)) # return a new ChatCompletion with function callings inside
if self._debug: print(f'generated by llm: {adapted_response}')
return adapted_response
else:
return await self._original_completions.create(*args, **kwargs)
@staticmethod
def insert_function_and_question(messages, functions_string):
user_message = None
for message in reversed(messages):
if message["role"] == "user":
user_message = message
break
if user_message:
user_message["content"] = f"<<function>>{functions_string}\n<<question>>{user_message['content']}"
return messages
class AsyncCustomOpenAIClient(AsyncOpenAI):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.chat.completions = cast(AsyncCompletions, AsyncCustomChatCompletions(self.chat.completions, debug= False)) # type: ignore