llm

package
v0.0.0-...-f17429d Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderOpenAI    = "openai"
	ProviderOllama    = "ollama"
	ProviderAlibaba   = "alibaba"
	ProviderAnthropic = "anthropic"
	ProviderLMStudio  = "lmstudio"
	ProviderCoze      = "coze"
)
View Source
const (
	LLM_OPENAI    = "llm.openai"
	LLM_ANTHROPIC = "llm.anthropic"
	LLM_COZE      = "llm.coze"
	LLM_OLLAMA    = "llm.ollama"
	LLM_LMSTUDIO  = "llm.lmstudio"
	LLM_ALIBABA   = "llm.alibaba"
)
View Source
const (
	OutputFormatText       = "text"
	OutputFormatJSON       = "json"
	OutputFormatJSONObject = "json_object"
	OutputFormatJSONSchema = "json_schema"
	OutputFormatXML        = "xml"
	OutputFormatHTML       = "html"
	OutputFormatSQL        = "sql"
)
View Source
const (
	// LLM调用相关信号
	SignalLLMRequestStart = "llm.request.start"
	SignalLLMRequestEnd   = "llm.request.end"
	SignalLLMRequestError = "llm.request.error"
	// SignalLLMUsage 单次 LLM 调用用量与延迟落库前广播;sender 为 *LLMUsageSignalPayload(见 tracking)。
	SignalLLMUsage = "llm.usage"

	// 会话相关信号
	SignalSessionCreated = "session.created"
	SignalSessionUpdated = "session.updated"
	SignalSessionDeleted = "session.deleted"

	// 消息相关信号
	SignalMessageCreated = "message.created"
	SignalMessageUpdated = "message.updated"
	SignalMessageDeleted = "message.deleted"
)

信号类型定义

Variables

This section is empty.

Functions

func BuildQueryExpansionUserPrompt

func BuildQueryExpansionUserPrompt(text string, maxTerms int) string

BuildQueryExpansionUserPrompt builds the user message for one-shot query expansion.

func BuildQueryRewriteUserPrompt

func BuildQueryRewriteUserPrompt(userQuery string, extraInstruction string) string

BuildQueryRewriteUserPrompt builds a one-shot user message for LLM-based query rewrite.

func ClipOpenAPIUsageBody

func ClipOpenAPIUsageBody(s string) string

ClipOpenAPIUsageBody 裁剪大段 JSON/文本,供用量审计或其它落库复用。

func CopyOpenAPIProxyResponseHeaders

func CopyOpenAPIProxyResponseHeaders(dst http.Header, src http.Header)

CopyOpenAPIProxyResponseHeaders 复制上游响应头(跳过逐跳头)。

func CreateMessage

func CreateMessage(messageID, sessionID, role, content string, tokenCount int, model, provider, requestID string)

CreateMessage 创建消息并发送信号

func CreateSession

func CreateSession(sessionID, userID, title, provider, model, systemPrompt string)

CreateSession 创建会话并发送信号

func EmitOpenAPIAnthropicUsageFailure

func EmitOpenAPIAnthropicUsageFailure(reqBody []byte, res *OpenAPIProxyResult, meta OpenAPIUsageEmitMeta, extraMsg string)

EmitOpenAPIAnthropicUsageFailure 非流式或流式入口失败。

func EmitOpenAPIAnthropicUsageStreamSuccess

func EmitOpenAPIAnthropicUsageStreamSuccess(reqBody []byte, meta OpenAPIUsageEmitMeta, cap *AnthropicStreamCapture, channelID int, baseURL string, attempts []UsageChannelAttempt, quotaDelta int)

EmitOpenAPIAnthropicUsageStreamSuccess Anthropic 流式 messages 成功。

func EmitOpenAPIAnthropicUsageSuccess

func EmitOpenAPIAnthropicUsageSuccess(reqBody []byte, res *OpenAPIProxyResult, meta OpenAPIUsageEmitMeta, quotaDelta int)

EmitOpenAPIAnthropicUsageSuccess 非流式 Anthropic /v1/messages 成功。

func EmitOpenAPIOpenAIUsageFailure

func EmitOpenAPIOpenAIUsageFailure(reqBody []byte, res *OpenAPIProxyResult, meta OpenAPIUsageEmitMeta, extraMsg string)

EmitOpenAPIOpenAIUsageFailure 非流式或流式入口失败时的用量信号。

func EmitOpenAPIOpenAIUsageStreamSuccess

func EmitOpenAPIOpenAIUsageStreamSuccess(reqBody []byte, meta OpenAPIUsageEmitMeta, cap *OpenAIStreamCapture, channelID int, baseURL string, attempts []UsageChannelAttempt, quotaDelta int)

EmitOpenAPIOpenAIUsageStreamSuccess 流式 chat completion 成功后的用量(建议请求体含 stream_options.include_usage)。

func EmitOpenAPIOpenAIUsageSuccess

func EmitOpenAPIOpenAIUsageSuccess(reqBody []byte, res *OpenAPIProxyResult, meta OpenAPIUsageEmitMeta, quotaDelta int)

EmitOpenAPIOpenAIUsageSuccess 非流式 OpenAI 兼容 chat completion 成功后的用量信号。

func EnsureOpenAIChatStreamIncludeUsage

func EnsureOpenAIChatStreamIncludeUsage(body []byte) []byte

EnsureOpenAIChatStreamIncludeUsage 在 stream 为 true 时合并 stream_options.include_usage,便于上游返回 usage 块。

func ExpandedQueryFromModelAnswer

func ExpandedQueryFromModelAnswer(original, modelOut string, maxTerms int, separator string) (expanded string, terms []string)

ExpandedQueryFromModelAnswer parses expansion model output and joins terms with the original query.

func ExtractJSONFromLLMOutput

func ExtractJSONFromLLMOutput(s string, maxChars int) string

ExtractJSONFromLLMOutput pulls a JSON object/array substring from common LLM wrappers (fences, prose).

func GenerateLingRequestID

func GenerateLingRequestID() string

func NormalizeRewrittenQuery

func NormalizeRewrittenQuery(s string) string

NormalizeRewrittenQuery trims fences/quotes and keeps the first line of model output.

Types

type AlibabaHandler

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

func NewAlibabaHandler

func NewAlibabaHandler(ctx context.Context, llmOptions *LLMOptions) (*AlibabaHandler, error)

func (*AlibabaHandler) Interrupt

func (h *AlibabaHandler) Interrupt()

func (*AlibabaHandler) Provider

func (h *AlibabaHandler) Provider() string

func (*AlibabaHandler) Query

func (h *AlibabaHandler) Query(text, model string) (string, error)

func (*AlibabaHandler) QueryStream

func (h *AlibabaHandler) QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

func (*AlibabaHandler) QueryWithOptions

func (h *AlibabaHandler) QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

type AnthropicHandler

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

func NewAnthropicHandler

func NewAnthropicHandler(ctx context.Context, llmOptions *LLMOptions) (*AnthropicHandler, error)

func (*AnthropicHandler) Interrupt

func (h *AnthropicHandler) Interrupt()

func (*AnthropicHandler) Provider

func (h *AnthropicHandler) Provider() string

func (*AnthropicHandler) Query

func (h *AnthropicHandler) Query(text, model string) (string, error)

func (*AnthropicHandler) QueryStream

func (h *AnthropicHandler) QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

func (*AnthropicHandler) QueryWithOptions

func (h *AnthropicHandler) QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

type AnthropicStreamCapture

type AnthropicStreamCapture struct {
	StatusCode       int
	WallLatencyMs    int64
	StartedAtMs      int64
	FirstTokenAtMs   int64
	CompletedAtMs    int64
	RequestID        string
	Model            string
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	// contains filtered or unexported fields
}

AnthropicStreamCapture Anthropic SSE 流摘要(message_start / message_delta.usage)。

func ProxyAnthropicStreamWithCapture

func ProxyAnthropicStreamWithCapture(ctx context.Context, body []byte, anthropicVersion, anthropicBeta string, ch UpstreamChannel, w http.ResponseWriter) (*AnthropicStreamCapture, error)

ProxyAnthropicStreamWithCapture 流式转发并解析 Anthropic SSE。

type ChatMessage

type ChatMessage struct {
	Role    string
	Content string
}

type CompletionTokensDetails

type CompletionTokensDetails struct {
	AudioTokens              int
	ReasoningTokens          int
	AcceptedPredictionTokens int
	RejectedPredictionTokens int
}

type CozeHandler

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

func NewCozeHandler

func NewCozeHandler(ctx context.Context, llmOptions *LLMOptions) (*CozeHandler, error)

func (*CozeHandler) Interrupt

func (h *CozeHandler) Interrupt()

func (*CozeHandler) Provider

func (h *CozeHandler) Provider() string

func (*CozeHandler) Query

func (h *CozeHandler) Query(text, model string) (string, error)

func (*CozeHandler) QueryStream

func (h *CozeHandler) QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

func (*CozeHandler) QueryWithOptions

func (h *CozeHandler) QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

type FewShotExample

type FewShotExample struct {
	User      string
	Assistant string
}

type LLMDetails

type LLMDetails struct {
	RequestID               string
	Provider                string
	BaseURL                 string
	Model                   string
	Input                   string
	SystemPrompt            string
	N                       int
	MaxTokens               int
	EstimatedMaxOutputChars int
	FilterEmoji             bool
	RequestedOutputFormat   string
	AppliedResponseFormat   string
	ResponseFormatApplied   bool
	ResponseID              string
	Object                  string
	Created                 int64
	SystemFingerprint       string
	PromptFilterResultsJSON string
	ServiceTierJSON         string
	ChoicesCount            int
	Choices                 []QueryChoice
	Usage                   *TokenUsage
	UsageRawJSON            string
	ChoicesRawJSON          string
	RawResponseJSON         string
}

type LLMHandler

type LLMHandler interface {
	Query(text, model string) (string, error)

	QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

	QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

	Provider() string

	Interrupt()
}

LLMHandler common llm hanlder interface

func NewLLMProvider

func NewLLMProvider(ctx context.Context, provider, apiKey, apiURL, systemPrompt string) (LLMHandler, error)

NewLLMProvider provides a SoulNexus-like factory signature for Ling.

func NewProviderHandler

func NewProviderHandler(ctx context.Context, provider string, llmOptions *LLMOptions) (LLMHandler, error)

NewProviderHandler creates an LLM handler by provider type. Note: in Ling, non-OpenAI providers currently use OpenAI-compatible chat API shape.

type LLMOptions

type LLMOptions struct {
	Provider        string
	ApiKey          string
	BaseURL         string
	SystemPrompt    string
	FewShotExamples []FewShotExample
	Logger          *zap.Logger // Logger is optional; used for async memory summarization warnings and diagnostics.
}

type LLMProvider

type LLMProvider string

LLMProvider common provider type

func (LLMProvider) ToString

func (lp LLMProvider) ToString() string

ToString toString for llm

type LLMRequestEndData

type LLMRequestEndData struct {
	RequestID    string `json:"request_id"`
	SessionID    string `json:"session_id"`
	UserID       string `json:"user_id"`
	Provider     string `json:"provider"`
	Model        string `json:"model"`
	RequestType  string `json:"request_type"`
	Success      bool   `json:"success"`
	InputTokens  int    `json:"input_tokens"`
	OutputTokens int    `json:"output_tokens"`
	TotalTokens  int    `json:"total_tokens"`
	LatencyMs    int64  `json:"latency_ms"`
	Output       string `json:"output"`
	RequestedAt  int64  `json:"requested_at"`
	CompletedAt  int64  `json:"completed_at"`
}

LLMRequestEndData LLM请求结束信号数据

type LLMRequestErrorData

type LLMRequestErrorData struct {
	RequestID    string `json:"request_id"`
	SessionID    string `json:"session_id"`
	UserID       string `json:"user_id"`
	Provider     string `json:"provider"`
	Model        string `json:"model"`
	RequestType  string `json:"request_type"`
	ErrorCode    string `json:"error_code"`
	ErrorMessage string `json:"error_message"`
	LatencyMs    int64  `json:"latency_ms"`
	RequestedAt  int64  `json:"requested_at"`
	CompletedAt  int64  `json:"completed_at"`
}

LLMRequestErrorData LLM请求错误信号数据

type LLMRequestStartData

type LLMRequestStartData struct {
	RequestID   string `json:"request_id"`
	SessionID   string `json:"session_id"`
	UserID      string `json:"user_id"`
	Provider    string `json:"provider"`
	Model       string `json:"model"`
	RequestType string `json:"request_type"`
	Input       string `json:"input"`
	RequestedAt int64  `json:"requested_at"`
}

LLMRequestStartData LLM请求开始信号数据

type LLMRequestTracker

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

LLMRequestTracker LLM请求跟踪器

func NewLLMRequestTracker

func NewLLMRequestTracker(sessionID, userID, provider, model, baseURL, requestType string) *LLMRequestTracker

NewLLMRequestTracker 创建LLM请求跟踪器

func (*LLMRequestTracker) Complete

func (t *LLMRequestTracker) Complete(response *QueryResponse)

Complete 完成请求并记录成功信息

func (*LLMRequestTracker) Error

func (t *LLMRequestTracker) Error(errCode, errorMessage string)

Error 记录请求错误

func (*LLMRequestTracker) GetRequestID

func (t *LLMRequestTracker) GetRequestID() string

GetRequestID 获取请求ID

func (*LLMRequestTracker) MarkFirstToken

func (t *LLMRequestTracker) MarkFirstToken()

MarkFirstToken 标记首个token时间

func (*LLMRequestTracker) MarkStarted

func (t *LLMRequestTracker) MarkStarted()

MarkStarted 标记实际开始处理时间

func (*LLMRequestTracker) SetChannelUsageMeta

func (t *LLMRequestTracker) SetChannelUsageMeta(channelID int, attempts []UsageChannelAttempt)

SetChannelUsageMeta 可选:绑定本次调用命中的上游渠道 id 与多渠道路由尝试明细(如经网关轮询)。

func (*LLMRequestTracker) SetIPAddress

func (t *LLMRequestTracker) SetIPAddress(ip string)

SetIPAddress 设置IP地址

func (*LLMRequestTracker) SetRequestContent

func (t *LLMRequestTracker) SetRequestContent(content string)

SetRequestContent 设置请求内容

func (*LLMRequestTracker) SetResponseContent

func (t *LLMRequestTracker) SetResponseContent(content string)

SetResponseContent 设置响应内容

func (*LLMRequestTracker) SetStatusCode

func (t *LLMRequestTracker) SetStatusCode(code int)

SetStatusCode 设置HTTP状态码

func (*LLMRequestTracker) SetUserAgent

func (t *LLMRequestTracker) SetUserAgent(userAgent string)

SetUserAgent 设置用户代理

type LLMUsageSignalPayload

type LLMUsageSignalPayload struct {
	RequestID       string
	UserID          string
	Provider        string
	Model           string
	BaseURL         string
	RequestType     string
	ChannelID       int // 最终成功的 llm_channels.id;全失败时为 0
	ChannelAttempts []UsageChannelAttempt
	InputTokens     int
	OutputTokens    int
	TotalTokens     int
	QuotaDelta      int // 凭证侧扣除的额度单位(HTTP 层按模型策略计算)
	LatencyMs       int64
	TTFTMs          int64
	TPS             float64
	QueueTimeMs     int64
	RequestContent  string
	ResponseContent string
	UserAgent       string
	IPAddress       string
	StatusCode      int
	Success         bool
	ErrorCode       string
	ErrorMessage    string
	RequestedAtMs   int64
	StartedAtMs     int64
	FirstTokenAtMs  int64
	CompletedAtMs   int64
}

LLMUsageSignalPayload 在 SignalLLMUsage 上作为 sender 传递;时间字段为 Unix 毫秒,与 models.LLMUsage 对齐。

type LMStudioHandler

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

func NewLMStudioHandler

func NewLMStudioHandler(ctx context.Context, llmOptions *LLMOptions) (*LMStudioHandler, error)

func (*LMStudioHandler) Interrupt

func (h *LMStudioHandler) Interrupt()

func (*LMStudioHandler) Provider

func (h *LMStudioHandler) Provider() string

func (*LMStudioHandler) Query

func (h *LMStudioHandler) Query(text, model string) (string, error)

func (*LMStudioHandler) QueryStream

func (h *LMStudioHandler) QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

func (*LMStudioHandler) QueryWithOptions

func (h *LMStudioHandler) QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

type MessageCreatedData

type MessageCreatedData struct {
	MessageID  string `json:"message_id"`
	SessionID  string `json:"session_id"`
	Role       string `json:"role"`
	Content    string `json:"content"`
	TokenCount int    `json:"token_count"`
	Model      string `json:"model"`
	Provider   string `json:"provider"`
	RequestID  string `json:"request_id"`
	CreatedAt  int64  `json:"created_at"`
}

MessageCreatedData 消息创建信号数据

type OllamaHandler

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

func NewOllamaHandler

func NewOllamaHandler(ctx context.Context, llmOptions *LLMOptions) (*OllamaHandler, error)

func (*OllamaHandler) Interrupt

func (h *OllamaHandler) Interrupt()

func (*OllamaHandler) Provider

func (h *OllamaHandler) Provider() string

func (*OllamaHandler) Query

func (h *OllamaHandler) Query(text, model string) (string, error)

func (*OllamaHandler) QueryStream

func (h *OllamaHandler) QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

func (*OllamaHandler) QueryWithOptions

func (h *OllamaHandler) QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

type OpenAIStreamCapture

type OpenAIStreamCapture struct {
	StatusCode       int
	WallLatencyMs    int64
	StartedAtMs      int64
	FirstTokenAtMs   int64
	CompletedAtMs    int64
	RequestID        string
	Model            string
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	CachedPrompt     int // prompt_tokens_details.cached_tokens
	// contains filtered or unexported fields
}

OpenAIStreamCapture 流式结束后用于用量信号(依赖上游在流中返回 usage,见 EnsureOpenAIChatStreamIncludeUsage)。

func ProxyOpenAIStreamWithCapture

func ProxyOpenAIStreamWithCapture(ctx context.Context, body []byte, accept string, ch UpstreamChannel, w http.ResponseWriter) (*OpenAIStreamCapture, error)

ProxyOpenAIStreamWithCapture 流式转发并解析 SSE data 行中的 id/model/usage;将字节原样写入 w。

type OpenAPIProxyResult

type OpenAPIProxyResult struct {
	FinalStatus   int
	FinalBody     []byte
	FinalHeader   http.Header
	WinChannelID  int
	WinBaseURL    string
	Attempts      []UsageChannelAttempt
	WallLatencyMs int64
	QueueMs       int64
	WinHopMs      int64
	AllFailed     bool
}

OpenAPIProxyResult 非流式多渠道路由执行结果。

func ProxyAnthropicNonStream

func ProxyAnthropicNonStream(ctx context.Context, body []byte, anthropicVersion, anthropicBeta string, channels []UpstreamChannel) *OpenAPIProxyResult

ProxyAnthropicNonStream 按 channels 顺序重试 Anthropic /v1/messages。

func ProxyOpenAINonStream

func ProxyOpenAINonStream(ctx context.Context, body []byte, accept string, channels []UpstreamChannel) *OpenAPIProxyResult

ProxyOpenAINonStream 按 channels 顺序重试,直到 OpenAI 兼容 chat completion 业务成功或全部失败。

type OpenAPIUsageEmitMeta

type OpenAPIUsageEmitMeta struct {
	UserIDStr string
	UserAgent string
	ClientIP  string
}

OpenAPIUsageEmitMeta 由 HTTP 层填入,用于 OpenAPI 代理用量信号(不含 gin / models)。

type OpenaiHandler

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

func NewOpenaiHandler

func NewOpenaiHandler(ctx context.Context, llmOptions *LLMOptions) (*OpenaiHandler, error)

func (*OpenaiHandler) GetMaxMemoryMessages

func (oh *OpenaiHandler) GetMaxMemoryMessages() int

func (*OpenaiHandler) Interrupt

func (oh *OpenaiHandler) Interrupt()

func (*OpenaiHandler) Provider

func (oh *OpenaiHandler) Provider() string

func (*OpenaiHandler) Query

func (oh *OpenaiHandler) Query(text, model string) (string, error)

func (*OpenaiHandler) QueryStream

func (oh *OpenaiHandler) QueryStream(text string, options *QueryOptions, callback func(segment string, isComplete bool) error) (*QueryResponse, error)

func (*OpenaiHandler) QueryWithOptions

func (oh *OpenaiHandler) QueryWithOptions(text string, options *QueryOptions) (*QueryResponse, error)

func (*OpenaiHandler) ResetMemory

func (oh *OpenaiHandler) ResetMemory()

func (*OpenaiHandler) SetMaxMemoryMessages

func (oh *OpenaiHandler) SetMaxMemoryMessages(n int)

func (*OpenaiHandler) SummarizeMemory

func (oh *OpenaiHandler) SummarizeMemory(model string) (string, error)

type PromptTokensDetails

type PromptTokensDetails struct {
	AudioTokens  int
	CachedTokens int
}

type QueryChoice

type QueryChoice struct {
	Index        int
	Content      string
	FinishReason string
}

type QueryExpansion

type QueryExpansion struct {
	Original string
	Expanded string
	Terms    []string
	Debug    map[string]any
}

QueryExpansion contains the results of query expansion

type QueryOptions

type QueryOptions struct {
	Model                string
	N                    int
	MaxTokens            int
	Temperature          float32
	TopP                 float32
	LogitBias            map[string]int
	FilterEmoji          bool
	EnableJSONOutput     bool
	OutputFormat         string
	EmotionalTone        bool   // EmotionalTone, when true, appends a short instruction so replies read warmer and more human (still factual).
	EnableQueryExpansion bool   // EnableQueryExpansion enables automatic query expansion using LLM
	ExpansionMaxTerms    int    // ExpansionMaxTerms maximum number of expansion terms
	ExpansionSeparator   string // ExpansionSeparator separator for expanded terms

	// EnableQueryRewrite rewrites the user message with a stateless LLM call before expansion/main query.
	EnableQueryRewrite bool
	// QueryRewriteModel overrides the model for the rewrite call only (empty = use Model, then handler default).
	QueryRewriteModel string
	// QueryRewriteInstruction is appended to the rewrite prompt as extra constraints.
	QueryRewriteInstruction string
	// EnableSelfQueryJSONOutput requests strict JSON object replies (response_format json_object on OpenaiHandler).
	// SelfQueryExtractor sets this by default; other handlers may ignore it and still return parseable text.
	EnableSelfQueryJSONOutput bool
	// Messages allows callers to pass short-term conversation history explicitly.
	// Handlers append current user text as the final user turn when needed.
	Messages []ChatMessage

	// 新增字段用于数据库记录
	SessionID   string // 会话ID
	UserID      string // 用户ID
	RequestType string // 请求类型: query, query_stream, rewrite, expand
	// HTTPUserAgent / ClientIP / UsageChannelID 供服务端调用(如 OpenAPI Agent)写入 llm_usage。
	HTTPUserAgent  string
	ClientIP       string
	UsageChannelID int
}

type QueryResponse

type QueryResponse struct {
	Provider     string          `json:"provider"`
	Model        string          `json:"model"`
	Choices      []QueryChoice   `json:"choices"`
	Usage        *TokenUsage     `json:"usage"`
	Expansion    *QueryExpansion `json:"expansion,omitempty"`
	Rewrite      *QueryRewrite   `json:"rewrite,omitempty"`
	FinishReason string          `json:"finish_reason,omitempty"`

	// 新增字段用于数据库记录
	RequestID   string `json:"request_id"`
	SessionID   string `json:"session_id"`
	UserID      string `json:"user_id"`
	RequestedAt int64  `json:"requested_at"`
	CompletedAt int64  `json:"completed_at"`
	LatencyMs   int64  `json:"latency_ms"`
}

type QueryRewrite

type QueryRewrite struct {
	Original  string
	Rewritten string
}

QueryRewrite records the optional LLM rewrite step applied before expansion / main completion.

type SelfQueryExtractor

type SelfQueryExtractor struct {
	LLM LLMHandler

	AllowedFields []string
}

SelfQueryExtractor turns a natural-language question into a search query + structured filters for the retrieval step of RAG. It does not execute search or produce grounded answers by itself. Use NewSelfQueryExtractor and call Extract only when you need that decomposition (on demand).

func NewSelfQueryExtractor

func NewSelfQueryExtractor(h LLMHandler, allowedFields []string) *SelfQueryExtractor

NewSelfQueryExtractor returns an extractor. allowedFields may be empty (no restriction hint).

func (*SelfQueryExtractor) Extract

func (e *SelfQueryExtractor) Extract(ctx context.Context, question string, opt *SelfQueryOptions) (*SelfQueryResult, error)

Extract runs the self-query prompt and parses JSON from the model output.

type SelfQueryFilterSpec

type SelfQueryFilterSpec struct {
	Namespace string   `json:"namespace,omitempty"`
	Source    string   `json:"source,omitempty"`
	DocType   string   `json:"doc_type,omitempty"`
	Location  string   `json:"location,omitempty"`
	Years     []string `json:"years,omitempty"`
	Dates     []string `json:"dates,omitempty"`
	TagsAny   []string `json:"tags_any,omitempty"`
}

SelfQueryFilterSpec is the structured filter subset produced by the model.

type SelfQueryOptions

type SelfQueryOptions struct {
	Model string

	// AllowedFields restricts which filter keys the model may use (prompt hint).
	AllowedFields []string

	// MaxJSONChars caps input length before JSON extraction (0 = default 16000).
	MaxJSONChars int

	// UsePlainQuery uses Query instead of QueryWithOptions with JSON object mode.
	// Set true for backends that handle JSON poorly in structured-output mode.
	UsePlainQuery bool
}

SelfQueryOptions configures a single SelfQuery extraction call.

type SelfQueryResult

type SelfQueryResult struct {
	Query   string
	Filters map[string]any

	Spec SelfQueryFilterSpec
	Raw  string
}

SelfQueryResult is the parsed self-query output plus a Qdrant-oriented filter map. It is an intermediate retrieval plan (rewritten query + metadata constraints), not retrieved documents and not the final user-facing answer; wire it to your search/vector layer, then run generation over returned chunks.

func SelfQueryExtract

func SelfQueryExtract(ctx context.Context, h LLMHandler, question string, allowedFields []string, opt *SelfQueryOptions) (*SelfQueryResult, error)

SelfQueryExtract is a convenience wrapper around NewSelfQueryExtractor(...).Extract(...).

type SessionCreatedData

type SessionCreatedData struct {
	SessionID    string `json:"session_id"`
	UserID       string `json:"user_id"`
	Title        string `json:"title"`
	Provider     string `json:"provider"`
	Model        string `json:"model"`
	SystemPrompt string `json:"system_prompt"`
	CreatedAt    int64  `json:"created_at"`
}

SessionCreatedData 会话创建信号数据

type TokenUsage

type TokenUsage struct {
	PromptTokens            int
	CompletionTokens        int
	TotalTokens             int
	PromptTokensDetails     *PromptTokensDetails
	CompletionTokensDetails *CompletionTokensDetails
}

type UpstreamChannel

type UpstreamChannel struct {
	ID                 int
	Key                string
	BaseURL            *string
	OpenAIOrganization *string
}

UpstreamChannel OpenAPI 上游渠道(由 HTTP 层从 llm_channels 映射而来)。

type UsageChannelAttempt

type UsageChannelAttempt struct {
	Order        int    `json:"order"`
	ChannelID    int    `json:"channel_id"`
	BaseURL      string `json:"base_url,omitempty"`
	Success      bool   `json:"success"`
	StatusCode   int    `json:"status_code,omitempty"`
	LatencyMs    int64  `json:"latency_ms,omitempty"`
	TTFTMs       int64  `json:"ttft_ms,omitempty"`
	ErrorCode    string `json:"error_code,omitempty"`
	ErrorMessage string `json:"error_message,omitempty"`
}

UsageChannelAttempt 用量信号里描述单次渠道尝试(与 models.LLMUsageChannelAttempt JSON 对齐)。

Jump to

Keyboard shortcuts

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