-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_texttool_test.go
121 lines (108 loc) · 2.52 KB
/
example_texttool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package fun_test
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/fun"
)
var (
// It has to be an object and not just an array of numbers.
// This is current limitation of AI providers.
jsonSchemaNumbers = []byte(`
{
"type": "object",
"properties": {
"numbers": {"type": "array", "items": {"type": "number"}}
},
"additionalProperties": false,
"required": [
"numbers"
]
}
`)
jsonSchemaNumber = []byte(`{"type": "integer"}`)
)
type toolInput struct {
Numbers []float64 `json:"numbers"`
}
func ExampleTextTool() {
if os.Getenv("OPENAI_API_KEY") == "" {
fmt.Println("skipped")
return
}
ctx := context.Background()
f := fun.Text[int, int]{
Provider: &fun.OpenAITextProvider{
APIKey: os.Getenv("OPENAI_API_KEY"),
Model: "gpt-4o-mini-2024-07-18",
MaxContextLength: 128_000,
MaxResponseLength: 16_384,
Seed: 42,
},
Prompt: `Sum numbers together. Output only the number.`,
Tools: map[string]fun.TextTooler{
"sum_numbers": &fun.TextTool[toolInput, float64]{
Description: "Sums numbers together.",
InputJSONSchema: jsonSchemaNumbers,
OutputJSONSchema: jsonSchemaNumber,
Fun: func(_ context.Context, input toolInput) (float64, errors.E) {
res := 0.0
for _, n := range input.Numbers {
res += n
}
return res, nil
},
},
},
}
errE := f.Init(ctx)
if errE != nil {
log.Fatalf("% -+#.1v\n", errE)
}
// We use the recorder to make sure the tool has really been called.
ctx = fun.WithTextRecorder(ctx)
output, errE := f.Call(ctx, 38, 4)
if errE != nil {
log.Fatalf("% -+#.1v\n", errE)
}
fmt.Println(output)
calls := fun.GetTextRecorder(ctx).Calls()
// We change calls a bit for the example to be deterministic.
cleanCalls(calls)
messages, err := json.MarshalIndent(calls[0].Messages, "", " ")
if err != nil {
log.Fatalf("%v\n", err)
}
fmt.Println(string(messages))
// Output:
// 42
// [
// {
// "role": "system",
// "content": "Sum numbers together. Output only the number."
// },
// {
// "role": "user",
// "content": "[38,4]"
// },
// {
// "role": "tool_use",
// "content": "{\"numbers\":[38,4]}",
// "toolUseId": "call_1_2",
// "toolUseName": "sum_numbers"
// },
// {
// "role": "tool_result",
// "content": "42",
// "toolUseId": "call_1_2",
// "toolDuration": 100004.000
// },
// {
// "role": "assistant",
// "content": "42"
// }
// ]
}