yym68686 commited on
Commit
0140d23
·
1 Parent(s): bcf9434

Upgrade the request body field Function, use the Tool field for function calls.

Browse files
Files changed (3) hide show
  1. models.py +21 -1
  2. request.py +19 -4
  3. utils.py +0 -1
models.py CHANGED
@@ -17,6 +17,15 @@ class Tool(BaseModel):
17
  type: str
18
  function: Function
19
 
 
 
 
 
 
 
 
 
 
20
  class ImageUrl(BaseModel):
21
  url: str
22
 
@@ -29,7 +38,18 @@ class Message(BaseModel):
29
  role: str
30
  name: Optional[str] = None
31
  arguments: Optional[str] = None
32
- content: Union[str, List[ContentItem]]
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  class RequestModel(BaseModel):
35
  model: str
 
17
  type: str
18
  function: Function
19
 
20
+ class FunctionCall(BaseModel):
21
+ name: str
22
+ arguments: str
23
+
24
+ class ToolCall(BaseModel):
25
+ id: str
26
+ type: str
27
+ function: FunctionCall
28
+
29
  class ImageUrl(BaseModel):
30
  url: str
31
 
 
38
  role: str
39
  name: Optional[str] = None
40
  arguments: Optional[str] = None
41
+ content: Optional[Union[str, List[ContentItem]]] = None
42
+ tool_calls: Optional[List[ToolCall]] = None
43
+
44
+ class Message(BaseModel):
45
+ role: str
46
+ name: Optional[str] = None
47
+ content: Optional[Union[str, List[ContentItem]]] = None
48
+ tool_calls: Optional[List[ToolCall]] = None
49
+ tool_call_id: Optional[str] = None
50
+
51
+ class Config:
52
+ extra = "allow" # 允许额外的字段
53
 
54
  class RequestModel(BaseModel):
55
  model: str
request.py CHANGED
@@ -126,7 +126,8 @@ async def get_gpt_payload(request, engine, provider):
126
 
127
  messages = []
128
  for msg in request.messages:
129
- name = None
 
130
  if isinstance(msg.content, list):
131
  content = []
132
  for item in msg.content:
@@ -138,9 +139,23 @@ async def get_gpt_payload(request, engine, provider):
138
  content.append(image_message)
139
  else:
140
  content = msg.content
141
- name = msg.name
142
- if name:
143
- messages.append({"role": msg.role, "name": name, "content": content})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  else:
145
  messages.append({"role": msg.role, "content": content})
146
 
 
126
 
127
  messages = []
128
  for msg in request.messages:
129
+ tool_calls = None
130
+ tool_call_id = None
131
  if isinstance(msg.content, list):
132
  content = []
133
  for item in msg.content:
 
139
  content.append(image_message)
140
  else:
141
  content = msg.content
142
+ tool_calls = msg.tool_calls
143
+ tool_call_id = msg.tool_call_id
144
+
145
+ if tool_calls:
146
+ tool_calls_list = []
147
+ for tool_call in tool_calls:
148
+ tool_calls_list.append({
149
+ "id": tool_call.id,
150
+ "type": tool_call.type,
151
+ "function": {
152
+ "name": tool_call.function.name,
153
+ "arguments": tool_call.function.arguments
154
+ }
155
+ })
156
+ messages.append({"role": msg.role, "tool_calls": tool_calls_list})
157
+ elif tool_call_id:
158
+ messages.append({"role": msg.role, "tool_call_id": tool_call_id, "content": content})
159
  else:
160
  messages.append({"role": msg.role, "content": content})
161
 
utils.py CHANGED
@@ -12,7 +12,6 @@ def update_config(config_data):
12
  model_dict[model] = model
13
  if type(model) == dict:
14
  model_dict.update({new: old for old, new in model.items()})
15
- # model_dict.update({old: old for old, new in model.items()})
16
  provider['model'] = model_dict
17
  config_data['providers'][index] = provider
18
  api_keys_db = config_data['api_keys']
 
12
  model_dict[model] = model
13
  if type(model) == dict:
14
  model_dict.update({new: old for old, new in model.items()})
 
15
  provider['model'] = model_dict
16
  config_data['providers'][index] = provider
17
  api_keys_db = config_data['api_keys']