gochat

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 8 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
    SystemMessage("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 {
	Config(opts ...Option) ClientBuilder             // 用Option去设置 core.config
	Messages(messages ...core.Message) ClientBuilder // 批量设置消息
	Temperature(temp float64) ClientBuilder          // 采样温度,控制模型生成文本的多样性。temperature越高,生成的文本更多样,反之,生成的文本更确定。
	Model(model string) ClientBuilder                // 用于指定模型名称
	MaxTokens(max int) ClientBuilder                 // 用于限制模型输出的最大 Token 数。若生成内容超过此值,生成将提前停止,且返回的finish_reason为length。
	Stop(stop ...string) ClientBuilder               // 用于指定停止词。当模型生成的文本中出现stop 指定的字符串或token_id时,生成将立即终止。
	TopP(top float64) ClientBuilder                  // 核采样的概率阈值,控制模型生成文本的多样性。
	TopK(top int) ClientBuilder                      // 指定生成过程中用于采样的候选 Token 数量。值越大,输出越随机;值越小,输出越确定。若设为 null 或大于 100,则禁用 top_k 策略,仅 top_p 策略生效。取值必须为大于或等于 0 的整数。
	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
	SystemMessage(msg string) ClientBuilder    // 独立设置系统消息
	DeveloperMessage(msg string) ClientBuilder // 独立设置开发者消息 (OpenAI o1/o3)
	AssistantMessage(msg string) ClientBuilder
	Tools(tool ...core.Tool) ClientBuilder
	ToolChoice(choice interface{}) ClientBuilder   // 设置工具选择策略,支持 string (如 "auto") 或具体对象
	ParallelToolCalls(parallel bool) ClientBuilder // 是否开启并行工具调用
	PresencePenalty(penalty float64) ClientBuilder
	FrequencyPenalty(penalty float64) ClientBuilder // 频率惩罚
	ResponseFormat(format string) ClientBuilder     // 设置响应格式, "json" / "text"
	GetResponse() (*core.Response, error)           // 获取默认(OpenAI)的响应
	GetResponseFor(clientType ClientType) (*core.Response, error)
	GetStream() (*core.Stream, error) // 获取默认(OpenAI)的流
	GetStreamFor(clientType ClientType) (*core.Stream, error)
	Build() (core.Client, error) // 构建并返回客户端(openai)
	BuildFor(clientType ClientType) (core.Client, error)
}

func Client

func Client() ClientBuilder

Client 创建一个新的 ClientBuilder 实例

type ClientType

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

type Option added in v0.2.4

type Option func(*core.Config)

Option defines the configuration options for ClientBuilder

func WithAPIKey added in v0.2.4

func WithAPIKey(key string) Option

WithAPIKey sets the API key

func WithAuthToken added in v0.2.4

func WithAuthToken(token string) Option

WithAuthToken sets the auth token

func WithBaseURL added in v0.2.4

func WithBaseURL(url string) Option

WithBaseURL sets the base URL

func WithMaxRetries added in v0.2.4

func WithMaxRetries(retries int) Option

WithMaxRetries sets the maximum number of retries

func WithModel added in v0.2.4

func WithModel(model string) Option

WithModel sets the model name

func WithTimeout added in v0.2.4

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the request timeout

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