-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_anthropic_basic_test.go
More file actions
96 lines (87 loc) · 2.69 KB
/
example_anthropic_basic_test.go
File metadata and controls
96 lines (87 loc) · 2.69 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
package integration
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/flexigpt/inference-go"
"github.com/flexigpt/inference-go/spec"
)
// Example_anthropic_basicConversation demonstrates a minimal non-streaming
// call to Anthropic's Messages API using the normalized inference-go API.
func Example_anthropic_basicConversation() {
ctx, cancel := context.WithTimeout(context.Background(), 30*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, "anthropic", &inference.AddProviderConfig{
SDKType: spec.ProviderSDKTypeAnthropic,
Origin: spec.DefaultAnthropicOrigin,
ChatCompletionPathPrefix: spec.DefaultAnthropicChatCompletionPrefix,
APIKeyHeaderKey: spec.DefaultAnthropicAuthorizationHeaderKey,
// DefaultHeaders are optional; the official SDK sets anthropic-version.
})
if err != nil {
fmt.Fprintln(os.Stderr, "error adding Anthropic provider:", err)
return
}
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
fmt.Fprintln(os.Stderr, "ANTHROPIC_API_KEY not set; skipping live Anthropic call")
fmt.Println("OK")
return
}
if err := ps.SetProviderAPIKey(ctx, "anthropic", apiKey); err != nil {
fmt.Fprintln(os.Stderr, "error setting Anthropic API key:", err)
return
}
req := &spec.FetchCompletionRequest{
ModelParam: spec.ModelParam{
Name: "claude-haiku-4-5-20251001",
Stream: false,
MaxPromptLength: 4096,
MaxOutputLength: 256,
SystemPrompt: "You are a concise, helpful assistant.",
},
Inputs: []spec.InputUnion{
{
Kind: spec.InputKindInputMessage,
InputMessage: &spec.InputOutputContent{
Role: spec.RoleUser,
Contents: []spec.InputOutputContentItemUnion{
{
Kind: spec.ContentItemKindText,
TextItem: &spec.ContentItemText{
Text: "Say hello from Anthropic in one short sentence.",
},
},
},
},
},
},
}
resp, err := ps.FetchCompletion(ctx, "anthropic", req, &spec.FetchCompletionOptions{CompletionKey: "haiku45"})
if err != nil {
fmt.Fprintln(os.Stderr, "FetchCompletion error:", err)
if resp != nil && resp.Error != nil {
fmt.Fprintln(os.Stderr, "Provider error:", resp.Error.Message)
}
return
}
for _, out := range resp.Outputs {
if out.Kind != spec.OutputKindOutputMessage || out.OutputMessage == nil {
continue
}
for _, c := range out.OutputMessage.Contents {
if c.Kind == spec.ContentItemKindText && c.TextItem != nil {
fmt.Fprintln(os.Stderr, "Anthropic assistant:", c.TextItem.Text)
}
}
}
fmt.Println("OK")
// Output: OK
}