Skip to content

Commit

Permalink
Bedrock pr review (#31)
Browse files Browse the repository at this point in the history
* WIP: Implement v3 anthropic client (bedrock support)

* Add unit tests

* Add initial tests for validation

* Finish validation tests

* Update integration tests, examples and readme

* Extract status code from bedrock error and send through current error
parser

* Update usage examples path to `pkg/internal/examples`.

---------

Co-authored-by: pigeonlaser <[email protected]>
  • Loading branch information
madebywelch and pigeonlaser authored Jun 30, 2024
1 parent 5c1492f commit 1cc78a7
Show file tree
Hide file tree
Showing 40 changed files with 1,857 additions and 966 deletions.
287 changes: 30 additions & 257 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,150 +1,55 @@
# Unofficial Anthropic SDK in Go
# Unofficial Anthropic SDK for Go

This project provides an unofficial Go SDK for Anthropic, a A next-generation AI assistant for your tasks, no matter the scale. The SDK makes it easy to interact with the Anthropic API in Go applications. For more information about Anthropic, including API documentation, visit the official [Anthropic documentation.](https://console.anthropic.com/docs)
This project provides an unofficial Go SDK for Anthropic, a next-generation AI assistant platform. The SDK simplifies interactions with the Anthropic API in Go applications. For more information about Anthropic and its API, visit the [official Anthropic documentation](https://console.anthropic.com/docs).

[![GoDoc](https://godoc.org/github.com/madebywelch/anthropic-go?status.svg)](https://pkg.go.dev/github.com/madebywelch/anthropic-go/v2)
[![GoDoc](https://godoc.org/github.com/madebywelch/anthropic-go?status.svg)](https://pkg.go.dev/github.com/madebywelch/anthropic-go/v3)

## Installation

You can install the Anthropic SDK in Go using go get:
Install the Anthropic SDK for Go using:

```go
go get github.com/madebywelch/anthropic-go/v2
go get github.com/madebywelch/anthropic-go/v3
```

## Usage

To use the Anthropic SDK, you'll need to initialize a client and make requests to the Anthropic API. Here's an example of initializing a client and performing a regular and a streaming completion:
## Features

## Completion Example

```go
package main
- Support for both native Anthropic API and AWS Bedrock
- Completion and streaming completion
- Message and streaming message support
- Tool usage capabilities

import (
"fmt"
## Quick Start

"github.com/madebywelch/anthropic-go/v2/pkg/anthropic"
"github.com/madebywelch/anthropic-go/v2/pkg/anthropic/utils"
)

func main() {
client, err := anthropic.NewClient("your-api-key")
if err != nil {
panic(err)
}

prompt, err := utils.GetPrompt("Why is the sky blue?")
if err != nil {
panic(err)
}

request := anthropic.NewCompletionRequest(
prompt,
anthropic.WithModel[anthropic.CompletionRequest](anthropic.ClaudeV2_1),
anthropic.WithMaxTokens[anthropic.CompletionRequest](100),
)

// Note: Only use client.Complete when streaming is disabled, otherwise use client.CompleteStream!
response, err := client.Complete(request)
if err != nil {
panic(err)
}

fmt.Printf("Completion: %s\n", response.Completion)
}
```

### Completion Example Output

```
The sky appears blue to us due to the way the atmosphere scatters light from the sun
```

## Completion Streaming Example
Here's a basic example of using the SDK:

```go
package main

import (
"context"
"fmt"

"github.com/madebywelch/anthropic-go/v2/pkg/anthropic"
"github.com/madebywelch/anthropic-go/v2/pkg/anthropic/utils"
"github.com/madebywelch/anthropic-go/v3/pkg/anthropic"
"github.com/madebywelch/anthropic-go/v3/pkg/anthropic/client/native"
)

func main() {
client, err := anthropic.NewClient("your-api-key")
ctx := context.Background()
client, err := native.MakeClient(native.Config{
APIKey: "your-api-key",
})
if err != nil {
panic(err)
}

prompt, err := utils.GetPrompt("Why is the sky blue?")
if err != nil {
panic(err)
}

request := anthropic.NewCompletionRequest(
prompt,
anthropic.WithModel[anthropic.CompletionRequest](anthropic.ClaudeV2_1),
anthropic.WithMaxTokens[anthropic.CompletionRequest](100),
anthropic.WithStreaming[anthropic.CompletionRequest](true),
)

// Note: Only use client.CompleteStream when streaming is enabled, otherwise use client.Complete!
resps, errs := client.CompleteStream(request)

for {
select {
case resp := <-resps:
fmt.Printf("Completion: %s\n", resp.Completion)
case err := <-errs:
panic(err)
}
}
}
```

### Completion Streaming Example Output

```
There
are
a
few
reasons
why
the
sky
appears
```

## Messages Example

```go
package main

import (
"fmt"

"github.com/madebywelch/anthropic-go/v2/pkg/anthropic"
)

func main() {
client, err := anthropic.NewClient("your-api-key")
if err != nil {
panic(err)
}

// Prepare a message request
request := anthropic.NewMessageRequest(
[]anthropic.MessagePartRequest{{Role: "user", Content: []anthropic.ContentBlock{anthropic.NewTextContentBlock("Hello, world!")}}},
anthropic.WithModel[anthropic.MessageRequest](anthropic.ClaudeV2_1),
anthropic.WithModel[anthropic.MessageRequest](anthropic.Claude35Sonnet),
anthropic.WithMaxTokens[anthropic.MessageRequest](20),
)

// Call the Message method
response, err := client.Message(request)
response, err := client.Message(ctx, request)
if err != nil {
panic(err)
}
Expand All @@ -153,152 +58,20 @@ func main() {
}
```

### Messages Example Output

```
{ID:msg_01W3bZkuMrS3h1ehqTdF84vv Type:message Model:claude-2.1 Role:assistant Content:[{Type:text Text:Hello!}] StopReason:end_turn Stop: StopSequence:}
```

## Messages Streaming Example

```go
package main

import (
"fmt"
"os"

"github.com/madebywelch/anthropic-go/v2/pkg/anthropic"
)

func main() {
apiKey, ok := os.LookupEnv("ANTHROPIC_API_KEY")
if !ok {
fmt.Printf("missing ANTHROPIC_API_KEY environment variable")
}
client, err := anthropic.NewClient(apiKey)
if err != nil {
panic(err)
}

// Prepare a message request
request := anthropic.NewMessageRequest(
[]anthropic.MessagePartRequest{{Role: "user", Content: "Hello, Good Morning!"}},
anthropic.WithModel[anthropic.MessageRequest](anthropic.ClaudeV2_1),
anthropic.WithMaxTokens[anthropic.MessageRequest](20),
anthropic.WithStreaming[anthropic.MessageRequest](true),
)

// Call the Message method
resps, errors := client.MessageStream(request)

for {
select {
case response := <-resps:
if response.Type == "content_block_delta" {
fmt.Println(response.Delta.Text)
}
if response.Type == "message_stop" {
fmt.Println("Message stop")
return
}
case err := <-errors:
fmt.Println(err)
return
}
}
}
```

### Messages Streaming Example Output

```
Good
morning
!
As
an
AI
language
model
,
I
don
't
have
feelings
or
a
physical
state
,
but
```

## Messages Tools Example

```go
package main

import (
"github.com/madebywelch/anthropic-go/v2/pkg/anthropic"
)

func main() {
client, err := anthropic.NewClient("your-api-key")
if err != nil {
panic(err)
}

// Prepare a message request
request := &anthropic.MessageRequest{
Model: anthropic.Claude3Opus,
MaxTokensToSample: 1024,
Tools: []anthropic.Tool{
{
Name: "get_weather",
Description: "Get the weather",
InputSchema: anthropic.InputSchema{
Type: "object",
Properties: map[string]anthropic.Property{
"city": {Type: "string", Description: "city to get the weather for"},
"unit": {Type: "string", Enum: []string{"celsius", "fahrenheit"}, Description: "temperature unit to return"}},
Required: []string{"city"},
},
},
},
Messages: []anthropic.MessagePartRequest{
{
Role: "user",
Content: []anthropic.ContentBlock{
anthropic.NewTextContentBlock("what's the weather in Charleston?"),
},
},
},
}

// Call the Message method
response, err := client.Message(request)
if err != nil {
panic(err)
}
## Usage

if response.StopReason == "tool_use" {
// Do something with the tool response
}
}
```
For more detailed usage examples, including streaming, completion, and tool usage, please refer to the `pkg/internal/examples` directory in the repository.

## Contributing

Contributions to this project are welcome. To contribute, follow these steps:
Contributions are welcome! To contribute:

- Fork this repository
- Create a new branch (`git checkout -b feature/my-new-feature`)
- Commit your changes (`git commit -am 'Add some feature'`)
- Push the branch (`git push origin feature/my-new-feature`)
- Create a new pull request
1. Fork this repository
2. Create a new branch (`git checkout -b feature/my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push the branch (`git push origin feature/my-new-feature`)
5. Create a new pull request

## License

This project is licensed under the Apache License, Version 2.0 - see the [LICENSE](LICENSE) file for details.
This project is licensed under the Apache License, Version 2.0. See the [LICENSE](LICENSE) file for details.
23 changes: 22 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
module github.com/madebywelch/anthropic-go/v2
module github.com/madebywelch/anthropic-go/v3

go 1.20

require (
github.com/aws/aws-sdk-go-v2 v1.30.1
github.com/aws/aws-sdk-go-v2/config v1.27.23
github.com/aws/aws-sdk-go-v2/credentials v1.17.23
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.12.1
)

require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.15 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.1 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
)
30 changes: 30 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
github.com/aws/aws-sdk-go-v2 v1.30.1 h1:4y/5Dvfrhd1MxRDD77SrfsDaj8kUkkljU7XE83NPV+o=
github.com/aws/aws-sdk-go-v2 v1.30.1/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 h1:tW1/Rkad38LA15X4UQtjXZXNKsCgkshC3EbmcUmghTg=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3/go.mod h1:UbnqO+zjqk3uIt9yCACHJ9IVNhyhOCnYk8yA19SAWrM=
github.com/aws/aws-sdk-go-v2/config v1.27.23 h1:Cr/gJEa9NAS7CDAjbnB7tHYb3aLZI2gVggfmSAasDac=
github.com/aws/aws-sdk-go-v2/config v1.27.23/go.mod h1:WMMYHqLCFu5LH05mFOF5tsq1PGEMfKbu083VKqLCd0o=
github.com/aws/aws-sdk-go-v2/credentials v1.17.23 h1:G1CfmLVoO2TdQ8z9dW+JBc/r8+MqyPQhXCafNZcXVZo=
github.com/aws/aws-sdk-go-v2/credentials v1.17.23/go.mod h1:V/DvSURn6kKgcuKEk4qwSwb/fZ2d++FFARtWSbXnLqY=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.9 h1:Aznqksmd6Rfv2HQN9cpqIV/lQRMaIpJkLLaJ1ZI76no=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.9/go.mod h1:WQr3MY7AxGNxaqAtsDWn+fBxmd4XvLkzeqQ8P1VM0/w=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.13 h1:5SAoZ4jYpGH4721ZNoS1znQrhOfZinOhc4XuTXx/nVc=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.13/go.mod h1:+rdA6ZLpaSeM7tSg/B0IEDinCIBJGmW8rKDFkYpP04g=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.13 h1:WIijqeaAO7TYFLbhsZmi2rgLEAtWOC1LhxCAVTJlSKw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.13/go.mod h1:i+kbfa76PQbWw/ULoWnp51EYVWH4ENln76fLQE3lXT8=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.12.1 h1:3B45hjMYPuv9K3M8dBUhQiLaZz6QIOF3AYgCadMoUpQ=
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.12.1/go.mod h1:jeJzYp86gwna3f1bV3q0A9pxOyrdK4D0thCZ84ru6L0=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.15 h1:I9zMeF107l0rJrpnHpjEiiTSCKYAIw8mALiXcPsGBiA=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.15/go.mod h1:9xWJ3Q/S6Ojusz1UIkfycgD1mGirJfLLKqq3LPT7WN8=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.1 h1:p1GahKIjyMDZtiKoIn0/jAj/TkMzfzndDv5+zi2Mhgc=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.1/go.mod h1:/vWdhoIoYA5hYoPZ6fm7Sv4d8701PiG5VKe8/pPJL60=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.1 h1:lCEv9f8f+zJ8kcFeAjRZsekLd/x5SAm96Cva+VbUdo8=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.1/go.mod h1:xyFHA4zGxgYkdD73VeezHt3vSKEG9EmFnGwoKlP00u4=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.1 h1:+woJ607dllHJQtsnJLi52ycuqHMwlW+Wqm2Ppsfp4nQ=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.1/go.mod h1:jiNR3JqT15Dm+QWq2SRgh0x0bCNSRP2L25+CqPNpJlQ=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
Loading

0 comments on commit 1cc78a7

Please sign in to comment.