generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LLM clients for OpenAI, Google, Anthropic (#7)
- Loading branch information
1 parent
4d84a05
commit 9d074a5
Showing
12 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/cover.out | ||
/.env*.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package llm | ||
|
||
import ( | ||
"io" | ||
"log/slog" | ||
|
||
"github.com/anthropics/anthropic-sdk-go" | ||
"github.com/anthropics/anthropic-sdk-go/option" | ||
) | ||
|
||
type AnthropicClient struct { | ||
Client *anthropic.Client | ||
log *slog.Logger | ||
} | ||
|
||
type NewAnthropicClientOptions struct { | ||
Log *slog.Logger | ||
Token string | ||
} | ||
|
||
func NewAnthropicClient(opts NewAnthropicClientOptions) *AnthropicClient { | ||
if opts.Log == nil { | ||
opts.Log = slog.New(slog.NewTextHandler(io.Discard, nil)) | ||
} | ||
|
||
return &AnthropicClient{ | ||
Client: anthropic.NewClient(option.WithAPIKey(opts.Token)), | ||
log: opts.Log, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package llm_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/anthropics/anthropic-sdk-go" | ||
"maragu.dev/env" | ||
"maragu.dev/is" | ||
|
||
"maragu.dev/llm" | ||
) | ||
|
||
func TestNewAnthropicClient(t *testing.T) { | ||
t.Run("can create a new client with a token", func(t *testing.T) { | ||
client := llm.NewAnthropicClient(llm.NewAnthropicClientOptions{Token: "123"}) | ||
is.NotNil(t, client) | ||
}) | ||
} | ||
|
||
func TestAnthropicClientPrompt(t *testing.T) { | ||
_ = env.Load(".env.test.local") | ||
|
||
t.Run("can do a basic chat completion", func(t *testing.T) { | ||
client := llm.NewAnthropicClient(llm.NewAnthropicClientOptions{Token: env.GetStringOrDefault("ANTHROPIC_TOKEN", "")}) | ||
is.NotNil(t, client) | ||
|
||
res, err := client.Client.Messages.New(context.Background(), anthropic.MessageNewParams{ | ||
System: anthropic.F([]anthropic.TextBlockParam{ | ||
anthropic.NewTextBlock(`Only say the word "Hi", nothing more.`), | ||
}), | ||
Messages: anthropic.F([]anthropic.MessageParam{ | ||
anthropic.NewUserMessage(anthropic.NewTextBlock("Hi.")), | ||
}), | ||
Model: anthropic.F(anthropic.ModelClaude3_5HaikuLatest), | ||
MaxTokens: anthropic.F(int64(4)), | ||
}) | ||
is.NotError(t, err) | ||
is.True(t, strings.Contains(fmt.Sprint(res.Content), "Hi")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
llama32-1b: | ||
image: maragudk/llama-3.2-1b-instruct-q4_k_m | ||
ports: | ||
- "8090:8080" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.