Skip to content

Commit 0ca4ea4

Browse files
authored
move json schema to directory/package (#407)
* move json schema to directory/package * added jsonschema to README
1 parent 5f4ef29 commit 0ca4ea4

File tree

4 files changed

+119
-54
lines changed

4 files changed

+119
-54
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,66 @@ func main() {
516516
```
517517
</details>
518518

519+
<details>
520+
<summary>JSON Schema for function calling</summary>
521+
522+
It is now possible for chat completion to choose to call a function for more information ([see developer docs here](https://platform.openai.com/docs/guides/gpt/function-calling)).
523+
524+
In order to describe the type of functions that can be called, a JSON schema must be provided. Many JSON schema libraries exist and are more advanced than what we can offer in this library, however we have included a simple `jsonschema` package for those who want to use this feature without formatting their own JSON schema payload.
525+
526+
The developer documents give this JSON schema definition as an example:
527+
528+
```json
529+
{
530+
"name":"get_current_weather",
531+
"description":"Get the current weather in a given location",
532+
"parameters":{
533+
"type":"object",
534+
"properties":{
535+
"location":{
536+
"type":"string",
537+
"description":"The city and state, e.g. San Francisco, CA"
538+
},
539+
"unit":{
540+
"type":"string",
541+
"enum":[
542+
"celsius",
543+
"fahrenheit"
544+
]
545+
}
546+
},
547+
"required":[
548+
"location"
549+
]
550+
}
551+
}
552+
```
553+
554+
Using the `jsonschema` package, this schema could be created using structs as such:
555+
556+
```go
557+
FunctionDefinition{
558+
Name: "get_current_weather",
559+
Parameters: jsonschema.Definition{
560+
Type: jsonschema.Object,
561+
Properties: map[string]jsonschema.Definition{
562+
"location": {
563+
Type: jsonschema.String,
564+
Description: "The city and state, e.g. San Francisco, CA",
565+
},
566+
"unit": {
567+
Type: jsonschema.String,
568+
Enum: []string{"celcius", "fahrenheit"},
569+
},
570+
},
571+
Required: []string{"location"},
572+
},
573+
}
574+
```
575+
576+
The `Parameters` field of a `FunctionDefinition` can accept either of the above styles, or even a nested struct from another library (as long as it can be marshalled into JSON).
577+
</details>
578+
519579
<details>
520580
<summary>Error handling</summary>
521581

chat.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,47 +62,16 @@ type FunctionDefinition struct {
6262
Name string `json:"name"`
6363
Description string `json:"description,omitempty"`
6464
// Parameters is an object describing the function.
65-
// You can pass a raw byte array describing the schema,
66-
// or you can pass in a struct which serializes to the proper JSONSchema.
67-
// The JSONSchemaDefinition struct is provided for convenience, but you should
68-
// consider another specialized library for more complex schemas.
65+
// You can pass a []byte describing the schema,
66+
// or you can pass in a struct which serializes to the proper JSON schema.
67+
// The jsonschema package is provided for convenience, but you should
68+
// consider another specialized library if you require more complex schemas.
6969
Parameters any `json:"parameters"`
7070
}
7171

7272
// Deprecated: use FunctionDefinition instead.
7373
type FunctionDefine = FunctionDefinition
7474

75-
type JSONSchemaType string
76-
77-
const (
78-
JSONSchemaTypeObject JSONSchemaType = "object"
79-
JSONSchemaTypeNumber JSONSchemaType = "number"
80-
JSONSchemaTypeString JSONSchemaType = "string"
81-
JSONSchemaTypeArray JSONSchemaType = "array"
82-
JSONSchemaTypeNull JSONSchemaType = "null"
83-
JSONSchemaTypeBoolean JSONSchemaType = "boolean"
84-
)
85-
86-
// JSONSchemaDefinition is a struct for JSON Schema.
87-
// It is fairly limited and you may have better luck using a third-party library.
88-
type JSONSchemaDefinition struct {
89-
// Type is a type of JSON Schema.
90-
Type JSONSchemaType `json:"type,omitempty"`
91-
// Description is a description of JSON Schema.
92-
Description string `json:"description,omitempty"`
93-
// Enum is a enum of JSON Schema. It used if Type is JSONSchemaTypeString.
94-
Enum []string `json:"enum,omitempty"`
95-
// Properties is a properties of JSON Schema. It used if Type is JSONSchemaTypeObject.
96-
Properties map[string]JSONSchemaDefinition `json:"properties,omitempty"`
97-
// Required is a required of JSON Schema. It used if Type is JSONSchemaTypeObject.
98-
Required []string `json:"required,omitempty"`
99-
// Items is a property of JSON Schema. It used if Type is JSONSchemaTypeArray.
100-
Items *JSONSchemaDefinition `json:"items,omitempty"`
101-
}
102-
103-
// Deprecated: use JSONSchemaDefinition instead.
104-
type JSONSchemaDefine = JSONSchemaDefinition
105-
10675
type FinishReason string
10776

10877
const (

chat_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package openai_test
22

33
import (
4-
. "github.com/sashabaranov/go-openai"
5-
"github.com/sashabaranov/go-openai/internal/test/checks"
6-
74
"context"
85
"encoding/json"
96
"fmt"
@@ -13,6 +10,10 @@ import (
1310
"strings"
1411
"testing"
1512
"time"
13+
14+
. "github.com/sashabaranov/go-openai"
15+
"github.com/sashabaranov/go-openai/internal/test/checks"
16+
"github.com/sashabaranov/go-openai/jsonschema"
1617
)
1718

1819
func TestChatCompletionsWrongModel(t *testing.T) {
@@ -128,22 +129,22 @@ func TestChatCompletionsFunctions(t *testing.T) {
128129
},
129130
Functions: []FunctionDefinition{{
130131
Name: "test",
131-
Parameters: &JSONSchemaDefinition{
132-
Type: JSONSchemaTypeObject,
133-
Properties: map[string]JSONSchemaDefinition{
132+
Parameters: &jsonschema.Definition{
133+
Type: jsonschema.Object,
134+
Properties: map[string]jsonschema.Definition{
134135
"count": {
135-
Type: JSONSchemaTypeNumber,
136+
Type: jsonschema.Number,
136137
Description: "total number of words in sentence",
137138
},
138139
"words": {
139-
Type: JSONSchemaTypeArray,
140+
Type: jsonschema.Array,
140141
Description: "list of words in sentence",
141-
Items: &JSONSchemaDefinition{
142-
Type: JSONSchemaTypeString,
142+
Items: &jsonschema.Definition{
143+
Type: jsonschema.String,
143144
},
144145
},
145146
"enumTest": {
146-
Type: JSONSchemaTypeString,
147+
Type: jsonschema.String,
147148
Enum: []string{"hello", "world"},
148149
},
149150
},
@@ -165,22 +166,22 @@ func TestChatCompletionsFunctions(t *testing.T) {
165166
},
166167
Functions: []FunctionDefine{{
167168
Name: "test",
168-
Parameters: &JSONSchemaDefine{
169-
Type: JSONSchemaTypeObject,
170-
Properties: map[string]JSONSchemaDefine{
169+
Parameters: &jsonschema.Definition{
170+
Type: jsonschema.Object,
171+
Properties: map[string]jsonschema.Definition{
171172
"count": {
172-
Type: JSONSchemaTypeNumber,
173+
Type: jsonschema.Number,
173174
Description: "total number of words in sentence",
174175
},
175176
"words": {
176-
Type: JSONSchemaTypeArray,
177+
Type: jsonschema.Array,
177178
Description: "list of words in sentence",
178-
Items: &JSONSchemaDefine{
179-
Type: JSONSchemaTypeString,
179+
Items: &jsonschema.Definition{
180+
Type: jsonschema.String,
180181
},
181182
},
182183
"enumTest": {
183-
Type: JSONSchemaTypeString,
184+
Type: jsonschema.String,
184185
Enum: []string{"hello", "world"},
185186
},
186187
},

jsonschema/json.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Package jsonschema provides very simple functionality for representing a JSON schema as a
2+
// (nested) struct. This struct can be used with the chat completion "function call" feature.
3+
// For more complicated schemas, it is recommended to use a dedicated JSON schema library
4+
// and/or pass in the schema in []byte format.
5+
package jsonschema
6+
7+
type DataType string
8+
9+
const (
10+
Object DataType = "object"
11+
Number DataType = "number"
12+
Integer DataType = "integer"
13+
String DataType = "string"
14+
Array DataType = "array"
15+
Null DataType = "null"
16+
Boolean DataType = "boolean"
17+
)
18+
19+
// Definition is a struct for describing a JSON Schema.
20+
// It is fairly limited and you may have better luck using a third-party library.
21+
type Definition struct {
22+
// Type specifies the data type of the schema.
23+
Type DataType `json:"type,omitempty"`
24+
// Description is the description of the schema.
25+
Description string `json:"description,omitempty"`
26+
// Enum is used to restrict a value to a fixed set of values. It must be an array with at least
27+
// one element, where each element is unique. You will probably only use this with strings.
28+
Enum []string `json:"enum,omitempty"`
29+
// Properties describes the properties of an object, if the schema type is Object.
30+
Properties map[string]Definition `json:"properties,omitempty"`
31+
// Required specifies which properties are required, if the schema type is Object.
32+
Required []string `json:"required,omitempty"`
33+
// Items specifies which data type an array contains, if the schema type is Array.
34+
Items *Definition `json:"items,omitempty"`
35+
}

0 commit comments

Comments
 (0)