The Model module is the large language model abstraction layer of the tRPC-Agent-Go framework, providing a unified LLM interface design that currently supports OpenAI-compatible API calls. Through standardized interface design, developers can flexibly switch between different model providers, achieving seamless model integration and invocation. This module has been verified to be compatible with most OpenAI-like interfaces both inside and outside the company.
The Model module has the following core features:
Unified Interface Abstraction: Provides standardized Model interface, shielding differences between model providers
Streaming Response Support: Native support for streaming output, enabling real-time interactive experience
Multimodal Capabilities: Supports text, image, audio, and other multimodal content processing
Complete Error Handling: Provides dual-layer error handling mechanism, distinguishing between system errors and API errors
Extensible Configuration: Supports rich custom configuration options to meet different scenario requirements
# Basic usage: Configure through environment variables, run directly.cdexamples/runner
exportOPENAI_BASE_URL="https://api.deepseek.com/v1"exportOPENAI_API_KEY="your-api-key"gorunmain.go-modeldeepseek-chat
Platform Integration Configuration
All platform integration methods follow the same pattern, only requiring configuration of different environment variables or direct setting in code:
model:=openai.New("Model name",openai.WithBaseURL("Platform API address"),openai.WithAPIKey("API key"),)
Supported Platforms and Their Configuration
The following are configuration examples for each platform, divided into environment variable configuration and code configuration methods:
Environment Variable Configuration
The runner example supports specifying model names through command line parameters (-model), which is actually passing the model name when calling openai.New().
model:=openai.New("deepseek-chat",openai.WithBaseURL("https://api.deepseek.com/v1"),openai.WithAPIKey("your-api-key"),)// Other platform configurations are similar, only need to modify model name, BaseURL and APIKey, no additional fields needed.
// Model is the interface that all language models must implement.typeModelinterface{// Generate content, supports streaming response.GenerateContent(ctxcontext.Context,request*Request)(<-chan*Response,error)// Return basic model information.Info()Info}// Model information structure.typeInfostruct{Namestring// Model name.}
// Request represents the request sent to the model.typeRequeststruct{// Message list, containing system instructions, user input and assistant replies.Messages[]Message`json:"messages"`// Generation configuration (inlined into request).GenerationConfig`json:",inline"`// Tool list.Toolsmap[string]tool.Tool`json:"-"`}// GenerationConfig contains generation parameter configuration.typeGenerationConfigstruct{// Whether to use streaming response.Streambool`json:"stream"`// Temperature parameter (0.0-2.0).Temperature*float64`json:"temperature,omitempty"`// Maximum generation token count.MaxTokens*int`json:"max_tokens,omitempty"`// Top-P sampling parameter.TopP*float64`json:"top_p,omitempty"`// Stop generation markers.Stop[]string`json:"stop,omitempty"`// Frequency penalty.FrequencyPenalty*float64`json:"frequency_penalty,omitempty"`// Presence penalty.PresencePenalty*float64`json:"presence_penalty,omitempty"`// Reasoning effort level ("low", "medium", "high").ReasoningEffort*string`json:"reasoning_effort,omitempty"`// Whether to enable thinking mode.ThinkingEnabled*bool`json:"-"`// Maximum token count for thinking mode.ThinkingTokens*int`json:"-"`}
// Response represents the response returned by the model.typeResponsestruct{// OpenAI compatible fields.IDstring`json:"id,omitempty"`Objectstring`json:"object,omitempty"`Createdint64`json:"created,omitempty"`Modelstring`json:"model,omitempty"`SystemFingerprint*string`json:"system_fingerprint,omitempty"`Choices[]Choice`json:"choices,omitempty"`Usage*Usage`json:"usage,omitempty"`// Error information.Error*ResponseError`json:"error,omitempty"`// Internal fields.Timestamptime.Time`json:"-"`Donebool`json:"-"`IsPartialbool`json:"-"`}// ResponseError represents API-level errors.typeResponseErrorstruct{Messagestring`json:"message"`TypeErrorType`json:"type"`Paramstring`json:"param,omitempty"`Codestring`json:"code,omitempty"`}
import("context""fmt""trpc.group/trpc-go/trpc-agent-go/model""trpc.group/trpc-go/trpc-agent-go/model/openai")funcmain(){// Create model instance.llm:=openai.New("deepseek-chat")// Build request.temperature:=0.7maxTokens:=1000request:=&model.Request{Messages:[]model.Message{model.NewSystemMessage("You are a professional AI assistant."),model.NewUserMessage("Introduce Go language's concurrency features."),},GenerationConfig:model.GenerationConfig{Temperature:&temperature,MaxTokens:&maxTokens,Stream:false,},}// Call model.ctx:=context.Background()responseChan,err:=llm.GenerateContent(ctx,request)iferr!=nil{fmt.Printf("System error: %v\n",err)return}// Handle response.forresponse:=rangeresponseChan{ifresponse.Error!=nil{fmt.Printf("API error: %s\n",response.Error.Message)return}iflen(response.Choices)>0{fmt.Printf("Reply: %s\n",response.Choices[0].Message.Content)}ifresponse.Done{break}}}
// Streaming request configuration.request:=&model.Request{Messages:[]model.Message{model.NewSystemMessage("You are a creative story teller."),model.NewUserMessage("Write a short story about a robot learning to paint."),},GenerationConfig:model.GenerationConfig{Stream:true,// Enable streaming output.},}// Handle streaming response.responseChan,err:=llm.GenerateContent(ctx,request)iferr!=nil{returnerr}forresponse:=rangeresponseChan{ifresponse.Error!=nil{fmt.Printf("Error: %s",response.Error.Message)return}iflen(response.Choices)>0&&response.Choices[0].Delta.Content!=""{fmt.Print(response.Choices[0].Delta.Content)}ifresponse.Done{break}}
// Use advanced generation parameters.temperature:=0.3maxTokens:=2000topP:=0.9presencePenalty:=0.2frequencyPenalty:=0.5reasoningEffort:="high"request:=&model.Request{Messages:[]model.Message{model.NewSystemMessage("You are a professional technical documentation writer."),model.NewUserMessage("Explain the advantages and disadvantages of microservice architecture."),},GenerationConfig:model.GenerationConfig{Temperature:&temperature,MaxTokens:&maxTokens,TopP:&topP,PresencePenalty:&presencePenalty,FrequencyPenalty:&frequencyPenalty,ReasoningEffort:&reasoningEffort,Stream:true,},}
// Read image file.imageData,_:=os.ReadFile("image.jpg")// Create multimodal message.request:=&model.Request{Messages:[]model.Message{model.NewSystemMessage("You are an image analysis expert."),{Role:model.RoleUser,ContentParts:[]model.ContentPart{{Type:model.ContentTypeText,Text:stringPtr("What's in this image?"),},{Type:model.ContentTypeImage,Image:&model.Image{Data:imageData,Format:"jpeg",},},},},},}
// Set pre-request callback function.model:=openai.New("deepseek-chat",openai.WithChatRequestCallback(func(ctxcontext.Context,req*openai.ChatCompletionNewParams){// Called before request is sent.log.Printf("Sending request: model=%s, message count=%d",req.Model,len(req.Messages))}),// Set response callback function (non-streaming).openai.WithChatResponseCallback(func(ctxcontext.Context,req*openai.ChatCompletionNewParams,resp*openai.ChatCompletion){// Called when complete response is received.log.Printf("Received response: ID=%s, tokens used=%d",resp.ID,resp.Usage.TotalTokens)}),// Set streaming response callback function.openai.WithChatChunkCallback(func(ctxcontext.Context,req*openai.ChatCompletionNewParams,chunk*openai.ChatCompletionChunk){// Called when each streaming response chunk is received.log.Printf("Received streaming chunk: ID=%s",chunk.ID)}),)