fix: fix tool calls required omitempty field

Co-Authored-By: Minghan Zhang <112773885+zmh-program@users.noreply.github.com>
This commit is contained in:
Deng Junhai 2024-06-22 02:36:00 +08:00
parent 485066feb2
commit 78ff6a19a4
3 changed files with 14 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package skylark
import (
"chat/globals"
"chat/utils"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/volcengine/volc-sdk-golang/service/maas/models/api"
)
@ -20,19 +21,21 @@ func getFunctionCall(calls *globals.ToolCalls) *api.FunctionCall {
}
func getType(p globals.ToolProperty) string {
if p.Type == nil {
t, ok := p["type"]
if !ok {
return "string"
}
return *p.Type
return t.(string)
}
func getDescription(p globals.ToolProperty) string {
if p.Description == nil {
desc, ok := p["description"]
if !ok {
return ""
}
return *p.Description
return desc.(string)
}
func getValue(p globals.ToolProperty) *structpb.Value {

View File

@ -16,7 +16,7 @@ type ToolFunction struct {
type ToolParameters struct {
Type string `json:"type"`
Properties ToolProperties `json:"properties"`
Required []string `json:"required"`
Required *[]string `json:"required,omitempty"`
}
type ToolProperties map[string]ToolProperty
@ -25,7 +25,8 @@ type ToolProperties map[string]ToolProperty
type JsonSchemaType any
type JSONSchemaDefinition any
type ToolProperty struct {
type ToolProperty map[string]interface{}
type DetailToolProperty struct {
Type *string `json:"type,omitempty"`
Enum *[]JsonSchemaType `json:"enum,omitempty"`
Const *JsonSchemaType `json:"const,omitempty"`

View File

@ -83,6 +83,10 @@ func NumTokensFromMessages(messages []globals.Message, model string, responseTyp
}
func NumTokensFromResponse(response string, model string) int {
if len(response) == 0 {
return 0
}
return NumTokensFromMessages([]globals.Message{{Content: response}}, model, true)
}