llmkit

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 21 Imported by: 2 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

llmkit

中文文档

llmkit is a zero-dependency Go library for working with OpenAI Chat Completions, OpenAI Responses, Anthropic Claude Messages, and Google Gemini. Applications and agents can use Client to call any provider through a single request and streaming event type, while gateways and proxies can use Codec for any-to-any request and SSE stream translation with multi-turn reasoning, tool calls, and provider signatures preserved.

Capabilities

  • Unified Client (Client): Send a protocol-neutral Request to any supported provider and consume a normalized Event stream.
  • Any-to-Any Protocol Translation (Codec): Decode inbound HTTP requests into Request, encode them for another target protocol, decode upstream responses into Event streams, and re-encode them for the caller's protocol.
  • Multi-Turn Reasoning & Tool Signatures: Preserve reasoning summaries, encrypted reasoning payloads, tool-call IDs, and provider-specific tool signatures across turns and protocols.
  • Zero Production Dependencies: Production packages depend only on the Go standard library.

Installation

go get github.com/VaalaCat/llmkit

Quick Start

Calling a Model with Client
package main

import (
	"context"
	"fmt"

	"github.com/VaalaCat/llmkit"
)

func main() {
	client := llmkit.NewClient(llmkit.ClientOptions{})
	events, err := client.Call(
		context.Background(),
		llmkit.Request{
			Stream: true,
			Messages: []llmkit.Message{{
				Role: llmkit.RoleUser,
				Content: []llmkit.ContentBlock{{
					Type: llmkit.ContentTypeText,
					Text: "Hello!",
				}},
			}},
		},
		llmkit.Target{
			Protocol: llmkit.ProtocolOpenAIResponses,
			BaseURL:  "https://api.openai.com",
			APIKey:   "sk-...",
			Model:    "gpt-5",
		},
		llmkit.CallOptions{},
	)
	if err != nil {
		panic(err)
	}
	for event := range events {
		if event.Type == llmkit.EventContentDelta && event.Delta != nil {
			fmt.Print(event.Delta.Text)
		}
	}
}
Translating Protocols with Codec
codec := llmkit.NewCodec()

decoded, err := codec.DecodeRequest(llmkit.DecodeRequestInput{
	Method: "POST",
	Path:   llmkit.RouteResponses,
	Body:   inboundBody,
})
if err != nil {
	panic(err)
}

encoded, err := codec.EncodeRequest(llmkit.EncodeRequestInput{
	Request: decoded.Request,
	Target: llmkit.Target{
		Protocol: llmkit.ProtocolGemini,
		Model:    "gemini-2.5-pro",
	},
})

Documentation

Overview

Package llmkit provides three public entry points for LLM access: construct protocol-neutral Request and Event IR directly, use Codec to translate raw protocol payloads, or use Client for end-to-end HTTP calls. Stream failures that happen after Client.Call returns are delivered as an EventError followed by channel close. ErrorStageStream is available for stream-stage failures surfaced before a channel is returned; it does not introduce a second error channel.

Package llmkit provides the public LLM access API.

Index

Constants

View Source
const (
	HeaderAuthorization    = ir.HeaderAuthorization
	HeaderContentType      = ir.HeaderContentType
	HeaderConnection       = ir.HeaderConnection
	HeaderCacheControl     = ir.HeaderCacheControl
	HeaderOpenAIOrg        = ir.HeaderOpenAIOrg
	HeaderXAPIKey          = ir.HeaderXAPIKey
	HeaderAnthropicVersion = ir.HeaderAnthropicVersion

	ContentTypeJSON       = ir.ContentTypeJSON
	ContentTypeSSE        = ir.ContentTypeSSE
	BearerPrefix          = ir.BearerPrefix
	CacheControlNoCache   = ir.CacheControlNoCache
	ConnectionKeepAlive   = ir.ConnectionKeepAlive
	AnthropicVersionValue = ir.AnthropicVersionValue
)
View Source
const (
	RouteChatCompletions = ir.RouteChatCompletions
	RouteResponses       = ir.RouteResponses
	RouteMessages        = ir.RouteMessages
)
View Source
const (
	FinishReasonStop          = ir.FinishReasonStop
	FinishReasonLength        = ir.FinishReasonLength
	FinishReasonToolCalls     = ir.FinishReasonToolCalls
	FinishReasonContentFilter = ir.FinishReasonContentFilter

	ClaudeStopEndTurn   = ir.ClaudeStopEndTurn
	ClaudeStopMaxTokens = ir.ClaudeStopMaxTokens
	ClaudeStopToolUse   = ir.ClaudeStopToolUse
)
View Source
const (
	MessageStart      = ir.MessageStart
	ContentBlockStart = ir.ContentBlockStart
	ContentBlockDelta = ir.ContentBlockDelta
	ContentBlockStop  = ir.ContentBlockStop
	MessageDelta      = ir.MessageDelta
	MessageStop       = ir.MessageStop

	ClaudeTextDelta      = ir.ClaudeTextDelta
	ClaudeInputJSONDelta = ir.ClaudeInputJSONDelta
	ClaudeThinkingDelta  = ir.ClaudeThinkingDelta
	ClaudeSignatureDelta = ir.ClaudeSignatureDelta
)
View Source
const (
	ResponseCreated            = ir.ResponseCreated
	ResponseInProgress         = ir.ResponseInProgress
	ResponseCompleted          = ir.ResponseCompleted
	ResponseFailed             = ir.ResponseFailed
	ResponseIncomplete         = ir.ResponseIncomplete
	OutputItemAdded            = ir.OutputItemAdded
	OutputItemDone             = ir.OutputItemDone
	ContentPartAdded           = ir.ContentPartAdded
	ContentPartDelta           = ir.ContentPartDelta
	ContentPartDone            = ir.ContentPartDone
	OutputTextDelta            = ir.OutputTextDelta
	OutputTextDone             = ir.OutputTextDone
	ReasoningTextDelta         = ir.ReasoningTextDelta
	ReasoningTextDone          = ir.ReasoningTextDone
	FunctionCallArgumentsDelta = ir.FunctionCallArgumentsDelta
	FunctionCallArgumentsDone  = ir.FunctionCallArgumentsDone
	RefusalDelta               = ir.RefusalDelta

	ChatStreamDone = ir.ChatStreamDone
)
View Source
const (
	BuiltinToolFallbackDrop        = convert.BuiltinToolFallbackDrop
	BuiltinToolFallbackError       = convert.BuiltinToolFallbackError
	BuiltinToolFallbackPassthrough = convert.BuiltinToolFallbackPassthrough
	BuiltinToolFallbackFunction    = convert.BuiltinToolFallbackFunction
)
View Source
const (
	DroppedToolReasonCrossProtocolIncompatible   = convert.DroppedToolReasonCrossProtocolIncompatible
	DroppedToolReasonFunctionFallbackUnsupported = convert.DroppedToolReasonFunctionFallbackUnsupported
)
View Source
const (
	ToolSignatureFallbackNone   = convert.ToolSignatureFallbackNone
	ToolSignatureFallbackGemini = convert.ToolSignatureFallbackGemini
)
View Source
const (
	ProtocolOpenAIChat      = ir.ProtocolOpenAIChat
	ProtocolOpenAIResponses = ir.ProtocolOpenAIResponses
	ProtocolClaudeMessages  = ir.ProtocolClaudeMessages
	ProtocolClaude          = ir.ProtocolClaude
	ProtocolGemini          = ir.ProtocolGemini
	ProtocolUnknown         = ir.ProtocolUnknown

	RoleSystem    = ir.RoleSystem
	RoleUser      = ir.RoleUser
	RoleAssistant = ir.RoleAssistant
	RoleTool      = ir.RoleTool
	RoleDeveloper = ir.RoleDeveloper

	ContentTypeText       = ir.ContentTypeText
	ContentTypeImage      = ir.ContentTypeImage
	ContentTypeAudio      = ir.ContentTypeAudio
	ContentTypeToolUse    = ir.ContentTypeToolUse
	ContentTypeToolResult = ir.ContentTypeToolResult
	ContentTypeThinking   = ir.ContentTypeThinking
	ContentTypeInputText  = ir.ContentTypeInputText
	ContentTypeOutputText = ir.ContentTypeOutputText
	ContentTypeImageURL   = ir.ContentTypeImageURL
	ContentTypeFunction   = ir.ContentTypeFunction

	ImageSourceBase64 = ir.ImageSourceBase64
	ImageSourceURL    = ir.ImageSourceURL

	EventStreamStart            = ir.EventStreamStart
	EventContentDelta           = ir.EventContentDelta
	EventContentBlock           = ir.EventContentBlock
	EventToolCallDelta          = ir.EventToolCallDelta
	EventThinkingDelta          = ir.EventThinkingDelta
	EventUsage                  = ir.EventUsage
	EventDone                   = ir.EventDone
	EventError                  = ir.EventError
	EventRawPassthrough         = ir.EventRawPassthrough
	EventContentBlockStop       = ir.EventContentBlockStop
	EventSignatureDelta         = ir.EventSignatureDelta
	EventToolCallStart          = ir.EventToolCallStart
	EventToolCallArgumentsDelta = ir.EventToolCallArgumentsDelta
	EventToolCallEnd            = ir.EventToolCallEnd
	EventReasoningSummaryDelta  = ir.EventReasoningSummaryDelta
	EventReasoningContentDelta  = ir.EventReasoningContentDelta
	EventReasoningDone          = ir.EventReasoningDone

	ReasoningCompleted   = ir.ReasoningCompleted
	ReasoningInterrupted = ir.ReasoningInterrupted

	ReasoningContextUnknown     = ir.ReasoningContextUnknown
	ReasoningContextCurrentTurn = ir.ReasoningContextCurrentTurn
	ReasoningContextAllTurns    = ir.ReasoningContextAllTurns
)

Variables

View Source
var (
	ErrUnsupportedEndpoint = errors.New("llmkit: unsupported endpoint")
	ErrUnsupportedProtocol = errors.New("llmkit: unsupported protocol")
	ErrNilDecodedRequest   = errors.New("llmkit: protocol handler returned a nil request")
)
View Source
var (
	ErrFunctionToolMissingName            = convert.ErrFunctionToolMissingName
	ErrBuiltinToolUnsupported             = convert.ErrBuiltinToolUnsupported
	ErrNamespacedFunctionMissingNamespace = convert.ErrNamespacedFunctionMissingNamespace
	ErrNamespacedFunctionMissingName      = convert.ErrNamespacedFunctionMissingName
	ErrNamespacedFunctionNotFunction      = convert.ErrNamespacedFunctionNotFunction
	ErrNamespacedFunctionNameCollision    = convert.ErrNamespacedFunctionNameCollision
)

Functions

func ConvertToolSchemas

func ConvertToolSchemas(tools []Tool, profile ToolSchemaProfile) ([]Tool, []ToolSchemaChange)

ConvertToolSchemas 生成兼容后的工具定义副本,并列出被移除的选项和约束。

Types

type BuiltinToolFallbackPolicy

type BuiltinToolFallbackPolicy = convert.BuiltinToolFallbackPolicy

func NormalizeBuiltinToolFallback

func NormalizeBuiltinToolFallback(value string) BuiltinToolFallbackPolicy

type CallOptions

type CallOptions struct {
	HTTPClient HTTPDoer
	Conversion ConversionOptions
}

type Client

type Client interface {
	Call(context.Context, Request, Target, CallOptions) (<-chan Event, error)
}

func NewClient

func NewClient(options ClientOptions) Client

type ClientOptions

type ClientOptions struct {
	Codec      Codec
	HTTPClient HTTPDoer
}

type Codec

type Codec interface {
	DecodeRequest(DecodeRequestInput) (DecodedRequest, error)
	EncodeRequest(EncodeRequestInput) (EncodedRequest, error)
	DecodeResponse(context.Context, DecodeResponseInput) (<-chan Event, error)
	EncodeResponse(context.Context, EncodeResponseInput) (<-chan EncodedChunk, error)
}

func NewCodec

func NewCodec() Codec

type ContentBlock

type ContentBlock = ir.ContentBlock

type ContentType

type ContentType = ir.ContentType

type ConversionOptions

type ConversionOptions struct {
	GeminiGeneration      GeminiGenerationConfig
	ToolSignatureFallback ToolSignatureFallback // 这一轮没有可用签名时补什么:none / gemini
	BuiltinToolFallback   BuiltinToolFallbackPolicy
	RequestFields         RequestFieldPermissions
	OnDroppedTools        func([]DroppedTool)
	// MergeMessageRoles lists the roles whose adjacent messages are merged into one before encoding:
	// Content and ToolCalls are concatenated in order. Messages carrying ToolCallID or RawJSON never merge
	// and break adjacency.
	MergeMessageRoles []Role
}

type ConversionState

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

type DecodeRequestInput

type DecodeRequestInput struct {
	Method  string
	Path    string
	Headers map[string][]string
	Body    []byte
}

type DecodeResponseInput

type DecodeResponseInput struct {
	Protocol   Protocol
	StatusCode int
	Headers    map[string][]string
	Body       io.Reader
	Stream     bool
	State      ConversionState
}

type DecodedRequest

type DecodedRequest struct {
	Protocol Protocol
	Request  Request
}

type DeltaPayload

type DeltaPayload = ir.DeltaPayload

type DroppedTool

type DroppedTool = convert.DroppedTool

type EncodeRequestInput

type EncodeRequestInput struct {
	Request Request
	Target  Target
	Options ConversionOptions
}

type EncodeResponseInput

type EncodeResponseInput struct {
	Protocol Protocol
	Events   <-chan Event
	Stream   bool
}

type EncodedChunk

type EncodedChunk struct {
	Data []byte
	Err  error
}

type EncodedRequest

type EncodedRequest struct {
	Method  string
	Path    string
	Headers map[string][]string
	Body    []byte
	State   ConversionState
}

type Error

type Error struct {
	Stage      ErrorStage
	StatusCode int
	Retryable  bool
	Cause      error
	// contains filtered or unexported fields
}

func (*Error) Error

func (err *Error) Error() string

func (*Error) Unwrap

func (err *Error) Unwrap() error

type ErrorPayload

type ErrorPayload = ir.ErrorPayload

type ErrorStage

type ErrorStage string
const (
	ErrorStageEncode   ErrorStage = "encode"
	ErrorStageConnect  ErrorStage = "connect"
	ErrorStageUpstream ErrorStage = "upstream"
	ErrorStageDecode   ErrorStage = "decode"
	// ErrorStageStream is available to callers that surface a stream failure
	// before returning an event channel. Once a channel exists, stream failures
	// remain EventError followed by channel close.
	ErrorStageStream ErrorStage = "stream"
)

type Event

type Event = ir.Event

type EventType

type EventType = ir.EventType

type GeminiGenerationConfig

type GeminiGenerationConfig = convert.GeminiGenerationConfig

type HTTPDoer

type HTTPDoer interface {
	Do(*http.Request) (*http.Response, error)
}

type Message

type Message = ir.Message

type Protocol

type Protocol = ir.Protocol

type ProviderData

type ProviderData = ir.ProviderData

type RawSSEEvent

type RawSSEEvent = ir.RawSSEEvent

type ReasoningContent

type ReasoningContent = ir.ReasoningContent

type ReasoningContext

type ReasoningContext = ir.ReasoningContext

ReasoningContext 是思考上下文模式:在请求中表示调用方指定的模式,在响应事件中表示服务端报告的实际模式。

type ReasoningStatus

type ReasoningStatus = ir.ReasoningStatus

type Request

type Request = ir.Request

type RequestFieldPermissions

type RequestFieldPermissions = convert.RequestFieldPermissions

func DefaultRequestFieldPermissions

func DefaultRequestFieldPermissions() RequestFieldPermissions

type Role

type Role = ir.Role

type StreamingToolCall

type StreamingToolCall = ir.StreamingToolCall

type Target

type Target struct {
	Protocol     Protocol
	BaseURL      string
	EndpointPath string
	APIKey       string
	Model        string
	Headers      map[string][]string
}

type Tool

type Tool = ir.Tool

type ToolCall

type ToolCall = ir.ToolCall

type ToolCallDelta

type ToolCallDelta = ir.ToolCallDelta

type ToolCallProviderData

type ToolCallProviderData = ir.ToolCallProviderData

type ToolCallRequestInfo

type ToolCallRequestInfo struct {
	HasTools            bool // 请求里有工具定义
	HasToolHistory      bool // 历史里有工具调用或工具结果
	HasPackedToolCallID bool // 历史里有 call_llmkit_ 开头的工具调用 ID
}

ToolCallRequestInfo 描述请求里和工具调用有关的三个特征。

func InspectToolCallRequest

func InspectToolCallRequest(protocol Protocol, body []byte) (ToolCallRequestInfo, error)

InspectToolCallRequest 读取请求里的工具定义、工具历史和打包的工具调用 ID,不做协议转换。

type ToolChoice

type ToolChoice = ir.ToolChoice

type ToolSchemaChange

type ToolSchemaChange struct {
	ToolName string `json:"tool_name"`
	Path     string `json:"path"`
}

ToolSchemaChange 记录被移除字段的工具名和 IR JSON Pointer 路径。

type ToolSchemaProfile

type ToolSchemaProfile string

ToolSchemaProfile 指定发送给上游的工具定义兼容方式。

const (
	ToolSchemaProfileNone         ToolSchemaProfile = "none"
	ToolSchemaProfileGeminiCompat ToolSchemaProfile = "gemini_compat"
)

type ToolSignatureFallback

type ToolSignatureFallback = convert.ToolSignatureFallback

ToolSignatureFallback 是一轮工具调用没有可用签名时补什么。

type Usage

type Usage = ir.Usage

Directories

Path Synopsis
Package endpoint 解析和展开模型 API 的动态请求路径。
Package endpoint 解析和展开模型 API 的动态请求路径。
internal
protocol/claude
Package claude implements the Anthropic Claude Messages API protocol codec.
Package claude implements the Anthropic Claude Messages API protocol codec.
protocol/gemini
Package gemini 转换 Gemini 原生生成请求与响应。
Package gemini 转换 Gemini 原生生成请求与响应。
protocol/openai/chat
Package chat implements the OpenAI Chat Completions protocol codec.
Package chat implements the OpenAI Chat Completions protocol codec.
protocol/openai/responses
Package responses implements the OpenAI Responses API protocol codec.
Package responses implements the OpenAI Responses API protocol codec.
Package ir defines the protocol-agnostic intermediate representation used by LLM protocol codecs.
Package ir defines the protocol-agnostic intermediate representation used by LLM protocol codecs.

Jump to

Keyboard shortcuts

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