|
1 | 1 | package anthropic
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 |
| - "fmt" |
6 |
| - "strings" |
7 |
| - |
8 |
| - "github.com/anthropics/anthropic-sdk-go" |
9 |
| - "github.com/anthropics/anthropic-sdk-go/option" |
10 |
| - "github.com/danielmiessler/fabric/common" |
11 |
| - "github.com/danielmiessler/fabric/plugins" |
12 |
| - goopenai "github.com/sashabaranov/go-openai" |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/anthropics/anthropic-sdk-go" |
| 9 | + "github.com/anthropics/anthropic-sdk-go/option" |
| 10 | + "github.com/danielmiessler/fabric/common" |
| 11 | + "github.com/danielmiessler/fabric/plugins" |
| 12 | + goopenai "github.com/sashabaranov/go-openai" |
13 | 13 | )
|
14 | 14 |
|
15 | 15 | const defaultBaseUrl = "https://api.anthropic.com/"
|
16 | 16 |
|
17 | 17 | func NewClient() (ret *Client) {
|
18 |
| - vendorName := "Anthropic" |
19 |
| - ret = &Client{} |
20 |
| - |
21 |
| - ret.PluginBase = &plugins.PluginBase{ |
22 |
| - Name: vendorName, |
23 |
| - EnvNamePrefix: plugins.BuildEnvVariablePrefix(vendorName), |
24 |
| - ConfigureCustom: ret.configure, |
25 |
| - } |
26 |
| - |
27 |
| - ret.ApiBaseURL = ret.AddSetupQuestion("API Base URL", false) |
28 |
| - ret.ApiBaseURL.Value = defaultBaseUrl |
29 |
| - ret.ApiKey = ret.PluginBase.AddSetupQuestion("API key", true) |
30 |
| - |
31 |
| - // we could provide a setup question for the following settings |
32 |
| - ret.maxTokens = 4096 |
33 |
| - ret.defaultRequiredUserMessage = "Hi" |
34 |
| - ret.models = []string{ |
35 |
| - anthropic.ModelClaude3_5HaikuLatest, anthropic.ModelClaude3_5Haiku20241022, |
36 |
| - anthropic.ModelClaude3_5SonnetLatest, anthropic.ModelClaude3_5Sonnet20241022, |
37 |
| - anthropic.ModelClaude_3_5_Sonnet_20240620, anthropic.ModelClaude3OpusLatest, |
38 |
| - anthropic.ModelClaude_3_Opus_20240229, anthropic.ModelClaude_3_Sonnet_20240229, |
39 |
| - anthropic.ModelClaude_3_Haiku_20240307, anthropic.ModelClaude_2_1, |
40 |
| - anthropic.ModelClaude_2_0, anthropic.ModelClaude_Instant_1_2, |
41 |
| - } |
42 |
| - |
43 |
| - return |
| 18 | + vendorName := "Anthropic" |
| 19 | + ret = &Client{} |
| 20 | + |
| 21 | + ret.PluginBase = &plugins.PluginBase{ |
| 22 | + Name: vendorName, |
| 23 | + EnvNamePrefix: plugins.BuildEnvVariablePrefix(vendorName), |
| 24 | + ConfigureCustom: ret.configure, |
| 25 | + } |
| 26 | + |
| 27 | + ret.ApiBaseURL = ret.AddSetupQuestion("API Base URL", false) |
| 28 | + ret.ApiBaseURL.Value = defaultBaseUrl |
| 29 | + ret.ApiKey = ret.PluginBase.AddSetupQuestion("API key", true) |
| 30 | + |
| 31 | + ret.maxTokens = 4096 |
| 32 | + ret.defaultRequiredUserMessage = "Hi" |
| 33 | + ret.models = []string{ |
| 34 | + anthropic.ModelClaude3_5HaikuLatest, anthropic.ModelClaude3_5Haiku20241022, |
| 35 | + anthropic.ModelClaude3_5SonnetLatest, anthropic.ModelClaude3_5Sonnet20241022, |
| 36 | + anthropic.ModelClaude_3_5_Sonnet_20240620, anthropic.ModelClaude3OpusLatest, |
| 37 | + anthropic.ModelClaude_3_Opus_20240229, anthropic.ModelClaude_3_Sonnet_20240229, |
| 38 | + anthropic.ModelClaude_3_Haiku_20240307, anthropic.ModelClaude_2_1, |
| 39 | + anthropic.ModelClaude_2_0, anthropic.ModelClaude_Instant_1_2, |
| 40 | + } |
| 41 | + |
| 42 | + return |
44 | 43 | }
|
45 | 44 |
|
46 | 45 | type Client struct {
|
47 |
| - *plugins.PluginBase |
48 |
| - ApiBaseURL *plugins.SetupQuestion |
49 |
| - ApiKey *plugins.SetupQuestion |
| 46 | + *plugins.PluginBase |
| 47 | + ApiBaseURL *plugins.SetupQuestion |
| 48 | + ApiKey *plugins.SetupQuestion |
50 | 49 |
|
51 |
| - maxTokens int |
52 |
| - defaultRequiredUserMessage string |
53 |
| - models []string |
| 50 | + maxTokens int |
| 51 | + defaultRequiredUserMessage string |
| 52 | + models []string |
54 | 53 |
|
55 |
| - client *anthropic.Client |
| 54 | + client *anthropic.Client |
56 | 55 | }
|
57 | 56 |
|
58 | 57 | func (an *Client) configure() (err error) {
|
59 |
| - if an.ApiBaseURL.Value != "" { |
60 |
| - baseURL := an.ApiBaseURL.Value |
61 |
| - |
62 |
| - // If the base URL contains a UUID, ensure it ends with /v1 |
63 |
| - if strings.Contains(baseURL, "-") && !strings.HasSuffix(baseURL, "/v1") { |
64 |
| - if strings.HasSuffix(baseURL, "/") { |
65 |
| - baseURL = strings.TrimSuffix(baseURL, "/") |
66 |
| - } |
67 |
| - baseURL = baseURL + "/v1" |
68 |
| - } |
69 |
| - |
70 |
| - an.client = anthropic.NewClient( |
71 |
| - option.WithAPIKey(an.ApiKey.Value), |
72 |
| - option.WithBaseURL(baseURL), |
73 |
| - ) |
74 |
| - } else { |
75 |
| - an.client = anthropic.NewClient(option.WithAPIKey(an.ApiKey.Value)) |
76 |
| - } |
77 |
| - return |
| 58 | + if an.ApiBaseURL.Value != "" { |
| 59 | + baseURL := an.ApiBaseURL.Value |
| 60 | + |
| 61 | + if strings.Contains(baseURL, "-") && !strings.HasSuffix(baseURL, "/v1") { |
| 62 | + if strings.HasSuffix(baseURL, "/") { |
| 63 | + baseURL = strings.TrimSuffix(baseURL, "/") |
| 64 | + } |
| 65 | + baseURL = baseURL + "/v1" |
| 66 | + } |
| 67 | + |
| 68 | + an.client = anthropic.NewClient( |
| 69 | + option.WithAPIKey(an.ApiKey.Value), |
| 70 | + option.WithBaseURL(baseURL), |
| 71 | + ) |
| 72 | + } else { |
| 73 | + an.client = anthropic.NewClient(option.WithAPIKey(an.ApiKey.Value)) |
| 74 | + } |
| 75 | + return |
78 | 76 | }
|
79 | 77 |
|
80 | 78 | func (an *Client) ListModels() (ret []string, err error) {
|
81 |
| - return an.models, nil |
| 79 | + return an.models, nil |
82 | 80 | }
|
83 | 81 |
|
84 | 82 | func (an *Client) SendStream(
|
85 |
| - msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string, |
| 83 | + msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions, channel chan string, |
86 | 84 | ) (err error) {
|
87 |
| - |
88 |
| - messages := an.toMessages(msgs) |
89 |
| - |
90 |
| - ctx := context.Background() |
91 |
| - stream := an.client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{ |
92 |
| - Model: anthropic.F(opts.Model), |
93 |
| - MaxTokens: anthropic.F(int64(an.maxTokens)), |
94 |
| - TopP: anthropic.F(opts.TopP), |
95 |
| - Temperature: anthropic.F(opts.Temperature), |
96 |
| - Messages: anthropic.F(messages), |
97 |
| - }) |
98 |
| - |
99 |
| - for stream.Next() { |
100 |
| - event := stream.Current() |
101 |
| - |
102 |
| - switch delta := event.Delta.(type) { |
103 |
| - case anthropic.ContentBlockDeltaEventDelta: |
104 |
| - if delta.Text != "" { |
105 |
| - channel <- delta.Text |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - if stream.Err() != nil { |
111 |
| - fmt.Printf("Messages stream error: %v\n", stream.Err()) |
112 |
| - } |
113 |
| - close(channel) |
114 |
| - return |
| 85 | + messages := an.toMessages(msgs) |
| 86 | + |
| 87 | + ctx := context.Background() |
| 88 | + stream := an.client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{ |
| 89 | + Model: anthropic.F(opts.Model), |
| 90 | + MaxTokens: anthropic.F(int64(an.maxTokens)), |
| 91 | + TopP: anthropic.F(opts.TopP), |
| 92 | + Temperature: anthropic.F(opts.Temperature), |
| 93 | + Messages: anthropic.F(messages), |
| 94 | + }) |
| 95 | + |
| 96 | + for stream.Next() { |
| 97 | + event := stream.Current() |
| 98 | + |
| 99 | + switch delta := event.Delta.(type) { |
| 100 | + case anthropic.ContentBlockDeltaEventDelta: |
| 101 | + if delta.Text != "" { |
| 102 | + channel <- delta.Text |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if stream.Err() != nil { |
| 108 | + fmt.Printf("Messages stream error: %v\n", stream.Err()) |
| 109 | + } |
| 110 | + close(channel) |
| 111 | + return |
115 | 112 | }
|
116 | 113 |
|
117 | 114 | func (an *Client) Send(ctx context.Context, msgs []*goopenai.ChatCompletionMessage, opts *common.ChatOptions) (ret string, err error) {
|
118 |
| - messages := an.toMessages(msgs) |
119 |
| - |
120 |
| - var message *anthropic.Message |
121 |
| - if message, err = an.client.Messages.New(ctx, anthropic.MessageNewParams{ |
122 |
| - Model: anthropic.F(opts.Model), |
123 |
| - MaxTokens: anthropic.F(int64(an.maxTokens)), |
124 |
| - TopP: anthropic.F(opts.TopP), |
125 |
| - Temperature: anthropic.F(opts.Temperature), |
126 |
| - Messages: anthropic.F(messages), |
127 |
| - }); err != nil { |
128 |
| - return |
129 |
| - } |
130 |
| - ret = message.Content[0].Text |
131 |
| - return |
| 115 | + messages := an.toMessages(msgs) |
| 116 | + |
| 117 | + var message *anthropic.Message |
| 118 | + if message, err = an.client.Messages.New(ctx, anthropic.MessageNewParams{ |
| 119 | + Model: anthropic.F(opts.Model), |
| 120 | + MaxTokens: anthropic.F(int64(an.maxTokens)), |
| 121 | + TopP: anthropic.F(opts.TopP), |
| 122 | + Temperature: anthropic.F(opts.Temperature), |
| 123 | + Messages: anthropic.F(messages), |
| 124 | + }); err != nil { |
| 125 | + return |
| 126 | + } |
| 127 | + ret = message.Content[0].Text |
| 128 | + return |
132 | 129 | }
|
133 | 130 |
|
134 | 131 | func (an *Client) toMessages(msgs []*goopenai.ChatCompletionMessage) (ret []anthropic.MessageParam) {
|
135 |
| - // we could call the method before calling the specific vendor |
136 |
| - normalizedMessages := common.NormalizeMessages(msgs, an.defaultRequiredUserMessage) |
137 |
| - |
138 |
| - // Iterate over the incoming session messages and process them |
139 |
| - for _, msg := range normalizedMessages { |
140 |
| - var message anthropic.MessageParam |
141 |
| - switch msg.Role { |
142 |
| - case goopenai.ChatMessageRoleUser: |
143 |
| - message = anthropic.NewUserMessage(anthropic.NewTextBlock(msg.Content)) |
144 |
| - default: |
145 |
| - message = anthropic.NewAssistantMessage(anthropic.NewTextBlock(msg.Content)) |
146 |
| - } |
147 |
| - ret = append(ret, message) |
148 |
| - } |
149 |
| - return |
| 132 | + normalizedMessages := common.NormalizeMessages(msgs, an.defaultRequiredUserMessage) |
| 133 | + |
| 134 | + for _, msg := range normalizedMessages { |
| 135 | + var message anthropic.MessageParam |
| 136 | + switch msg.Role { |
| 137 | + case goopenai.ChatMessageRoleUser: |
| 138 | + message = anthropic.NewUserMessage(anthropic.NewTextBlock(msg.Content)) |
| 139 | + default: |
| 140 | + message = anthropic.NewAssistantMessage(anthropic.NewTextBlock(msg.Content)) |
| 141 | + } |
| 142 | + ret = append(ret, message) |
| 143 | + } |
| 144 | + return |
150 | 145 | }
|
0 commit comments