Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/http/endpoints/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func ChatEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator
}

// Update input grammar or json_schema based on use_llama_grammar option
jsStruct := funcs.ToJSONStructure(config.FunctionsConfig.FunctionNameKey, config.FunctionsConfig.FunctionNameKey)
jsStruct := config.FunctionsConfig.ToJSONStructure(funcs)
g, err := jsStruct.Grammar(config.FunctionsConfig.GrammarOptions()...)
if err == nil {
config.Grammar = g
Expand Down
2 changes: 1 addition & 1 deletion core/http/endpoints/openai/realtime_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (m *wrappedModel) Predict(ctx context.Context, messages schema.Messages, im
}

// Generate grammar from function definitions
jsStruct := functions.Functions(funcs).ToJSONStructure(turnCfg.FunctionsConfig.FunctionNameKey, turnCfg.FunctionsConfig.FunctionNameKey)
jsStruct := turnCfg.FunctionsConfig.ToJSONStructure(functions.Functions(funcs))
g, err := jsStruct.Grammar(turnCfg.FunctionsConfig.GrammarOptions()...)
if err == nil {
turnCfg.Grammar = g
Expand Down
2 changes: 1 addition & 1 deletion core/http/endpoints/openresponses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func ResponsesEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, eval
}

// Generate grammar to constrain model output to valid function calls
jsStruct := funcsWithNoAction.ToJSONStructure(cfg.FunctionsConfig.FunctionNameKey, cfg.FunctionsConfig.FunctionNameKey)
jsStruct := cfg.FunctionsConfig.ToJSONStructure(funcsWithNoAction)
g, err := jsStruct.Grammar(cfg.FunctionsConfig.GrammarOptions()...)
if err == nil {
cfg.Grammar = g
Expand Down
2 changes: 1 addition & 1 deletion core/http/endpoints/openresponses/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func handleWSResponseCreate(connCtx context.Context, conn *lockedConn, input *sc
funcsWithNoAction = funcsWithNoAction.Select(cfg.FunctionToCall())
}

jsStruct := funcsWithNoAction.ToJSONStructure(cfg.FunctionsConfig.FunctionNameKey, cfg.FunctionsConfig.FunctionNameKey)
jsStruct := cfg.FunctionsConfig.ToJSONStructure(funcsWithNoAction)
g, err := jsStruct.Grammar(cfg.FunctionsConfig.GrammarOptions()...)
if err == nil {
cfg.Grammar = g
Expand Down
5 changes: 5 additions & 0 deletions pkg/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (f Functions) ToJSONStructure(name, args string) JSONFunctionStructure {
return js
}

// ToJSONStructure converts functions using the configured property keys.
func (c FunctionsConfig) ToJSONStructure(functions Functions) JSONFunctionStructure {
return functions.ToJSONStructure(c.FunctionNameKey, c.FunctionArgumentsKey)
}

// Select returns a list of functions containing the function with the given name
func (f Functions) Select(name string) Functions {
var funcs Functions
Expand Down
27 changes: 27 additions & 0 deletions pkg/functions/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ var _ = Describe("LocalAI grammar functions", func() {
Expect(fnName.Const).To(Equal("search"))
Expect(fnArgs.Properties["query"].(map[string]any)["type"]).To(Equal("string"))
})

It("keeps the name and the arguments in separate properties when both keys are customized", func() {
var functions Functions = []Function{
{
Name: "get_weather",
Parameters: map[string]any{
"properties": map[string]any{
"city": map[string]any{
"type": "string",
},
},
},
},
}

config := FunctionsConfig{
FunctionNameKey: "function",
FunctionArgumentsKey: "parameters",
}
js := config.ToJSONStructure(functions)
Expect(js.OneOf[0].Properties).To(HaveLen(2))

fnName := js.OneOf[0].Properties["function"].(FunctionName)
fnArgs := js.OneOf[0].Properties["parameters"].(Argument)
Expect(fnName.Const).To(Equal("get_weather"))
Expect(fnArgs.Properties["city"].(map[string]any)["type"]).To(Equal("string"))
})
})
Context("Select()", func() {
It("selects one of the functions and returns a list containing only the selected one", func() {
Expand Down