A cross-platform Go library for interacting with multiple AI providers' APIs, inspired by aisuite. Currently supports OpenAI and Anthropic providers with a unified interface.
- Unified interface for multiple AI providers
- Currently supports:
- OpenAI (via go-openai)
- Anthropic (via official SDK)
- Groq (via OpenAI-compatible API)
- Gemini (via OpenAI-compatible API)
- SambaNova (via OpenAI-compatible API)
- Carefully designed API that follows each provider's best practices
- Gradual and thoughtful addition of necessary interfaces and fields
go get github.com/cpunion/go-aisuite
See complete examples in the examples directory.
package main
import (
"context"
"fmt"
"github.com/cpunion/go-aisuite"
"github.com/cpunion/go-aisuite/client"
)
func main() {
// Initialize client with environment variables
c := client.New(nil)
// Or initialize client with API keys
// c := client.New(&client.APIKey{
// OpenAI: "", // Set your OpenAI API key or keep empty to use OPENAI_API_KEY env
// Anthropic: "", // Set your Anthropic API key or keep empty to use ANTHROPIC_API_KEY env
// Groq: "", // Set your Groq API key or keep empty to use GROQ_API_KEY env
// Gemini: "", // Set your Gemini API key or keep empty to use GEMINI_API_KEY env
// Sambanova: "", // Set your SambaNova API key or keep empty to use SAMBANOVA_API_KEY env
// })
// Make a chat completion request
resp, err := c.ChatCompletion(context.Background(), aisuite.ChatCompletionRequest{
Model: "openai:gpt-4o-mini", // or "anthropic:claude-3-5-haiku-20241022"
Messages: []aisuite.ChatCompletionMessage{
{
Role: aisuite.RoleUser,
Content: "Hello, how are you?",
},
},
MaxTokens: 10,
})
if err != nil {
panic(err)
}
fmt.Printf("Response: %s\n", resp.Choices[0].Message.Content)
}
package main
import (
"context"
"fmt"
"github.com/cpunion/go-aisuite"
"github.com/cpunion/go-aisuite/client"
)
func main() {
// Initialize client with API keys
c := client.New(&client.APIKey{
OpenAI: "", // Set your OpenAI API key or keep empty to use OPENAI_API_KEY env
Anthropic: "", // Set your Anthropic API key or keep empty to use ANTHROPIC_API_KEY env
Groq: "", // Set your Groq API key or keep empty to use GROQ_API_KEY env
Gemini: "", // Set your Gemini API key or keep empty to use GEMINI_API_KEY env
Sambanova: "", // Set your SambaNova API key or keep empty to use SAMBANOVA_API_KEY env
})
// Create a streaming chat completion request
stream, err := c.StreamChatCompletion(context.Background(), aisuite.ChatCompletionRequest{
Model: "openai:gpt-4o-mini", // or "anthropic:claude-3-5-haiku-20241022"
Messages: []aisuite.ChatCompletionMessage{
{
Role: aisuite.RoleUser,
Content: "Hello, how are you?",
},
},
MaxTokens: 10,
})
if err != nil {
panic(err)
}
defer stream.Close()
// Read the response stream
for {
resp, err := stream.Recv()
if err != nil {
panic(err)
}
if len(resp.Choices) == 0 {
fmt.Println("No choices")
break
}
if resp.Choices[0].FinishReason != "" {
fmt.Printf("\nStream finished: %s\n", resp.Choices[0].FinishReason)
break
}
fmt.Print(resp.Choices[0].Delta.Content)
}
}
We welcome contributions! Please feel free to submit a Pull Request. We are carefully expanding the API surface area to maintain compatibility and usability across different providers.
MIT License
This project is inspired by aisuite and builds upon the excellent work of: