Skip to content

Commit

Permalink
Allow to customize no action behavior
Browse files Browse the repository at this point in the history
Signed-off-by: mudler <[email protected]>
  • Loading branch information
mudler committed Jul 9, 2023
1 parent b3f43ab commit e703226
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
10 changes: 8 additions & 2 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
43 changes: 25 additions & 18 deletions api/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -416,16 +399,40 @@ 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) {
log.Debug().Msgf("Response needs to process functions")

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)
}

Expand Down

0 comments on commit e703226

Please sign in to comment.