Skip to content

Commit

Permalink
Add LLM clients for OpenAI, Google, Anthropic (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg authored Nov 28, 2024
1 parent 4d84a05 commit 9d074a5
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ jobs:
name: Test
runs-on: ubuntu-latest

services:
llama32-1b:
image: "maragudk/llama-3.2-1b-instruct-q4_k_m"
ports:
- 8090:8080

steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -32,6 +38,9 @@ jobs:

- name: Test
run: go test -v -coverprofile=cover.out -shuffle on ./...
env:
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
ANTHROPIC_TOKEN: ${{ secrets.ANTHROPIC_TOKEN }}

evaluate:
name: Evaluate
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/cover.out
/.env*.local
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ lint:
.PHONY: test
test:
go test -coverprofile=cover.out -shuffle on ./...

.PHONY: test-down
test-down:
docker compose down

.PHONY: test-up
test-up:
docker compose up -d
30 changes: 30 additions & 0 deletions anthropic.go
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,
}
}
43 changes: 43 additions & 0 deletions anthropic_test.go
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"))
})
}
5 changes: 5 additions & 0 deletions docker-compose.yaml
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"
44 changes: 44 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,49 @@ go 1.23

require (
github.com/agnivade/levenshtein v1.2.0
github.com/anthropics/anthropic-sdk-go v0.2.0-alpha.4
github.com/google/generative-ai-go v0.18.0
github.com/openai/openai-go v0.1.0-alpha.38
google.golang.org/api v0.189.0
maragu.dev/env v0.2.0
maragu.dev/is v0.2.0
)

require (
cloud.google.com/go v0.115.0 // indirect
cloud.google.com/go/ai v0.8.0 // indirect
cloud.google.com/go/auth v0.7.2 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/longrunning v0.5.7 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading

0 comments on commit 9d074a5

Please sign in to comment.