mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 18:45:00 +00:00
refactor: break down json grammar parser in different files (#3004)
* refactor: break down json grammar parser in different files Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix: patch to `refactor_grammars` - propagate errors (#3006) propagate errors around Signed-off-by: Dave Lee <dave@gray101.com> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Signed-off-by: Dave Lee <dave@gray101.com> Co-authored-by: Dave <dave@gray101.com>
This commit is contained in:
parent
717cc6fe1a
commit
5eda7f578d
8 changed files with 218 additions and 153 deletions
|
@ -3,22 +3,11 @@ package functions_test
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/mudler/LocalAI/pkg/functions"
|
||||
. "github.com/mudler/LocalAI/pkg/functions"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func createFunction(field1 string, field2 string, name string, properties map[string]interface{}) map[string]interface{} {
|
||||
property := map[string]interface{}{}
|
||||
property[field1] = FunctionName{Const: name}
|
||||
property[field2] = Argument{
|
||||
Type: "object",
|
||||
Properties: properties,
|
||||
}
|
||||
return property
|
||||
}
|
||||
|
||||
var testFunctions = []Item{
|
||||
{
|
||||
Type: "object",
|
||||
|
@ -245,7 +234,8 @@ root-1-name ::= "\"search\""`
|
|||
var _ = Describe("JSON schema grammar tests", func() {
|
||||
Context("JSON", func() {
|
||||
It("generates a valid grammar from JSON schema", func() {
|
||||
grammar := NewJSONSchemaConverter("").GrammarFromBytes([]byte(testInput1))
|
||||
grammar, err := NewJSONSchemaConverter("").GrammarFromBytes([]byte(testInput1))
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(inputResult1, "\n")
|
||||
for _, r := range results {
|
||||
if r != "" {
|
||||
|
@ -255,7 +245,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
Expect(len(results)).To(Equal(len(strings.Split(grammar, "\n"))))
|
||||
})
|
||||
It("generates a valid grammar from JSON schema", func() {
|
||||
grammar := NewJSONSchemaConverter("").GrammarFromBytes([]byte(testInput2))
|
||||
grammar, err := NewJSONSchemaConverter("").GrammarFromBytes([]byte(testInput2))
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(inputResult3, "\n")
|
||||
for _, r := range results {
|
||||
if r != "" {
|
||||
|
@ -269,7 +260,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctions}
|
||||
|
||||
grammar := structuredGrammar.Grammar()
|
||||
grammar, err := structuredGrammar.Grammar()
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(inputResult1, "\n")
|
||||
for _, r := range results {
|
||||
if r != "" {
|
||||
|
@ -283,7 +275,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctions}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.EnableMaybeArray)
|
||||
grammar, err := structuredGrammar.Grammar(EnableMaybeArray)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
inputResult2,
|
||||
|
@ -301,7 +294,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.EnableMaybeArray)
|
||||
grammar, err := structuredGrammar.Grammar(EnableMaybeArray)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
inputResult4,
|
||||
|
@ -319,10 +313,11 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(
|
||||
functions.SetPrefix("suffix"),
|
||||
functions.EnableMaybeArray,
|
||||
grammar, err := structuredGrammar.Grammar(
|
||||
SetPrefix("suffix"),
|
||||
EnableMaybeArray,
|
||||
)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
rootResult(`"suffix" arr | realvalue`),
|
||||
|
@ -339,7 +334,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.SetPrefix("suffix"))
|
||||
grammar, err := structuredGrammar.Grammar(SetPrefix("suffix"))
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
rootResult(`"suffix" realvalue`),
|
||||
|
@ -356,7 +352,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.SetPrefix("suffix"), functions.EnableMaybeString)
|
||||
grammar, err := structuredGrammar.Grammar(SetPrefix("suffix"), EnableMaybeString)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
rootResult(`( "suffix" realvalue | mixedstring )`),
|
||||
|
@ -373,7 +370,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.SetPrefix("suffix"), functions.EnableMaybeString, functions.EnableMaybeArray)
|
||||
grammar, err := structuredGrammar.Grammar(SetPrefix("suffix"), EnableMaybeString, EnableMaybeArray)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
rootResult(`( "suffix" (arr | realvalue) | mixedstring )`),
|
||||
|
@ -392,7 +390,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.EnableMaybeString, functions.EnableMaybeArray)
|
||||
grammar, err := structuredGrammar.Grammar(EnableMaybeString, EnableMaybeArray)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
rootResult(`mixedstring | arr | realvalue`),
|
||||
|
@ -410,7 +409,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
structuredGrammar := JSONFunctionStructure{
|
||||
OneOf: testFunctionsName}
|
||||
|
||||
grammar := structuredGrammar.Grammar(functions.EnableMaybeString, functions.EnableMaybeArray, functions.NoMixedFreeString)
|
||||
grammar, err := structuredGrammar.Grammar(EnableMaybeString, EnableMaybeArray, NoMixedFreeString)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(
|
||||
strings.Join([]string{
|
||||
rootResult(`freestring | arr | realvalue`),
|
||||
|
@ -432,7 +432,8 @@ var _ = Describe("JSON schema grammar tests", func() {
|
|||
realvalue
|
||||
("," realvalue)*
|
||||
)? "]"`
|
||||
grammar := structuredGrammar.Grammar(functions.EnableMaybeString, functions.EnableMaybeArray, functions.DisableParallelNewLines)
|
||||
grammar, err := structuredGrammar.Grammar(EnableMaybeString, EnableMaybeArray, DisableParallelNewLines)
|
||||
Expect(err).To(BeNil())
|
||||
results := strings.Split(content, "\n")
|
||||
for _, r := range results {
|
||||
if r != "" {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue