Skip to content

Commit

Permalink
fix: fix sparkdesk and skylark function calling pointer is nil fatal (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 11, 2024
1 parent e8d5c35 commit 5adc0fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
33 changes: 20 additions & 13 deletions adapter/skylark/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,33 @@ func (c *ChatInstance) CreateRequest(props *ChatProps) *api.ChatReq {
}
}

func getToolCalls(choice *api.ChatResp) *globals.ToolCalls {
calls := choice.Choice.Message.FunctionCall
if calls == nil {
return nil
}

return &globals.ToolCalls{
globals.ToolCall{
Type: "function",
Id: fmt.Sprintf("%s-%s", calls.Name, choice.ReqId),
Function: globals.ToolCallFunction{
Name: calls.Name,
Arguments: calls.Arguments,
},
},
}
}

func getChoice(choice *api.ChatResp) *globals.Chunk {
if choice == nil || choice.Choice == nil || choice.Choice.Message == nil {
return &globals.Chunk{Content: ""}
}

message := choice.Choice.Message

calls := message.FunctionCall
return &globals.Chunk{
Content: message.Content,
ToolCall: utils.Multi(calls != nil, &globals.ToolCalls{
globals.ToolCall{
Type: "function",
Id: fmt.Sprintf("%s-%s", calls.Name, choice.ReqId),
Function: globals.ToolCallFunction{
Name: calls.Name,
Arguments: calls.Arguments,
},
},
}, nil),
Content: message.Content,
ToolCall: getToolCalls(choice),
}
}

Expand Down
30 changes: 19 additions & 11 deletions adapter/sparkdesk/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ func (c *ChatInstance) GetFunctionCalling(props *ChatProps) *FunctionsPayload {
}
}

func getFunctionCall(call *FunctionCall) *globals.ToolCalls {
if call == nil {
return nil
}

return &globals.ToolCalls{
globals.ToolCall{
Type: "function",
Id: fmt.Sprintf("%s-%s", call.Name, call.Arguments),
Function: globals.ToolCallFunction{
Name: call.Name,
Arguments: call.Arguments,
},
},
}
}

func getChoice(form *ChatResponse) *globals.Chunk {
if form == nil || len(form.Payload.Choices.Text) == 0 {
return &globals.Chunk{Content: ""}
Expand All @@ -80,17 +97,8 @@ func getChoice(form *ChatResponse) *globals.Chunk {
choice := choices[0]

return &globals.Chunk{
Content: choice.Content,
ToolCall: utils.Multi(choice.FunctionCall != nil, &globals.ToolCalls{
globals.ToolCall{
Type: "function",
Id: fmt.Sprintf("%s-%s", choice.FunctionCall.Name, choice.FunctionCall.Arguments),
Function: globals.ToolCallFunction{
Name: choice.FunctionCall.Name,
Arguments: choice.FunctionCall.Arguments,
},
},
}, nil),
Content: choice.Content,
ToolCall: getFunctionCall(choice.FunctionCall),
}
}

Expand Down

0 comments on commit 5adc0fd

Please sign in to comment.