You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -516,6 +516,66 @@ func main() {
516
516
```
517
517
</details>
518
518
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).
0 commit comments