client

package
v0.0.0-...-845e3fa Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicClient

type AnthropicClient struct {
	BaseUrl  string
	ApiKey   string
	Model    string
	Provider string
	Thinking bool
	// contains filtered or unexported fields
}

AnthropicClient Anthropic 协议客户端

func NewAnthropicClient

func NewAnthropicClient(config types.Input) *AnthropicClient

NewAnthropicClient 根据配置创建 Anthropic 客户端

重要配置说明:

  • DisableKeepAlives=true: 禁用 HTTP 连接复用,确保每个请求都建立新连接 这对于准确的性能测量至关重要,因为连接复用会跳过 DNS 解析和 TCP 连接建立时间, 导致测量结果不能反映真实的网络性能。在性能基准测试工具中,我们需要测量完整的 网络栈性能,包括 DNS 解析、TCP 连接建立、TLS 握手等。
  • DisableCompression=false: 启用压缩以节省带宽

func (*AnthropicClient) GetModel

func (c *AnthropicClient) GetModel() string

GetModel 获取模型名称

func (*AnthropicClient) GetProtocol

func (c *AnthropicClient) GetProtocol() string

GetProtocol 获取协议类型

func (*AnthropicClient) Request

func (c *AnthropicClient) Request(prompt string, stream bool) (*ResponseMetrics, error)

Request 发送 Anthropic 协议请求(支持流式和非流式)

func (*AnthropicClient) SetLogger

func (c *AnthropicClient) SetLogger(l *logger.Logger)

SetLogger 设置日志记录器

type AnthropicErrorResponse

type AnthropicErrorResponse struct {
	Type  string `json:"type"`
	Error struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error"`
}

AnthropicErrorResponse Anthropic API 错误响应结构

type AnthropicResponse

type AnthropicResponse struct {
	ID      string `json:"id"`
	Type    string `json:"type"`
	Role    string `json:"role"`
	Content []struct {
		Type string `json:"type"`
		Text string `json:"text"`
	} `json:"content"`
	Model string `json:"model"`
	Usage struct {
		InputTokens  int `json:"input_tokens"`
		OutputTokens int `json:"output_tokens"`
	} `json:"usage"`
}

AnthropicResponse Anthropic 非流式响应结构

type AnthropicStreamChunk

type AnthropicStreamChunk struct {
	Type  string `json:"type"`
	Index int    `json:"index,omitempty"`
	Delta struct {
		Type        string  `json:"type"`
		Text        string  `json:"text"`
		Thinking    *string `json:"thinking,omitempty"`
		PartialJSON *string `json:"partial_json,omitempty"`
	} `json:"delta,omitempty"`
	Usage *struct {
		InputTokens  int `json:"input_tokens"`
		OutputTokens int `json:"output_tokens"`
	} `json:"usage,omitempty"`
}

AnthropicStreamChunk Anthropic 流式响应数据块

type ChatCompletionMessage

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

ChatCompletionMessage represents a message in the chat completion request

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model         string                  `json:"model"`
	Messages      []ChatCompletionMessage `json:"messages"`
	Stream        bool                    `json:"stream,omitempty"`
	StreamOptions *StreamOptions          `json:"stream_options,omitempty"`
	Thinking      *ThinkingOptions        `json:"thinking,omitempty"`
}

ChatCompletionRequest represents the request payload for chat completion

type ChatCompletionResponse

type ChatCompletionResponse 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 struct {
		PromptTokens            int                      `json:"prompt_tokens"`
		CompletionTokens        int                      `json:"completion_tokens"`
		TotalTokens             int                      `json:"total_tokens"`
		CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
	} `json:"usage"`
}

ChatCompletionResponse represents the response from chat completion

type CompletionTokensDetails

type CompletionTokensDetails struct {
	ReasoningTokens int `json:"reasoning_tokens"`
	ThinkingTokens  int `json:"thinking_tokens"`
}

CompletionTokensDetails represents detailed completion token usage breakdown

type ModelClient

type ModelClient interface {
	Request(prompt string, stream bool) (*ResponseMetrics, error)
	GetProtocol() string
	GetModel() string
	SetLogger(logger *logger.Logger) // 设置日志记录器
}

ModelClient 定义统一的模型客户端接口

func NewClient

func NewClient(config types.Input, logger *logger.Logger) (ModelClient, error)

NewClient 根据配置创建客户端

type OpenAIClient

type OpenAIClient struct {
	Model    string
	Provider string
	Thinking bool // 是否开启 thinking 模式
	// contains filtered or unexported fields
}

OpenAIClient OpenAI 协议客户端

func NewOpenAIClient

func NewOpenAIClient(config types.Input) *OpenAIClient

NewOpenAIClient 根据配置创建 OpenAI 客户端

重要配置说明:

  • DisableKeepAlives=true: 禁用 HTTP 连接复用,确保每个请求都建立新连接 这对于准确的性能测量至关重要,因为连接复用会跳过 DNS 解析和 TCP 连接建立时间, 导致测量结果不能反映真实的网络性能。在性能基准测试工具中,我们需要测量完整的 网络栈性能,包括 DNS 解析、TCP 连接建立、TLS 握手等。
  • DisableCompression=false: 启用压缩以节省带宽

func (*OpenAIClient) GetModel

func (c *OpenAIClient) GetModel() string

GetModel 获取模型名称

func (*OpenAIClient) GetProtocol

func (c *OpenAIClient) GetProtocol() string

GetProtocol 获取协议类型

func (*OpenAIClient) Request

func (c *OpenAIClient) Request(prompt string, stream bool) (*ResponseMetrics, error)

Request 发送 OpenAI 协议请求(支持流式和非流式)

func (*OpenAIClient) SetLogger

func (c *OpenAIClient) SetLogger(l *logger.Logger)

SetLogger 设置日志记录器

type OpenAIErrorResponse

type OpenAIErrorResponse struct {
	Error struct {
		Message string `json:"message"`
		Type    string `json:"type"`
		Param   string `json:"param,omitempty"`
		Code    string `json:"code,omitempty"`
	} `json:"error"`
}

OpenAIErrorResponse represents OpenAI API error response

type ResponseMetrics

type ResponseMetrics struct {
	// 时间相关指标
	TimeToFirstToken time.Duration // 首个 token 的响应时间 (TTFT)
	TotalTime        time.Duration // 总耗时 (从请求开始到完全结束)

	// 网络连接指标
	DNSTime          time.Duration // DNS解析时间
	ConnectTime      time.Duration // TCP连接建立时间
	TLSHandshakeTime time.Duration // TLS握手时间
	TargetIP         string        // 目标服务器IP地址

	// 内容指标
	PromptTokens     int // 输入 token 数量
	ThinkingTokens   int // 思考/推理 token 数量
	CompletionTokens int // 输出 token 数量 (用于TPS计算)

	// 错误信息
	ErrorMessage string // 错误信息(如果有)
}

ResponseMetrics 响应指标数据

type StreamOptions

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

StreamOptions represents stream options for chat completion

type StreamResponseChunk

type StreamResponseChunk struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Index int `json:"index"`
		Delta struct {
			ThinkingContent *string `json:"reasoning_content,omitempty"`
			Content         string  `json:"content"`
		} `json:"delta"`
		FinishReason *string `json:"finish_reason"`
	} `json:"choices"`
	Usage *struct {
		PromptTokens            int                      `json:"prompt_tokens"`
		CompletionTokens        int                      `json:"completion_tokens"`
		TotalTokens             int                      `json:"total_tokens"`
		CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
	} `json:"usage,omitempty"`
}

StreamResponseChunk 流式响应数据块

type ThinkingOptions

type ThinkingOptions struct {
	Type string `json:"type"`
}

ThinkingOptions represents reasoning options for chat completion

Jump to

Keyboard shortcuts

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