openai

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIMessage

type APIMessage struct {
	Role             string            `json:"role"`
	Content          any               `json:"content"` // string 或 []schema.ContentPart
	ReasoningContent string            `json:"reasoning_content,omitempty"`
	Name             string            `json:"name,omitempty"`
	ToolCalls        []schema.ToolCall `json:"tool_calls,omitempty"`
	ToolCallID       string            `json:"tool_call_id,omitempty"`
}

APIMessage 发送给 OpenAI 的消息格式

type ChatModel

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

ChatModel OpenAI 兼容聊天模型(实现 chatmodel.BaseModel)

func NewChatModel

func NewChatModel(config *ChatModelConfig) (*ChatModel, error)

NewChatModel 创建 OpenAI 兼容聊天模型

func (*ChatModel) Generate

func (cm *ChatModel) Generate(ctx context.Context, input []*schema.Message) (*schema.Message, error)

func (*ChatModel) GetModelName

func (cm *ChatModel) GetModelName() string

func (*ChatModel) Stream

func (cm *ChatModel) Stream(ctx context.Context, input []*schema.Message) (*schema.StreamReader, error)

type ChatModelConfig

type ChatModelConfig struct {
	BaseURL string
	APIKey  string
	Model   string

	MaxCompletionTokens uint64             `json:"max_completion_tokens,omitempty"`
	ResponseFormat      ResponseFormatType `json:"response_format,omitempty"`
	Stop                string             `json:"stop,omitempty"`
	Temperature         float64            `json:"temperature,omitempty"`
	TopP                float64            `json:"top_p,omitempty"`
	N                   uint8              `json:"n,omitempty"`
	PresencePenalty     float64            `json:"presence_penalty,omitempty"`
	FrequencyPenalty    float64            `json:"frequency_penalty,omitempty"`

	Tools    []schema.Tool `json:"-"`
	Thinking Thinking      `json:"thinking,omitempty"`

	PromptCacheKey   string `json:"prompt_cache_key,omitempty"`
	SafetyIdentifier string `json:"safety_identifier,omitempty"`

	Debug      bool // 启用请求日志(输出到 stderr)
	TimeOut    time.Duration
	HTTPClient *http.Client `json:"-"`
}

ChatModelConfig OpenAI 兼容 API 配置

type ChatModelResponse

type ChatModelResponse struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created int64        `json:"created"`
	Model   string       `json:"model"`
	Choices []Choice     `json:"choices"`
	Usage   schema.Usage `json:"usage"`
}

ChatModelResponse OpenAI 非流式响应

type Choice

type Choice struct {
	Index        int            `json:"index"`
	Message      schema.Message `json:"message"`
	FinishReason string         `json:"finish_reason"`
}

Choice 响应选项

type Client

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

Client OpenAI 兼容 HTTP 客户端

func NewClient

func NewClient(config *ChatModelConfig) *Client

NewClient 创建 OpenAI 兼容客户端

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, in []*schema.Message) (*schema.Message, error)

Generate 非流式生成

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, in []*schema.Message) (*schema.StreamReader, error)

Stream 流式生成

type Delta

type Delta struct {
	Role             string            `json:"role,omitempty"`
	Content          string            `json:"content"`
	ReasoningContent string            `json:"reasoning_content,omitempty"`
	ToolCalls        []schema.ToolCall `json:"tool_calls,omitempty"`
}

Delta 流式增量

type RequestBody

type RequestBody struct {
	Model               string             `json:"model"`
	Messages            []APIMessage       `json:"messages"`
	MaxCompletionTokens uint64             `json:"max_completion_tokens,omitempty"`
	ResponseFormat      ResponseFormatType `json:"response_format,omitempty"`
	Stream              bool               `json:"stream,omitempty"`
	StreamOptions       *StreamOptions     `json:"stream_options,omitempty"`
	Tools               []Tool             `json:"tools,omitempty"`
	PromptCacheKey      string             `json:"prompt_cache_key,omitempty"`
	SafetyIdentifier    string             `json:"safety_identifier,omitempty"`
	Thinking            Thinking           `json:"thinking,omitempty"`
	Temperature         float64            `json:"temperature,omitempty"`
	TopP                float64            `json:"top_p,omitempty"`
	N                   uint8              `json:"n,omitempty"`
	PresencePenalty     float64            `json:"presence_penalty,omitempty"`
	FrequencyPenalty    float64            `json:"frequency_penalty,omitempty"`
}

RequestBody OpenAI API 请求体

type ResponseFormatType

type ResponseFormatType string

ResponseFormatType 响应格式类型

const (
	Text       ResponseFormatType = "text"
	JsonObject ResponseFormatType = "json_object"
)

type StreamChoice

type StreamChoice struct {
	Index        int    `json:"index"`
	Delta        Delta  `json:"delta"`
	FinishReason string `json:"finish_reason"`
}

StreamChoice 流式选项

type StreamOptions

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

StreamOptions 流式响应选项

type StreamResponse

type StreamResponse struct {
	Choices []StreamChoice `json:"choices"`
	Usage   *schema.Usage  `json:"usage,omitempty"`
}

StreamResponse 流式响应

type Thinking

type Thinking struct {
	Type ThinkingType     `json:"type,omitempty"`
	Keep ThinkingKeepEnum `json:"keep,omitempty"`
}

Thinking 思考能力配置

type ThinkingKeepEnum

type ThinkingKeepEnum string

ThinkingKeepEnum 历史思考内容保留策略

const (
	Null ThinkingKeepEnum = "null"
	All  ThinkingKeepEnum = "all"
)

type ThinkingType

type ThinkingType string

ThinkingType 思考能力开关

const (
	Enabled  ThinkingType = "enabled"
	Disabled ThinkingType = "disabled"
)

type Tool

type Tool struct {
	Type     ToolType    `json:"type"`
	Function schema.Tool `json:"function"`
}

Tool API 请求中的工具定义

func WrapTools

func WrapTools(tools []schema.Tool) []Tool

WrapTools 将 schema.Tool 列表包装为 API 请求格式

type ToolType

type ToolType string

ToolType 工具类型

const (
	Function ToolType = "function"
)

Jump to

Keyboard shortcuts

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