An open API service indexing awesome lists of open source software.

https://github.com/cubeofcube-dev/go-anthropic

用 Go 实现的 Anthropic SDK,支持 Claude 2.1、Claude 3、Claude 3.5(文字和图片)等模型。Anthropic SDK implemented in Go, supporting models such as Claude 2.1, Claude 3, and Claude 3.5 (supports sending images), etc.
https://github.com/cubeofcube-dev/go-anthropic

anthropic claude-2 claude-3 claude-3-5 claude-3-haiku claude-3-opus claude-3-sonnet go sdk

Last synced: 2 months ago
JSON representation

用 Go 实现的 Anthropic SDK,支持 Claude 2.1、Claude 3、Claude 3.5(文字和图片)等模型。Anthropic SDK implemented in Go, supporting models such as Claude 2.1, Claude 3, and Claude 3.5 (supports sending images), etc.

Awesome Lists containing this project

README

        

# Go Anthropic

**English** | [简体中文](./README.zh-CN.md)

Anthropic SDK implemented in Go, supporting models such as Claude 2.1, Claude 3, and Claude 3.5 (supports sending images), etc.

## Installation

```
go get github.com/cubeofcube-dev/go-anthropic
```

## Usage

By default, it will fetch `ANTHROPIC_API_KEY` and `ANTHROPIC_BASE_URL` from the environment variables.

```go
package main

import (
"fmt"

"github.com/cubeofcube-dev/go-anthropic"
)

func main() {
cli := anthropic.NewClient()
// cli := anthropic.NewClient(anthropic.ClientOptions{
// ApiKey: os.Getenv("ANTHROPIC_API_KEY"),
// BaseUrl: anthropic.DEFAULT_ANTHROPIC_BASE_URL,
// })

resp, err := cli.CreateMessages(anthropic.MessagesRequest{
// `MODEL_CLAUDE_35_SONNET`、`MODEL_CLAUDE_3_SONNET`、`MODEL_CLAUDE_3_OPUS`、`MODEL_CLAUDE_2_1`
Model: anthropic.MODEL_CLAUDE_3_HAIKU
Messages: []anthropic.Message{{
Role: "user",
Content: []anthropic.MessageContent{
&anthropic.MessageContentText{
Type: "text",
Text: "Hello Claude!",
},
}},
},
MaxTokens: 1024,
})
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(resp.Content[0].Text)
}
```

## Streaming Responses

```go
package main

import (
"fmt"

"github.com/cubeofcube-dev/go-anthropic"
)

func main() {
cli := anthropic.NewClient()
// cli := anthropic.NewClient(anthropic.ClientOptions{
// ApiKey: os.Getenv("ANTHROPIC_API_KEY"),
// BaseUrl: anthropic.DEFAULT_ANTHROPIC_BASE_URL,
// })

stream, _ := cli.CreateMessagesStream(anthropic.MessagesRequest{
// `MODEL_CLAUDE_35_SONNET`、`MODEL_CLAUDE_3_SONNET`、`MODEL_CLAUDE_3_OPUS`、`MODEL_CLAUDE_2_1`
Model: anthropic.MODEL_CLAUDE_3_HAIKU
Messages: []anthropic.Message{{
Role: "user",
Content: []anthropic.MessageContent{
&anthropic.MessageContentText{
Type: "text",
Text: "Hello Claude!",
},
}},
},
MaxTokens: 1024,
Stream: true,
})
defer stream.Close()
for {
resp, err := stream.Recv()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(resp.Delta.Text)
}
}
```

## References

- https://docs.anthropic.com/claude/reference/messages_post
- https://docs.anthropic.com/claude/reference/messages-streaming