Skip to content

Commit 55befe3

Browse files
committed
Add grammar_json to the request parameters to facilitate JSON generation
1 parent 483fddc commit 55befe3

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

api/openai.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ type OpenAIRequest struct {
145145

146146
// A grammar to constrain the LLM output
147147
Grammar string `json:"grammar" yaml:"grammar"`
148+
// A grammar object
149+
JSONFunctionGrammarObject *grammar.JSONFunctionStructure `json:"grammar_json_functions" yaml:"grammar_json_functions"`
148150

149151
TypicalP float64 `json:"typical_p" yaml:"typical_p"`
150152
}
@@ -433,6 +435,8 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error {
433435
// Update input grammar
434436
jsStruct := funcs.ToJSONStructure()
435437
config.Grammar = jsStruct.Grammar("")
438+
} else if input.JSONFunctionGrammarObject != nil {
439+
config.Grammar = input.JSONFunctionGrammarObject.Grammar("")
436440
}
437441

438442
// functions are not supported in stream mode (yet?)
@@ -486,6 +490,7 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error {
486490
}
487491

488492
predInput = strings.Join(mess, "\n")
493+
log.Debug().Msgf("Prompt (before templating): %s", predInput)
489494

490495
if toStream {
491496
log.Debug().Msgf("Stream request received")
@@ -522,7 +527,7 @@ func chatEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error {
522527
log.Debug().Msgf("Template failed loading: %s", err.Error())
523528
}
524529

525-
log.Debug().Msgf("Prompt: %s", predInput)
530+
log.Debug().Msgf("Prompt (after templating): %s", predInput)
526531
if processFunctions {
527532
log.Debug().Msgf("Grammar: %+v", config.Grammar)
528533
}

pkg/grammar/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type Function struct {
1111
}
1212
type Functions []Function
1313

14-
func (f Functions) ToJSONStructure() JSONStructure {
15-
js := JSONStructure{}
14+
func (f Functions) ToJSONStructure() JSONFunctionStructure {
15+
js := JSONFunctionStructure{}
1616
for _, function := range f {
1717
// t := function.Parameters["type"]
1818
//tt := t.(string)

pkg/grammar/json_schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ type Item struct {
211211
Properties Properties `json:"properties"`
212212
}
213213

214-
type JSONStructure struct {
214+
type JSONFunctionStructure struct {
215215
OneOf []Item `json:"oneOf,omitempty"`
216216
AnyOf []Item `json:"anyOf,omitempty"`
217217
}
218218

219-
func (j JSONStructure) Grammar(propOrder string) string {
219+
func (j JSONFunctionStructure) Grammar(propOrder string) string {
220220
dat, _ := json.Marshal(j)
221221
return NewJSONSchemaConverter(propOrder).GrammarFromBytes(dat)
222222
}

pkg/grammar/json_schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var _ = Describe("JSON schema grammar tests", func() {
6666
})
6767
It("generates a valid grammar from JSON Objects", func() {
6868

69-
structuredGrammar := JSONStructure{
69+
structuredGrammar := JSONFunctionStructure{
7070
OneOf: []Item{
7171
{
7272
Type: "object",

0 commit comments

Comments
 (0)