Files
go-llm/v2/message.go
Steve Dudenhoeffer a4cb4baab5 Add go-llm v2: redesigned API for simpler LLM abstraction
v2 is a new Go module (v2/) with a dramatically simpler API:
- Unified Message type (no more Input marker interface)
- Define[T] for ergonomic tool creation with standard context.Context
- Chat session with automatic tool-call loop (agent loop)
- Streaming via pull-based StreamReader
- MCP one-call connect (MCPStdioServer, MCPHTTPServer, MCPSSEServer)
- Middleware support (logging, retry, timeout, usage tracking)
- Decoupled JSON Schema (map[string]any, no provider coupling)
- Sample tools: WebSearch, Browser, Exec, ReadFile, WriteFile, HTTP
- Providers: OpenAI, Anthropic, Google (all with streaming)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 20:00:08 -05:00

74 lines
2.0 KiB
Go

package llm
// Role represents who authored a message.
type Role string
const (
RoleSystem Role = "system"
RoleUser Role = "user"
RoleAssistant Role = "assistant"
RoleTool Role = "tool"
)
// Image represents an image attachment.
type Image struct {
// Provide exactly one of URL or Base64.
URL string // HTTP(S) URL
Base64 string // Raw base64-encoded data
ContentType string // MIME type (e.g., "image/png"), required for Base64
}
// Content represents message content with optional text and images.
type Content struct {
Text string
Images []Image
}
// ToolCall represents a tool invocation requested by the assistant.
type ToolCall struct {
ID string
Name string
Arguments string // raw JSON
}
// Message represents a single message in a conversation.
type Message struct {
Role Role
Content Content
// ToolCallID is set when Role == RoleTool, identifying which tool call this responds to.
ToolCallID string
// ToolCalls is set when the assistant requests tool invocations.
ToolCalls []ToolCall
}
// UserMessage creates a user message with text content.
func UserMessage(text string) Message {
return Message{Role: RoleUser, Content: Content{Text: text}}
}
// UserMessageWithImages creates a user message with text and images.
func UserMessageWithImages(text string, images ...Image) Message {
return Message{Role: RoleUser, Content: Content{Text: text, Images: images}}
}
// SystemMessage creates a system prompt message.
func SystemMessage(text string) Message {
return Message{Role: RoleSystem, Content: Content{Text: text}}
}
// AssistantMessage creates an assistant message with text content.
func AssistantMessage(text string) Message {
return Message{Role: RoleAssistant, Content: Content{Text: text}}
}
// ToolResultMessage creates a tool result message.
func ToolResultMessage(toolCallID string, result string) Message {
return Message{
Role: RoleTool,
Content: Content{Text: result},
ToolCallID: toolCallID,
}
}