gochat

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 7 Imported by: 0

README

🚀 GoChat

GoChat is a modern, enterprise-ready Go client SDK for Large Language Models (LLMs). It provides an elegant and type-safe unified interface that completely smooths out the API differences between OpenAI, Anthropic (Claude), DeepSeek, Qwen, Ollama, and other major cloud providers or local models.

Go Reference Go Version Go Report Card codecov Docs

English | 简体中文


✨ Core Features (Why GoChat?)

  • 🔌 Unified Interface: Provides a consistent API interface that shields differences between different LLM providers
  • 🏗️ Builder Pattern: Chain calls, complete LLM requests in a single line of code
  • 🔐 OAuth2 Support: Built-in OAuth2 device code authentication for Qwen, Gemini, MiniMax
  • 🔗 Workflow Orchestration: Elegantly organize complex RAG or Agent reasoning flows through Pipeline

📦 Installation

go get github.com/DotNetAge/gochat

🚀 Quick Start

1. Client - LLM Calls

Using the Builder pattern makes LLM calls extremely simple:

import "github.com/DotNetAge/gochat"

// Create Builder and send request
resp, err := gochat.NewClientBuilder().
    Init(gochat.Config{APIKey: "your-api-key"}).
    Model("gpt-4o").
    Temperature(0.7).
    UserMessage("Hello, please introduce the Go language").
    GetResponse(gochat.OpenAIClient)

if err != nil {
    log.Fatal(err)
}
fmt.Println(resp.Content)

Streaming Response:

stream, err := gochat.NewClientBuilder().
    Init(gochat.Config{APIKey: "your-api-key"}).
    Model("gpt-4o").
    UserMessage("Write a poem about spring").
    GetStream(gochat.OpenAIClient)

if err != nil {
    log.Fatal(err)
}
defer stream.Close()

for stream.Next() {
    event := stream.Event()
    fmt.Print(event.Content)
}

Supported Client Types:

Constant Provider
gochat.OpenAIClient OpenAI / Compatible API
gochat.AnthropicClient Anthropic Claude
gochat.DeepSeekClient DeepSeek
gochat.OllamaClient Ollama Local Models
gochat.AzureClient Azure OpenAI

More Builder Methods:

gochat.NewClientBuilder().
    Init(config).
    Model("gpt-4o").           // Set model
    Temperature(0.7).          // Set temperature
    MaxTokens(1000).           // Max tokens
    TopP(0.9).                 // Top-p sampling
    Stop("###", "END").        // Stop sequences
    EnableThinking(true).      // Enable thinking mode
    ThinkingBudget(1024).      // Thinking budget
    EnableSearch(true).        // Enable search
    SysemMessage("You are an assistant"). // System message
    UserMessage("User message").   // User message
    AssistantMessage("Assistant message"). // Assistant message
    AttachFile(attachment).    // Attach file
    AttachImage(image).        // Attach image
    Tools(tool).               // Tools
    UsageCallback(func(u core.Usage) {
        fmt.Printf("Token usage: %d\n", u.TotalTokens)
    }).
    GetResponse(gochat.OpenAIClient)

2. Auth - OAuth2 Authentication

GoChat has built-in OAuth2 device code authentication support for Qwen, Gemini, and MiniMax:

Qwen
import "github.com/DotNetAge/gochat/auth"

// Create AuthManager
manager := auth.Qwen()

// Login to get token (will print verification link and code)
if err := manager.Login(); err != nil {
    log.Fatal(err)
}

// Get token for API calls
token, err := manager.GetToken()
if err != nil {
    log.Fatal(err)
}

// Use token to create client
resp, err := gochat.NewClientBuilder().
    Init(core.Config{AuthToken: token.Access}).
    Model("qwen-max").
    UserMessage("Hello").
    GetResponse(gochat.OpenAIClient)
Gemini
// Need to provide OAuth2 configuration
manager := auth.GeminiWithConfig(
    "your-client-id",
    "your-client-secret",
    "http://localhost:8080/callback",
    ":8080",
)

if err := manager.Login(); err != nil {
    log.Fatal(err)
}
MiniMax
// "cn" for China version, other values for international version
manager := auth.MiniMax("cn")

if err := manager.Login(); err != nil {
    log.Fatal(err)
}

Token Persistence:

// Specify token file path
manager := auth.Qwen("/path/to/qwen_token.json")

// First login
manager.Login()

// Automatically load saved token on subsequent startups
token, err := manager.GetToken()

3. Pipeline - Workflow Orchestration

Pipeline is used to organize complex LLM workflows:

import (
    "github.com/DotNetAge/gochat"
    "github.com/DotNetAge/gochat/pipeline"
    "github.com/DotNetAge/gochat/pipeline/steps"
)

// Create client
client, _ := openai.NewOpenAI(core.Config{
    APIKey: "your-api-key",
    Model:  "gpt-4o",
})

// Create Pipeline
p := pipeline.New[*pipeline.State]().
    AddStep(steps.NewTemplateStep(
        "Please answer the following question: {{.question}}",
        "prompt",
        "question",
    )).
    AddStep(steps.NewGenerateCompletionStep(client, "prompt", "answer", "gpt-4o"))

// Create state and set input
state := pipeline.NewState()
state.Set("question", "What is Go language?")

// Execute Pipeline
if err := p.Execute(ctx, state); err != nil {
    log.Fatal(err)
}

// Get result
fmt.Println(state.GetString("answer"))

Built-in Step Types:

Step Function
NewTemplateStep Template rendering, render state variables into prompts
NewGenerateCompletionStep Call LLM to generate response

Add Hook to Monitor Execution:

type LoggerHook struct{}

func (h *LoggerHook) OnStepStart(ctx context.Context, step pipeline.Step[*pipeline.State], state *pipeline.State) {
    fmt.Printf("[%s] Started\n", step.Name())
}

func (h *LoggerHook) OnStepComplete(ctx context.Context, step pipeline.Step[*pipeline.State], state *pipeline.State) {
    fmt.Printf("[%s] Completed\n", step.Name())
}

func (h *LoggerHook) OnStepError(ctx context.Context, step pipeline.Step[*pipeline.State], state *pipeline.State, err error) {
    fmt.Printf("[%s] Failed: %v\n", step.Name(), err)
}

p := pipeline.New[*pipeline.State]().
    AddStep(step1).
    AddStep(step2).
    AddHook(&LoggerHook{})

🔌 Fully Supported Providers

Provider Models Auth Methods
OpenAI GPT-4o, o1, o3-mini API Key
Anthropic Claude 3.5/3.7 API Key
DeepSeek V3, R1 API Key
Alibaba Qwen Tongyi Qianwen series API Key, OAuth2, Device Code
Google Gemini 1.5 Pro/Flash API Key, OAuth2
Ollama Locally deployed models Local Execution (No Key Required)
Azure OpenAI Microsoft-deployed models API Key (Azure format)

🏗️ Project Architecture

gochat/
├── gochat.go       # Builder pattern entry
├── client/         # LLM provider client implementations
├── core/           # Core interfaces and common functionality
├── auth/           # OAuth2 authentication module
├── pipeline/       # Workflow orchestration functionality
├── provider/       # Additional provider implementations
├── docs/           # Documentation
└── examples/       # Example code

📄 License

This project is open-sourced under the MIT License. PRs are welcome!


📚 Comprehensive Documentation

Please refer to the docs/ directory for detailed guides, architecture diagrams, and API references:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientBuilder

type ClientBuilder interface {
	Init(config core.Config) ClientBuilder
	Temperature(temp float64) ClientBuilder
	Model(model string) ClientBuilder
	MaxTokens(max int) ClientBuilder
	Stop(stop ...string) ClientBuilder
	TopP(top float64) ClientBuilder
	EnableThinking(think bool) ClientBuilder
	ThinkingBudget(budget int) ClientBuilder
	EnableSearch(search bool) ClientBuilder
	IncrementalOutput(enabled bool) ClientBuilder // 流式增量输出(DeepSeek, Qwen)
	Format(format string) ClientBuilder           // 响应格式,如 "json"(Ollama)
	KeepAlive(duration string) ClientBuilder      // 模型内存保持时长(Ollama)
	UsageCallback(fn func(core.Usage)) ClientBuilder
	Attach(attachments ...core.Attachment) ClientBuilder
	UserMessage(msg string) ClientBuilder
	SysemMessage(msg string) ClientBuilder
	AssistantMessage(msg string) ClientBuilder
	Tools(tool ...core.Tool) ClientBuilder
	GetResponse(clientType ClientType) (*core.Response, error)
	GetStream(clientType ClientType) (*core.Stream, error)
}

func Client

func Client() ClientBuilder

Client 创建一个新的 ClientBuilder 实例

type ClientType

type ClientType int
const (
	OpenAIClient ClientType = iota
	AnthropicClient
	DeepSeekClient
	AzureClient
	OllamaClient
	QwenClient // 阿里云通义千问(使用 DashScope API)
)

Directories

Path Synopsis
client
examples
01_basic_chat command
Basic example demonstrating simple chat completion with OpenAI.
Basic example demonstrating simple chat completion with OpenAI.
02_multi_turn command
Multi-turn conversation example.
Multi-turn conversation example.
03_streaming command
Streaming response example.
Streaming response example.
04_tool_calling command
Tool calling example.
Tool calling example.
05_multiple_providers command
Multiple providers example.
Multiple providers example.
06_image_input command
Multimodal input example - sending images to the model.
Multimodal input example - sending images to the model.
07_document_analysis command
Document analysis example - analyzing PDF, text files, or other documents.
Document analysis example - analyzing PDF, text files, or other documents.
08_multiple_images command
Multiple images example - analyzing multiple images in one request.
Multiple images example - analyzing multiple images in one request.
09_helper_utilities command
Helper utilities for common use cases.
Helper utilities for common use cases.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL