diff --git a/api/openai.go b/api/openai.go index 71bf391d..3d09f3c7 100644 --- a/api/openai.go +++ b/api/openai.go @@ -145,6 +145,8 @@ type OpenAIRequest struct { // A grammar to constrain the LLM output Grammar string `json:"grammar" yaml:"grammar"` + // A grammar object + JSONFunctionGrammarObject *grammar.JSONFunctionStructure `json:"grammar_json_functions" yaml:"grammar_json_functions"` TypicalP float64 `json:"typical_p" yaml:"typical_p"` } @@ -433,6 +435,8 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { // Update input grammar jsStruct := funcs.ToJSONStructure() config.Grammar = jsStruct.Grammar("") + } else if input.JSONFunctionGrammarObject != nil { + config.Grammar = input.JSONFunctionGrammarObject.Grammar("") } // functions are not supported in stream mode (yet?) @@ -486,6 +490,7 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { } predInput = strings.Join(mess, "\n") + log.Debug().Msgf("Prompt (before templating): %s", predInput) if toStream { log.Debug().Msgf("Stream request received") @@ -522,7 +527,7 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error { log.Debug().Msgf("Template failed loading: %s", err.Error()) } - log.Debug().Msgf("Prompt: %s", predInput) + log.Debug().Msgf("Prompt (after templating): %s", predInput) if processFunctions { log.Debug().Msgf("Grammar: %+v", config.Grammar) } diff --git a/pkg/grammar/functions.go b/pkg/grammar/functions.go index 0971322e..c468a89a 100644 --- a/pkg/grammar/functions.go +++ b/pkg/grammar/functions.go @@ -11,8 +11,8 @@ type Function struct { } type Functions []Function -func (f Functions) ToJSONStructure() JSONStructure { - js := JSONStructure{} +func (f Functions) ToJSONStructure() JSONFunctionStructure { + js := JSONFunctionStructure{} for _, function := range f { // t := function.Parameters["type"] //tt := t.(string) diff --git a/pkg/grammar/json_schema.go b/pkg/grammar/json_schema.go index 447921e0..5db2bca2 100644 --- a/pkg/grammar/json_schema.go +++ b/pkg/grammar/json_schema.go @@ -211,12 +211,12 @@ type Item struct { Properties Properties `json:"properties"` } -type JSONStructure struct { +type JSONFunctionStructure struct { OneOf []Item `json:"oneOf,omitempty"` AnyOf []Item `json:"anyOf,omitempty"` } -func (j JSONStructure) Grammar(propOrder string) string { +func (j JSONFunctionStructure) Grammar(propOrder string) string { dat, _ := json.Marshal(j) return NewJSONSchemaConverter(propOrder).GrammarFromBytes(dat) } diff --git a/pkg/grammar/json_schema_test.go b/pkg/grammar/json_schema_test.go index 94e29582..0d8dd992 100644 --- a/pkg/grammar/json_schema_test.go +++ b/pkg/grammar/json_schema_test.go @@ -66,7 +66,7 @@ var _ = Describe("JSON schema grammar tests", func() { }) It("generates a valid grammar from JSON Objects", func() { - structuredGrammar := JSONStructure{ + structuredGrammar := JSONFunctionStructure{ OneOf: []Item{ { Type: "object",