From e70322676cd2ca2182381260e62182214673d2c0 Mon Sep 17 00:00:00 2001 From: mudler Date: Sun, 9 Jul 2023 10:34:05 +0200 Subject: [PATCH] Allow to customize no action behavior Signed-off-by: mudler --- api/config.go | 10 ++++++++-- api/openai.go | 43 +++++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/api/config.go b/api/config.go index 60176ec..57fe0d1 100644 --- a/api/config.go +++ b/api/config.go @@ -42,19 +42,25 @@ type Config struct { MainGPU string `yaml:"main_gpu"` ImageGenerationAssets string `yaml:"asset_dir"` - DisableDefaultAnswer bool `yaml:"disable_default_answer"` - PromptCachePath string `yaml:"prompt_cache_path"` PromptCacheAll bool `yaml:"prompt_cache_all"` PromptCacheRO bool `yaml:"prompt_cache_ro"` Grammar string `yaml:"grammar"` + FunctionsConfig Functions `yaml:"function"` + PromptStrings, InputStrings []string InputToken [][]int functionCallString, functionCallNameString string } +type Functions struct { + DisableNoAction bool `yaml:"disable_no_action"` + NoActionFunctionName string `yaml:"no_action_function_name"` + NoActionDescriptionName string `yaml:"no_action_description_name"` +} + type TemplateConfig struct { Completion string `yaml:"completion"` Functions string `yaml:"function"` diff --git a/api/openai.go b/api/openai.go index c1b0104..9746952 100644 --- a/api/openai.go +++ b/api/openai.go @@ -363,23 +363,6 @@ func embeddingsEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { } func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { - // TODO: replace this with config settings - // Allow the user to set custom actions via config file - // to be "embedded" in each model - const noActionName = "answer" - const noActionDescription = "use this action to answer without performing any action" - - noActionGrammar := grammar.Function{ - Name: noActionName, - Description: noActionDescription, - Parameters: map[string]interface{}{ - "properties": map[string]interface{}{ - "message": map[string]interface{}{ - "type": "string", - "description": "The message to reply the user with", - }}, - }, - } process := func(s string, req *OpenAIRequest, config *Config, loader *model.ModelLoader, responses chan OpenAIResponse) { initialMessage := OpenAIResponse{ @@ -416,6 +399,18 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { } log.Debug().Msgf("Configuration read: %+v", config) + // Allow the user to set custom actions via config file + // to be "embedded" in each model + noActionName := "answer" + noActionDescription := "use this action to answer without performing any action" + + if config.FunctionsConfig.NoActionFunctionName != "" { + noActionName = config.FunctionsConfig.NoActionFunctionName + } + if config.FunctionsConfig.NoActionDescriptionName != "" { + noActionDescription = config.FunctionsConfig.NoActionDescriptionName + } + // process functions if we have any defined or if we have a function call string if len(input.Functions) > 0 && ((config.functionCallString != "none" || config.functionCallString == "") || len(config.functionCallNameString) > 0) { @@ -423,9 +418,21 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { processFunctions = true + noActionGrammar := grammar.Function{ + Name: noActionName, + Description: noActionDescription, + Parameters: map[string]interface{}{ + "properties": map[string]interface{}{ + "message": map[string]interface{}{ + "type": "string", + "description": "The message to reply the user with", + }}, + }, + } + // Append the no action function funcs = append(funcs, input.Functions...) - if !config.DisableDefaultAnswer { + if !config.FunctionsConfig.DisableNoAction { funcs = append(funcs, noActionGrammar) }