Skip to content

Commit

Permalink
Move schema generation to it's own package inside pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Oct 26, 2024
1 parent 258ed24 commit 6153e1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
9 changes: 6 additions & 3 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/conneroisu/groq-go/pkg/builders"
"github.com/conneroisu/groq-go/pkg/schema"
"github.com/conneroisu/groq-go/pkg/tools"
)

Expand Down Expand Up @@ -102,7 +103,7 @@ type (
Description string `json:"description,omitempty"`
// description of the chat completion response format json schema.
// Schema is the schema of the chat completion response format json schema.
Schema Schema `json:"schema"`
Schema schema.Schema `json:"schema"`
// Strict determines whether to enforce the schema upon the generated
// content.
Strict bool `json:"strict"`
Expand Down Expand Up @@ -380,8 +381,10 @@ func (c *Client) CreateChatCompletionJSON(
request ChatCompletionRequest,
output any,
) (err error) {
r := &reflector{}
schema := r.ReflectFromType(reflect.TypeOf(output))
schema, err := schema.ReflectSchema(reflect.TypeOf(output))
if err != nil {
return err
}
request.ResponseFormat = &ChatCompletionResponseFormat{}
request.ResponseFormat.JSONSchema = &ChatCompletionResponseFormatJSONSchema{
Name: schema.Title,
Expand Down
2 changes: 1 addition & 1 deletion schema.go → pkg/schema/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package groq
package schema

import (
"bytes"
Expand Down
14 changes: 11 additions & 3 deletions schema_test.go → pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package groq
package schema

import (
"encoding/json"
Expand Down Expand Up @@ -471,8 +471,16 @@ func TestSchemaGeneration(t *testing.T) {
reflector *reflector
fixture string
}{
{&TestUser{}, &reflector{}, "testdata/test_user.json"},
{&UserWithAnchor{}, &reflector{}, "testdata/user_with_anchor.json"},
{
&TestUser{},
&reflector{},
"testdata/test_user.json",
},
{
&UserWithAnchor{},
&reflector{},
"testdata/user_with_anchor.json",
},
{
&TestUser{},
&reflector{AssignAnchor: true},
Expand Down

0 comments on commit 6153e1c

Please sign in to comment.