https://github.com/jahvon/ai-adapter
Simple Go AI client adapter
https://github.com/jahvon/ai-adapter
client go llm
Last synced: 12 months ago
JSON representation
Simple Go AI client adapter
- Host: GitHub
- URL: https://github.com/jahvon/ai-adapter
- Owner: jahvon
- Created: 2025-06-26T17:29:30.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-26T18:47:21.000Z (12 months ago)
- Last Synced: 2025-06-26T19:35:55.441Z (12 months ago)
- Topics: client, go, llm
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AI Adapter
A unified Go library for interacting with multiple AI providers through a consistent interface.
Currently only **OpenAI** and **Anthropic** are supported.
## Quick Start
### Basic Usage
```go
package main
import (
"context"
"fmt"
"log"
"github.com/jahvon/ai-adapter"
)
func main() {
// Create configuration
cfg := ai.NewConfig().
WithProvider(ai.ProviderOpenAI).
WithModel(ai.ModelGPT4oMini).
WithAPIKeyEnv("OPENAI_API_KEY").
WithMaxTokens(500)
// Create adapter
adapter, err := ai.NewAdapter(cfg)
if err != nil {
log.Fatalf("Failed to create adapter: %v", err)
}
// Create request
req := &ai.Request{
UserPrompt: "Tell me a joke",
SystemPrompt: "You are a helpful assistant",
}
// Generate response
ctx := context.Background()
resp, err := adapter.Generate(ctx, req)
if err != nil {
log.Fatalf("Failed to generate response: %v", err)
}
fmt.Printf("Response: %s\n", resp.Content)
fmt.Printf("Model: %s\n", resp.Model)
fmt.Printf("Tokens used: %d\n", resp.Usage.TotalTokens)
}
```
### Streaming Response
```go
// Generate streaming response
stream, err := adapter.GenerateStream(ctx, req)
if err != nil {
log.Fatalf("Failed to generate stream: %v", err)
}
// Collect all chunks into a single response
accumulated, err := ai.CollectStream(ctx, stream)
if err != nil {
log.Fatalf("Failed to accumulate stream: %v", err)
}
fmt.Printf("Accumulated Response: %s\n", accumulated)
```