Unofficial Anthropic SDK in 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.
Installation
You can install the Anthropic SDK in Go using go get:
go get github.com/madebywelch/anthropic-go
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:
Completion Example
import "github.com/madebywelch/anthropic-go/pkg/anthropic"
func main() {
client, err := anthropic.NewClient(apiKey)
if err != nil {
panic(err)
}
response, _ := client.Complete(&anthropic.CompletionRequest{
Prompt: GetPrompt("Why is the sky blue?"),
Model: anthropic.ClaudeV1,
MaxTokensToSample: 100,
StopSequences: []string{"\r", "Human:"},
}, nil)
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
Streaming Example
import "github.com/madebywelch/anthropic-go/pkg/anthropic"
func main() {
client, err := anthropic.NewClient(apiKey)
if err != nil {
log.Fatalf("Error creating client: %v", err)
}
_, err := client.Complete(&anthropic.CompletionRequest{
Prompt: GetPrompt("Why is the sky blue?"),
Model: ClaudeV1,
MaxTokensToSample: 25,
Stream: true,
}, func(response *anthropic.CompletionResponse) error {
fmt.Printf("Completion: %s\n", response.Completion)
return nil
})
}
Streaming Example Output
The sky appears blue to
The sky appears blue to us due to how
The sky appears blue to us due to how the
The sky appears blue to us due to how the atmosphere
The sky appears blue to us due to how the atmosphere scatters light from
The sky appears blue to us due to how the atmosphere scatters light from the sun
Contributing
Contributions to this project are welcome. To contribute, follow these steps:
- 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
License
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.