-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_openai_responses_tools_attachments_test.go
More file actions
255 lines (239 loc) · 7.66 KB
/
example_openai_responses_tools_attachments_test.go
File metadata and controls
255 lines (239 loc) · 7.66 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package integration
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/flexigpt/inference-go"
"github.com/flexigpt/inference-go/spec"
)
const sendFile = true
// Example_openAIResponses_toolsAndAttachments demonstrates a more advanced
// Responses call that:
//
// - defines function and web-search tools,
// - sends text + image + file as input content,
// - enables streaming of both text and reasoning.
//
// The example only attempts a live call when OPENAI_API_KEY is set. The
// image/file payloads are placeholders; for a real run, replace them with
// valid data or URLs.
func Example_openAIResponses_toolsAndAttachments() {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
ps, err := newProviderSetWithDebug(slog.LevelDebug)
if err != nil {
fmt.Fprintln(os.Stderr, "error creating ProviderSetAPI:", err)
return
}
_, err = ps.AddProvider(ctx, "openai-responses-extended", &inference.AddProviderConfig{
SDKType: spec.ProviderSDKTypeOpenAIResponses,
Origin: spec.DefaultOpenAIOrigin,
ChatCompletionPathPrefix: "/v1/responses",
APIKeyHeaderKey: spec.DefaultAuthorizationHeaderKey,
})
if err != nil {
fmt.Fprintln(os.Stderr, "error adding OpenAI Responses provider:", err)
return
}
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
fmt.Fprintln(os.Stderr, "OPENAI_API_KEY not set; skipping extended OpenAI Responses example")
fmt.Println("OK")
return
}
if err := ps.SetProviderAPIKey(ctx, "openai-responses-extended", apiKey); err != nil {
fmt.Fprintln(os.Stderr, "error setting OpenAI API key:", err)
return
}
// Tool: summarize_document(document: string, focus: string).
summarizeTool := spec.ToolChoice{
Type: spec.ToolTypeFunction,
ID: "summarize-document",
Name: "summarize_document",
Description: "Summarize a document with an optional focus.",
Arguments: map[string]any{
"type": "object",
"properties": map[string]any{
"document": map[string]any{
"type": "string",
"description": "Full text of the document to summarize.",
},
"focus": map[string]any{
"type": "string",
"description": "Optional topic to focus on.",
},
},
"required": []any{"document"},
"additionalProperties": false,
},
}
// Web search tool: used for retrieving fresh information when needed.
webSearchTool := spec.ToolChoice{
Type: spec.ToolTypeWebSearch,
ID: "web-search",
Name: "web_search",
Description: "Search the web for recent information.",
WebSearchArguments: &spec.WebSearchToolChoiceItem{
MaxUses: 2,
SearchContextSize: spec.WebSearchContextSizeMedium,
AllowedDomains: []string{}, // any domain
UserLocation: &spec.WebSearchToolChoiceItemUserLocation{
City: "San Francisco",
Country: "US",
Region: "CA",
Timezone: "America/Los_Angeles",
},
},
}
toolChoices := []spec.ToolChoice{summarizeTool, webSearchTool}
// Placeholder image data (not a real image). In a real application, provide a valid base64-encoded image.
// 1x1 transparent PNG.
fakeImageData := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
userMessage := spec.InputOutputContent{
Role: spec.RoleUser,
Contents: []spec.InputOutputContentItemUnion{
{
Kind: spec.ContentItemKindText,
TextItem: &spec.ContentItemText{
Text: "This is a test. Very briefly reply with attached PDF name and data and describe the image. Use tools where appropriate. Keep the final answer short.",
},
},
{
Kind: spec.ContentItemKindImage,
ImageItem: &spec.ContentItemImage{
ImageMIME: spec.DefaultImageDataMIME,
ImageData: fakeImageData,
ImageName: "example-image",
Detail: spec.ImageDetailLow,
ImageURL: "",
ID: "abc",
},
},
},
}
if sendFile {
// Placeholder PDF URL.
fileURL := "https://www.w3schools.com/asp/text/textfile.txt"
userMessage.Contents = append(userMessage.Contents, spec.InputOutputContentItemUnion{
Kind: spec.ContentItemKindFile,
FileItem: &spec.ContentItemFile{
FileName: "dummy",
FileMIME: "text/plain",
FileURL: fileURL,
},
})
}
req := &spec.FetchCompletionRequest{
ModelParam: spec.ModelParam{
Name: "gpt-5-mini",
Stream: true,
MaxPromptLength: 8192,
MaxOutputLength: 8192,
SystemPrompt: "You are a research assistant that first uses tools " +
"when needed, then answers succinctly.",
Reasoning: &spec.ReasoningParam{
Type: spec.ReasoningTypeSingleWithLevels,
Level: spec.ReasoningLevelMedium,
},
OutputParam: &spec.OutputParam{
// Responses maps this to params.Text.format + params.Text.verbosity.
Verbosity: func() *spec.OutputVerbosity {
v := spec.OutputVerbosityMedium
return &v
}(),
Format: &spec.OutputFormat{
Kind: spec.OutputFormatKindJSONSchema,
JSONSchemaParam: &spec.JSONSchemaParam{
Name: "final_answer",
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"image_description": map[string]any{"type": "string"},
"file_name": map[string]any{"type": "string"},
"answer": map[string]any{"type": "string"},
},
"required": []any{"image_description", "answer", "file_name"},
"additionalProperties": false,
},
Strict: true,
},
},
},
},
Inputs: []spec.InputUnion{
{
Kind: spec.InputKindInputMessage,
InputMessage: &userMessage,
},
},
ToolChoices: toolChoices,
ToolPolicy: &spec.ToolPolicy{
// Demonstrates policy plumbing.
Mode: spec.ToolPolicyModeAuto,
DisableParallel: true,
},
}
// Stream both text and reasoning to stdout.
opts := &spec.FetchCompletionOptions{
StreamHandler: func(ev spec.StreamEvent) error {
switch ev.Kind {
case spec.StreamContentKindText:
if ev.Text != nil {
fmt.Fprintln(os.Stderr, ev.Text.Text)
}
case spec.StreamContentKindThinking:
if ev.Thinking != nil {
// In a real app you might log this separately; here we
// just prefix it.
fmt.Fprintf(os.Stderr, "\n[thinking] %s \n", ev.Thinking.Text)
}
}
return nil
},
StreamConfig: &spec.StreamConfig{
// Use library defaults; override here if you want.
},
CompletionKey: "gpt5mini",
}
resp, err := ps.FetchCompletion(ctx, "openai-responses-extended", req, opts)
if err != nil {
fmt.Fprintln(os.Stderr, "\nFetchCompletion error:", err)
if resp != nil && resp.Error != nil {
fmt.Fprintln(os.Stderr, "Provider error:", resp.Error.Message)
}
return
}
fmt.Fprintln(os.Stderr, "\n\n--- normalized outputs ---")
for _, out := range resp.Outputs {
switch out.Kind {
case spec.OutputKindFunctionToolCall:
if out.FunctionToolCall != nil {
fmt.Fprintf(os.Stderr, "Function tool call: %s(%s)\n",
out.FunctionToolCall.Name,
out.FunctionToolCall.Arguments,
)
}
case spec.OutputKindWebSearchToolCall:
if out.WebSearchToolCall != nil {
fmt.Fprintf(os.Stderr, "Web search call: %+v\n", out.WebSearchToolCall.WebSearchToolCallItems)
}
case spec.OutputKindOutputMessage:
if out.OutputMessage != nil {
for _, c := range out.OutputMessage.Contents {
if c.Kind == spec.ContentItemKindText && c.TextItem != nil {
fmt.Fprintln(os.Stderr, "Final answer:", c.TextItem.Text)
}
}
}
case spec.OutputKindReasoningMessage:
if out.ReasoningMessage != nil && len(out.ReasoningMessage.Summary) > 0 {
fmt.Fprintln(os.Stderr, "Reasoning summary:", out.ReasoningMessage.Summary[0])
}
default:
}
}
fmt.Println("OK")
// Output: OK
}