Skip to content

Commit cb0dfde

Browse files
committed
ADD a example for GPT chat completion tool_calls.
1 parent 0343e4c commit cb0dfde

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

examples/gpt-function-call/main.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
streamingjson "github.com/karminski/streaming-json-go"
7+
)
8+
9+
// In GPT's chat completion stream mode, the request for tool_calls returns a structure as follows:
10+
//
11+
// {
12+
// "id": "chatcmpl-?",
13+
// "object": "chat.completion.chunk",
14+
// "created": 1712000001,
15+
// "model": "gpt-4-0125-preview",
16+
// "system_fingerprint": "fp_?",
17+
// "choices": [
18+
// {
19+
// "index": 0,
20+
// "delta": {
21+
// "tool_calls": [
22+
// {
23+
// "index": 0,
24+
// "function": {
25+
// "arguments": "{\"fi"
26+
// }
27+
// }
28+
// ]
29+
// },
30+
// "logprobs": null,
31+
// "finish_reason": null
32+
// }
33+
// ]
34+
// }
35+
//
36+
// We need extract data.choices[0].delta.tool_calls[0].function.arguments.
37+
// The arguments fiels is a JSON fragment, we can use steaming-json-go complete it to a syntactically correct JSON and Unmarshal it.
38+
39+
func main() {
40+
// We use string slice to simulate the arguments field in the return of GPT.
41+
arguments := []string{`{"fu`, `nction`, `_name`, `"`, `:`, `"run`, `_code`, `", `, `"argu`, `ments"`, `: `, "\"print(", "\\\"hello", " world", "\\\"", ")\""}
42+
lexer := streamingjson.NewLexer()
43+
for _, jsonFragment := range arguments {
44+
errInAppendString := lexer.AppendString(jsonFragment)
45+
if errInAppendString != nil {
46+
panic("invalied json string appended: " + errInAppendString.Error())
47+
}
48+
completedJSON := lexer.CompleteJSON()
49+
fmt.Printf("%s\n", completedJSON)
50+
}
51+
// will print:
52+
// {"fu":null}
53+
// {"function":null}
54+
// {"function_name":null}
55+
// {"function_name":null}
56+
// {"function_name":null}
57+
// {"function_name":"run"}
58+
// {"function_name":"run_code"}
59+
// {"function_name":"run_code"}
60+
// {"function_name":"run_code", "argu":null}
61+
// {"function_name":"run_code", "arguments":null}
62+
// {"function_name":"run_code", "arguments":null}
63+
// {"function_name":"run_code", "arguments": "print("}
64+
// {"function_name":"run_code", "arguments": "print(\"hello"}
65+
// {"function_name":"run_code", "arguments": "print(\"hello world"}
66+
// {"function_name":"run_code", "arguments": "print(\"hello world\""}
67+
// {"function_name":"run_code", "arguments": "print(\"hello world\")"}
68+
}

0 commit comments

Comments
 (0)