llm

package
v0.0.0-...-c592e1f Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package llm 提供精简版 LLM 健康检查

Package llm 提供统一的 LLM 客户端,支持 OpenAI Chat、OpenAI Responses 和 Anthropic 三种 API 格式

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthCheck

func HealthCheck(ctx context.Context, cfg Config) error

HealthCheck 模型的健康检查 只验证 API 连通性、认证和模型是否存在,不校验回答内容

Types

type ChatRequest

type ChatRequest struct {
	Messages      []Message     `json:"messages"`
	Model         string        `json:"model,omitempty"`
	MaxTokens     int           `json:"max_tokens,omitempty"`
	Temperature   float32       `json:"temperature,omitempty"`
	System        string        `json:"system,omitempty"`
	InterfaceType InterfaceType `json:"interface_type,omitempty"`
}

ChatRequest 聊天请求

type ChatResponse

type ChatResponse struct {
	Content string `json:"content"`
	Usage   Usage  `json:"usage"`
}

ChatResponse 聊天响应

type Client

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

Client LLM 客户端

func NewClient

func NewClient(cfg Config, opts ...ClientOption) *Client

NewClient 创建新的 LLM 客户端

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error)

Chat 发送聊天请求(统一入口)

func (*Client) ChatNoException

func (c *Client) ChatNoException(ctx context.Context, req ChatRequest) (*ChatResponse, error)

ChatNoException 发送聊天请求,出错时返回错误信息而不是抛出异常

type ClientOption

type ClientOption func(*Client)

ClientOption 客户端配置选项

func WithInterfaceType

func WithInterfaceType(t InterfaceType) ClientOption

WithInterfaceType 设置接口类型

type Config

type Config struct {
	BaseURL       string        `json:"base_url,omitempty"`
	APIKey        string        `json:"api_key"`
	Model         string        `json:"model"`
	InterfaceType InterfaceType `json:"interface_type,omitempty"`
}

Config 健康检查配置

type InterfaceType

type InterfaceType string

InterfaceType 定义 API 接口类型

const (
	InterfaceOpenAIChat      InterfaceType = "openai_chat"
	InterfaceOpenAIResponses InterfaceType = "openai_responses"
	InterfaceAnthropic       InterfaceType = "anthropic"
)

type Message

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

Message 聊天消息

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	CachedTokens     int `json:"cached_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage token 使用情况

Jump to

Keyboard shortcuts

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