llm

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package llm provides shared LLM data types for agent-go consumers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAnthropicRequest

func BuildAnthropicRequest(req *ChatRequest) map[string]any

func BuildGeminiRequest

func BuildGeminiRequest(req *ChatRequest) map[string]any

func BuildOpenAIRequest

func BuildOpenAIRequest(req *ChatRequest) (map[string]any, error)

BuildOpenAIRequest 构建 OpenAI 兼容的非流式请求体

func BuildOpenAIStreamRequest

func BuildOpenAIStreamRequest(req *ChatRequest, tools []OpenAIToolDefinition) (map[string]any, error)

BuildOpenAIStreamRequest 构建流式请求体

Types

type AnthropicMessage

type AnthropicMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type ChatMessage

type ChatMessage struct {
	Role, Content, ReasoningContent, ToolCallID string
	ToolCalls                                   []ToolCallInfo
}

type ChatRequest

type ChatRequest struct {
	ProviderID, Model string
	Messages          []ChatMessage
	Temperature       float64
	Stream            bool
	Thinking          string
	Tools             bool
	ContextWindow     int
}

type ChatResponse

type ChatResponse struct {
	Content, ReasoningContent, Model string
	Usage                            *Usage
}

func ParseAnthropicResponse

func ParseAnthropicResponse(body []byte) (*ChatResponse, error)

func ParseGeminiResponse

func ParseGeminiResponse(body []byte) (*ChatResponse, error)

func ParseOpenAIResponse

func ParseOpenAIResponse(body []byte) (*ChatResponse, error)

type GeminiContent

type GeminiContent struct {
	Role  string `json:"role"`
	Parts []struct {
		Text string `json:"text"`
	} `json:"parts"`
}

type LLMService added in v0.2.0

type LLMService struct {
	// contains filtered or unexported fields
}

LLMService provides multi-provider LLM chat and model listing. It supports OpenAI-compatible, Anthropic, and Gemini providers.

func NewLLMService added in v0.2.0

func NewLLMService(timeout time.Duration) *LLMService

NewLLMService creates an LLMService with configurable timeout.

func (*LLMService) Chat added in v0.2.0

func (s *LLMService) Chat(req *ChatRequest) (*ChatResponse, error)

Chat performs a non-streaming chat completion.

func (*LLMService) ChatStream added in v0.2.0

func (s *LLMService) ChatStream(
	req *ChatRequest,
	onContent func(string),
	onReasoning func(string),
	onToolCallStart func(string, string),
	onToolCallDelta func(string, string),
	onToolCallEnd func(string, string, string),
	onDone func(),
	onError func(error),
)

ChatStream performs a streaming chat completion with delta callbacks. onContent: called for each content delta. onReasoning: called for each reasoning_content delta. onToolCallStart: called when a tool call begins. onToolCallDelta: called for each tool call arguments delta. onToolCallEnd: called when a tool call completes. onDone: called when the stream completes. onError: called when an error occurs.

func (*LLMService) ListModels added in v0.2.0

func (s *LLMService) ListModels(baseURL, apiKey string) ([]ModelInfo, error)

ListModels fetches available models from an OpenAI-compatible endpoint.

type ModelInfo

type ModelInfo struct {
	ID        string
	CreatedAt int64
	OwnedBy   string
}

type OpenAIChatMessage

type OpenAIChatMessage struct {
	Role             string         `json:"role"`
	Content          string         `json:"content"`
	ReasoningContent string         `json:"reasoning_content,omitempty"`
	ToolCallID       string         `json:"tool_call_id,omitempty"`
	ToolCalls        []ToolCallInfo `json:"tool_calls,omitempty"`
}

type OpenAIChatRequest

type OpenAIChatRequest struct {
	Model         string                 `json:"model"`
	Messages      []OpenAIChatMessage    `json:"messages"`
	Temperature   float64                `json:"temperature,omitempty"`
	Stream        bool                   `json:"stream,omitempty"`
	StreamOptions *OpenAIStreamOptions   `json:"stream_options,omitempty"`
	Tools         []OpenAIToolDefinition `json:"tools,omitempty"`
}

type OpenAIChatResponse

type OpenAIChatResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Index   int `json:"index"`
		Message struct {
			Role    string `json:"role"`
			Content string `json:"content"`
		} `json:"message"`
		FinishReason string `json:"finish_reason"`
	} `json:"choices"`
	Usage *Usage `json:"usage,omitempty"`
}

type OpenAIFunctionDef

type OpenAIFunctionDef struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Parameters  any    `json:"parameters"`
}

type OpenAIStreamOptions

type OpenAIStreamOptions struct {
	IncludeUsage bool `json:"include_usage"`
}

type OpenAIToolCallDelta

type OpenAIToolCallDelta struct {
	Index    int    `json:"index"`
	ID       string `json:"id,omitempty"`
	Type     string `json:"type,omitempty"`
	Function *struct {
		Name      string `json:"name,omitempty"`
		Arguments string `json:"arguments,omitempty"`
	} `json:"function,omitempty"`
}

type OpenAIToolDefinition

type OpenAIToolDefinition struct {
	Type     string            `json:"type"`
	Function OpenAIFunctionDef `json:"function"`
}

type StreamEvent

type StreamEvent string
const (
	StreamEventContent   StreamEvent = "content"
	StreamEventReasoning StreamEvent = "reasoning"
	StreamEventToolCall  StreamEvent = "tool_call"
	StreamEventDone      StreamEvent = "done"
	StreamEventError     StreamEvent = "error"
)

type ToolCall

type ToolCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

ToolCall represents a tool call with name and arguments.

type ToolCallInfo

type ToolCallInfo struct {
	ID, Type string
	Function struct{ Name, Arguments string }
}

type Usage

type Usage struct{ PromptTokens, CompletionTokens, TotalTokens int }

Jump to

Keyboard shortcuts

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