Skip to content

Commit

Permalink
Upgrade the request body field Function, use the Tool field for funct…
Browse files Browse the repository at this point in the history
…ion calls.
  • Loading branch information
yym68686 committed Aug 24, 2024
1 parent bcf9434 commit 0140d23
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
22 changes: 21 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class Tool(BaseModel):
type: str
function: Function

class FunctionCall(BaseModel):
name: str
arguments: str

class ToolCall(BaseModel):
id: str
type: str
function: FunctionCall

class ImageUrl(BaseModel):
url: str

Expand All @@ -29,7 +38,18 @@ class Message(BaseModel):
role: str
name: Optional[str] = None
arguments: Optional[str] = None
content: Union[str, List[ContentItem]]
content: Optional[Union[str, List[ContentItem]]] = None
tool_calls: Optional[List[ToolCall]] = None

class Message(BaseModel):
role: str
name: Optional[str] = None
content: Optional[Union[str, List[ContentItem]]] = None
tool_calls: Optional[List[ToolCall]] = None
tool_call_id: Optional[str] = None

class Config:
extra = "allow" # 允许额外的字段

class RequestModel(BaseModel):
model: str
Expand Down
23 changes: 19 additions & 4 deletions request.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ async def get_gpt_payload(request, engine, provider):

messages = []
for msg in request.messages:
name = None
tool_calls = None
tool_call_id = None
if isinstance(msg.content, list):
content = []
for item in msg.content:
Expand All @@ -138,9 +139,23 @@ async def get_gpt_payload(request, engine, provider):
content.append(image_message)
else:
content = msg.content
name = msg.name
if name:
messages.append({"role": msg.role, "name": name, "content": content})
tool_calls = msg.tool_calls
tool_call_id = msg.tool_call_id

if tool_calls:
tool_calls_list = []
for tool_call in tool_calls:
tool_calls_list.append({
"id": tool_call.id,
"type": tool_call.type,
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments
}
})
messages.append({"role": msg.role, "tool_calls": tool_calls_list})
elif tool_call_id:
messages.append({"role": msg.role, "tool_call_id": tool_call_id, "content": content})
else:
messages.append({"role": msg.role, "content": content})

Expand Down
1 change: 0 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def update_config(config_data):
model_dict[model] = model
if type(model) == dict:
model_dict.update({new: old for old, new in model.items()})
# model_dict.update({old: old for old, new in model.items()})
provider['model'] = model_dict
config_data['providers'][index] = provider
api_keys_db = config_data['api_keys']
Expand Down

0 comments on commit 0140d23

Please sign in to comment.