Skip to content

Commit

Permalink
feat: Add the initial Anthropic SDK implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
unfunco committed Apr 17, 2024
1 parent 1f87886 commit 9be37d7
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ trim_trailing_whitespace = true
[*.{go,mod}]
indent_size = 4
tab_width = 4

[*.md]
indent_size = 4
tab_width = 4
trim_trailing_whitespace = false
37 changes: 37 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PR

on:
pull_request:
branches:
- main
paths-ignore:
- .editorconfig
- .gitignore
- LICENSE.md
- README.md

jobs:
verify:
name: Verify
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Check formatting
run: test -z "$(go fmt ./...)"

build:
name: Build
needs: [ verify ]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@

### Installation and usage

TODO
```go
package main

import (
"os"

"github.com/unfunco/anthropic-sdk-go"
)

func main() {
transport := &anthropic.Transport{APIKey: os.Getenv("ANTHROPIC_API_KEY")}
claude := anthropic.NewClient(transport.Client())

_, _, _, = claude.Messages.Create(&anthropic.CreateMessageOptions{
Model: anthropic.Claude3Sonnet20240229,
})
}
```

### Development and testing

Expand Down
34 changes: 34 additions & 0 deletions anthropic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Package anthropic provides a client library for interacting with the
// Anthropic safety-first language model REST APIs.
package anthropic

import (
"context"
"net/http"
)

// Client manages communication with the Anthropic REST API.
type Client struct {
client *http.Client
}

// NewClient returns a new Anthropic REST API client.
func NewClient(client *http.Client) *Client {
if client == nil {
client = &http.Client{}
}

return &Client{
client: client,
}
}

// NewRequest creates an API request.
func (c *Client) NewRequest(url string) (*http.Request, error) {
return http.NewRequest(http.MethodGet, url, nil)
}

// Do sends an API request and returns the API response.
func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
return c.client.Do(req.WithContext(ctx))
}
11 changes: 11 additions & 0 deletions anthropic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package anthropic

import "testing"

func TestNewClient(t *testing.T) {
c1 := NewClient(nil)
c2 := NewClient(nil)
if c1.client == c2.client {
t.Errorf("NewClient() = %v, want %v", c1.client, c2.client)
}
}
44 changes: 44 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package anthropic

import "net/http"

type MessagesService struct{}

// Message defines ...
type Message struct {
Content string
Role string
}

// CreateMessageOptions defines ...
type CreateMessageOptions struct {
// MaxTokens defines the maximum number of tokens to generate before
// stopping. Token generation may stop before reaching this limit, this only
// specifies the absolute maximum number of tokens to generate. Different
// models have different maximum token limits.
MaxTokens int
// Messages ...
Messages []Message
// Model defines the language model that will be used to complete the
// prompt. See model.go for a list of available models.
Model LanguageModel
// StopSequences defines custom text sequences that will cause the model to
// stop generating.
StopSequences []string
// System provides a means of specifying context and instructions to the
// model, such as specifying a particular goal or role.
System string
// Temperature defines the amount of randomness injected into the response.
// Note that even with a temperature of 0.0, results will not be fully
// deterministic.
Temperature *float64
// TopK is used to remove long tail low probability responses.
TopK *int
// TopP defines the cumulative probability of the highest probability.
TopP *float64
}

// Create creates a new message using the provided options.
func (c *MessagesService) Create(options *CreateMessageOptions) (*Message, *http.Response, error) {
return nil, nil, nil
}
1 change: 1 addition & 0 deletions message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package anthropic
15 changes: 15 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package anthropic

// LanguageModel represents a language model that can be used to complete
// a prompt.
// https://docs.anthropic.com/claude/docs/models-overview
type LanguageModel string

const (
Claude3Opus20240229 LanguageModel = "claude-3-opus-20240229"
Claude3Sonnet20240229 LanguageModel = "claude-3-sonnet-20240229"
Claude3Haiku20240229 LanguageModel = "claude-3-haiku-20240307"
Claude21 LanguageModel = "claude-2.1"
Claude20 LanguageModel = "claude-2.0"
ClaudeInstant12 LanguageModel = "claude-instant-1.2"
)
10 changes: 10 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package anthropic

// Transport provides ...
type Transport struct {
APIKey string
}

func (t *Transport) Client() *Client {
return NewClient(nil)
}
1 change: 1 addition & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package anthropic

0 comments on commit 9be37d7

Please sign in to comment.