Skip to content

Latest commit

 

History

History
157 lines (123 loc) · 4.88 KB

README.md

File metadata and controls

157 lines (123 loc) · 4.88 KB

go-aisuite

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.

Build Status codecov GitHub go.mod Go version GitHub commits GitHub release Go Report Card Go Reference

Features

  • 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

Installation

go get github.com/cpunion/go-aisuite

Quick Start

See complete examples in the examples directory.

Chat

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)
}

Stream

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)
	}
}

Contributing

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.

License

MIT License

Acknowledgments

This project is inspired by aisuite and builds upon the excellent work of: