model

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package model 提供 OpenAI-Compatible 模型调用(DeepSeek / 月之暗面 / 通义 / 自托管 等)。

范围:M1 只需要 Complete(非流式 + tool_calls)。后续如需流式,可平移用户既有的 CompleteStream 实现(同一组类型)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Type       string
	Message    string
	Body       string
}

APIError 携带 HTTP 状态码 + 上游 body —— 调用方据此决定重试/退避策略。

func (*APIError) Error

func (e *APIError) Error() string

type Completer

type Completer interface {
	Complete(ctx context.Context, req Request) (Response, error)
}

Completer 抽象出 Complete,便于注入测试用假实现做 hermetic 测试。 *OpenAICompatibleProvider 满足它。

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // JSON 字符串
}

type FunctionSchema

type FunctionSchema struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters"` // JSON Schema(type=object + properties + required)
}

type Message

type Message struct {
	Role       string     `json:"role"`
	Content    string     `json:"content,omitempty"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`   // assistant 消息携带模型决定要调的工具
	ToolCallID string     `json:"tool_call_id,omitempty"` // tool 消息:对应哪一次工具调用
	Name       string     `json:"name,omitempty"`
}

Message 一条对话消息。role: system / user / assistant / tool。

type OpenAICompatibleProvider

type OpenAICompatibleProvider struct {
	BaseURL    string
	APIKey     string
	HTTPClient *http.Client
}

OpenAICompatibleProvider 调用任何 OpenAI-Compatible /chat/completions 端点 (DeepSeek / Moonshot / 通义 / 自托管 vLLM 等)。M1 只用 Complete(非流式)。

func NewOpenAICompatibleProvider

func NewOpenAICompatibleProvider(baseURL, apiKey string) *OpenAICompatibleProvider

func (*OpenAICompatibleProvider) Complete

func (p *OpenAICompatibleProvider) Complete(ctx context.Context, req Request) (Response, error)

type Request

type Request struct {
	Model       string           `json:"-"`
	Messages    []Message        `json:"messages"`
	Temperature float64          `json:"temperature,omitempty"`
	Tools       []ToolDefinition `json:"tools,omitempty"`
	// ToolChoice 可为字符串("auto"/"none"/"required")或对象
	// ({"type":"function","function":{"name":"X"}} 强制调用某个函数)。
	ToolChoice any `json:"tool_choice,omitempty"`
}

type Response

type Response struct {
	Content      string
	ToolCalls    []ToolCall
	FinishReason string
	Usage        Usage
	Raw          []byte // 调试用;不必常驻
}

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"` // 目前固定 "function"
	Function FunctionCall `json:"function"`
}

ToolCall 模型一次工具调用(assistant 消息里的 tool_calls 元素)。

type ToolDefinition

type ToolDefinition struct {
	Type     string         `json:"type"` // "function"
	Function FunctionSchema `json:"function"`
}

ToolDefinition 我们告诉模型可用的工具。

type Usage

type Usage struct {
	PromptTokens       int
	CompletionTokens   int
	TotalTokens        int
	CachedPromptTokens int
}

Jump to

Keyboard shortcuts

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