llm

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: MIT Imports: 25 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss = errors.New("cache miss")
	ErrCacheFull = errors.New("cache is full")
)

添加自定义错误类型

Functions

func AssistantPromptMessage

func AssistantPromptMessage(text string) *assistantPromptMessage

AssistantPromptMessage ...

func AssistantReasoningMessage

func AssistantReasoningMessage(text string) *assistantPromptMessage

AssistantReasoningMessage ...

func CompressQueryMessage

func CompressQueryMessage(messages []Message) (string, error)

CompressQueryMessage ...

func ConcatQueryMessage

func ConcatQueryMessage(messages []Message) (string, error)

ConcatQueryMessage ...

func LastQueryMessage

func LastQueryMessage(messages []Message) (string, error)

LastQueryMessage ...

func Register

func Register(name string, provider Provider)

Register ...

func SummarizeQueryMessage

func SummarizeQueryMessage(messages []Message) (string, error)

SummarizeQueryMessage extracts an intelligent summary from conversation messages

func ToolPromptMessage

func ToolPromptMessage(text string, toolID string) *toolPromptMessage

ToolPromptMessage ...

func WithConfigSupplier

func WithConfigSupplier(c ConfigSupplier)

WithConfigSupplier ...

func WithConfigs

func WithConfigs(c map[string]any)

WithConfigs ...

func WithSingleConfig

func WithSingleConfig(c any)

WithSingleConfig ...

Types

type BaseCache

type BaseCache struct {
	Config      *CacheConfig
	Embedder    embedding.Embedder
	VectorStore vdb.VDB
	Ranker      Scorer
}

BaseCache ...

func (*BaseCache) SetQueryProcessor

func (c *BaseCache) SetQueryProcessor(p QueryProcessor)

SetQueryProcessor ...

type Cache

type Cache interface {
	Fetch(context.Context, []Message) (*CachedDocument, bool, error)
	Store(context.Context, *CachedDocument, string) error
}

Cache ...

func NewMemoCache

func NewMemoCache() Cache

NewMemoCache ...

func NewRedisCache

func NewRedisCache(cli *redis.Client) Cache

NewRedisCache ...

type CacheConfig

type CacheConfig struct {
	TTL             time.Duration
	MaxEntries      int
	CleanupInterval time.Duration
	EnableMetrics   bool
	QueryProcessor  QueryProcessor
}

CacheConfig 定义缓存的配置选项

func DefaultConfig

func DefaultConfig() *CacheConfig

DefaultConfig 返回默认配置

type CacheFactory

type CacheFactory func() Cache

CacheFactory ...

type CachedDocument

type CachedDocument struct {
	ID     string
	Query  string
	Answer string
	Score  float64
	Vector []float64
}

CachedDocument ...

type ChatCompletionMessage

type ChatCompletionMessage struct {
	Role    string `json:"role"`
	Content string `json:"content,omitempty"`
	// 多种类型内容(支持图片和文本)
	MultipartContent []*MultipartContent `json:"contents,omitnil,omitempty" name:"contents"`
	// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
	ToolCalls []*ToolCall `json:"tool_calls,omitempty"`
	// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
	ToolCallID string `json:"tool_call_id,omitempty"`
}

ChatCompletionMessage ...

func (*ChatCompletionMessage) MarshalJSON

func (c *ChatCompletionMessage) MarshalJSON() ([]byte, error)

序列化 ChatCompletionMessage

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Messages       []*ChatCompletionMessage `json:"messages,omitnil,omitempty" name:"messages"`
	*InvokeOptions                          // base request options
}

ChatCompletionRequest represents a request structure for chat completion API.

type ChatCompletionResponseFormat

type ChatCompletionResponseFormat struct {
	Type       ChatCompletionResponseFormatType        `json:"type,omitempty"`
	JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"`
}

type ChatCompletionResponseFormatJSONSchema

type ChatCompletionResponseFormatJSONSchema struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Schema      json.Marshaler `json:"schema"`
	Strict      bool           `json:"strict"`
}

type ChatCompletionResponseFormatType

type ChatCompletionResponseFormatType string
const (
	ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object"
	ChatCompletionResponseFormatTypeJSONSchema ChatCompletionResponseFormatType = "json_schema"
	ChatCompletionResponseFormatTypeText       ChatCompletionResponseFormatType = "text"
)

type ChatContent

type ChatContent struct {
	// 内容类型
	// 注意:
	// 当前只支持传入单张图片,传入多张图片时,以第一个图片为准。
	// 注意:此字段可能返回 null,表示取不到有效值。
	Type string `json:"type,omitnil,omitempty" name:"type"`

	// 当 Type 为 text 时使用,表示具体的文本内容
	// 注意:此字段可能返回 null,表示取不到有效值。
	Text string `json:"text,omitnil,omitempty" name:"text"`

	// 图片的url,当 Type 为 image_url 时使用,表示具体的图片内容
	// 如"https://example.com/1.png" 或 图片的base64(注意 "data:image/jpeg;base64," 为必要部分):"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAA......"
	// 注意:此字段可能返回 null,表示取不到有效值。
	ImageURL *ChatImageURL `json:"image_url,omitnil,omitempty" name:"image_url"`
}

Content ...

type ChatImageURL

type ChatImageURL struct {
	// 图片的 Url(以 http:// 或 https:// 开头)
	// 注意:此字段可能返回 null,表示取不到有效值。
	URL string `json:"url,omitnil,omitempty" name:"url"`
}

ChatImageURL ...

type Chunk

type Chunk struct {
	ID                string        `json:"id"`
	CreatedAt         int64         `json:"created_at"`
	Model             string        `json:"model"`
	Object            string        `json:"object"`
	SystemFingerprint string        `json:"system_fingerprint"`
	Choices           []*ChunkDelta `json:"choices"`
	Usage             *Usage        `json:"usage"`
	Delta             *ChunkDelta   `json:"-"`
}

Chunk ...

func NewChunk

func NewChunk(i int, msg *assistantPromptMessage, useage *Usage) *Chunk

NewChunk ...

type ChunkDelta

type ChunkDelta struct {
	Index        int                     `json:"index"`
	FinishReason string                  `json:"finish_reason"`
	Logprobs     any                     `json:"logprobs"`
	Message      *assistantPromptMessage `json:"delta"`
}

ChunkDelta ...

type ConfigSupplier

type ConfigSupplier interface {
	Get(key string) any
}

ConfigSupplier ...

var Config ConfigSupplier = &envConfig{}

Config default cache

type ConfigSupplierFunc

type ConfigSupplierFunc func(key string) any

ConfigSupplierFunc ...

type ExactMatchScorer

type ExactMatchScorer struct {
}

ExactMatchScorer 精确匹配排序

func (*ExactMatchScorer) Eval

func (r *ExactMatchScorer) Eval(query string) float64

Eval 精确匹配排序

type FunctionCallBehavior

type FunctionCallBehavior string

FunctionCallBehavior is the behavior to use when calling functions.

const (
	// FunctionCallBehaviorNone will not call any functions.
	FunctionCallBehaviorNone FunctionCallBehavior = "none"
	// FunctionCallBehaviorAuto will call functions automatically.
	FunctionCallBehaviorAuto FunctionCallBehavior = "auto"
)

type FunctionDefinition

type FunctionDefinition struct {
	// Name is the name of the function.
	Name string `json:"name"`
	// Description is a description of the function.
	Description string `json:"description"`
	// Parameters is a list of parameters for the function.
	Parameters map[string]any `json:"parameters,omitempty"`
}

FunctionDefinition is a definition of a function that can be called by the model.

type FunctionReference

type FunctionReference struct {
	// Name is the name of the function.
	Name string `json:"name"`
}

FunctionReference is a reference to a function.

type Hook

type Hook interface {
	OnBeforeInvoke(context.Context) context.Context
	OnAfterInvoke(ctx context.Context, err error)
	OnFirstChunk(context.Context, error) context.Context
}

Hook ...

func NewOtelHook

func NewOtelHook(opts ...HookOption) Hook

NewOtelHook ...

type HookOption

type HookOption func(*OtelHook)

HookOption ...

type Instance

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

Instance ...

func NewInstance

func NewInstance(provider string, opts ...Option) *Instance

NewInstance ...

func (*Instance) Invoke

func (mi *Instance) Invoke(ctx context.Context,
	messages []Message, options ...InvokeOption) (*Response, error)

Invoke ...

type InvokeOption

type InvokeOption func(*InvokeOptions)

InvokeOption is a function that configures a InvokeOptions.

func WithModel

func WithModel(model string) InvokeOption

WithModel specifies which model name to use.

func WithStream

func WithStream(stream bool) InvokeOption

WithStream specifies stream output.

func WithTools

func WithTools(tools ...*Tool) InvokeOption

WithTools specifies which tools to use.

type InvokeOptions

type InvokeOptions struct {
	Stop           []string                      `json:"stop,omitempty"`
	ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"`
	// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
	// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
	// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
	LogitBias map[string]int `json:"logit_bias,omitempty"`
	// LogProbs indicates whether to return log probabilities of the output tokens or not.
	// If true, returns the log probabilities of each output token returned in the content of message.
	// This option is currently not available on the gpt-4-vision-preview model.
	LogProbs bool `json:"logprobs,omitempty"`
	// TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each
	// token position, each with an associated log probability.
	// logprobs must be set to true if this parameter is used.
	TopLogProbs int `json:"top_logprobs,omitempty"`
	// Options for streaming response. Only set this when you set stream: true.
	StreamOptions *StreamOptions `json:"stream_options,omitempty"`

	// Model is the model to use.
	Model string `json:"model"`
	// Stream is the stream output.
	Stream bool `json:"stream,omitempty"`
	// CandidateCount is the number of response candidates to generate.
	CandidateCount int `json:"candidate_count,omitempty"`
	// MaxTokens is the maximum number of tokens to generate.
	MaxTokens int `json:"max_tokens,omitempty"`
	// Temperature is the temperature for sampling, between 0 and 1.
	Temperature float64 `json:"temperature,omitempty"`
	// StopWords is a list of words to stop on.
	StopWords []string `json:"stop_words,omitempty"`
	// StreamingFunc is a function to be called for each chunk of a streaming response.
	// Return an error to stop streaming early.
	StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
	// TopK is the number of tokens to consider for top-k sampling.
	TopK int `json:"top_k,omitempty"`
	// TopP is the cumulative probability for top-p sampling.
	TopP float64 `json:"top_p,omitempty"`
	// Seed is a seed for deterministic sampling.
	Seed int `json:"seed,omitempty"`
	// MinLength is the minimum length of the generated text.
	MinLength int `json:"min_length,omitempty"`
	// MaxLength is the maximum length of the generated text.
	MaxLength int `json:"max_length,omitempty"`
	// N is how many chat completion choices to generate for each input message.
	N int `json:"n,omitempty"`
	// RepetitionPenalty is the repetition penalty for sampling.
	RepetitionPenalty float64 `json:"repetition_penalty,omitempty"`
	// FrequencyPenalty is the frequency penalty for sampling.
	FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
	// PresencePenalty is the presence penalty for sampling.
	PresencePenalty float64 `json:"presence_penalty,omitempty"`

	// JSONMode is a flag to enable JSON mode.
	JSONMode bool `json:"json_mode,omitempty"`

	// Tools is a list of tools to use. Each tool can be a specific tool or a function.
	Tools []*Tool `json:"tools,omitempty"`
	// ToolChoice is the choice of tool to use, it can either be "none", "auto" (the default behavior), or a specific tool as described in the ToolChoice type.
	ToolChoice any `json:"tool_choice,omitempty"`

	// Metadata is a map of metadata to include in the request.
	// The meaning of this field is specific to the backend in use.
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

InvokeOptions ...

type MemoCache

type MemoCache struct {
	BaseCache
	// contains filtered or unexported fields
}

MemoCache ...

func (*MemoCache) Fetch

func (m *MemoCache) Fetch(ctx context.Context, messages []Message) (*CachedDocument, bool, error)

Fetch ...

func (*MemoCache) Store

func (m *MemoCache) Store(ctx context.Context, document *CachedDocument, value string) error

Store ...

type Message

type Message interface {
	Content() string
	MultipartContent() []*MultipartContent
	Role() PromptMessageRole
	ToolID() string
	GetToolCalls() []*ToolCall
	String() string
}

Message ...

type Messages

type Messages[T PromptMessage | assistantPromptMessage] []T

Messages ...

type Metrics

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

Metrics 存储缓存的统计信息

func (*Metrics) GetStats

func (m *Metrics) GetStats() map[string]interface{}

GetStats 返回当前统计信息

type MultipartContent

type MultipartContent struct {
	Type string
	Data string
}

func MultipartContentImageURL

func MultipartContentImageURL(url string) *MultipartContent

func MultipartContentText

func MultipartContentText(text string) *MultipartContent

type NoneLogger

type NoneLogger struct {
}

NoneLogger ...

func (*NoneLogger) DebugContextf

func (w *NoneLogger) DebugContextf(ctx context.Context, format string, args ...interface{})

DebugContextf ...

func (*NoneLogger) ErrorContextf

func (w *NoneLogger) ErrorContextf(ctx context.Context, format string, args ...interface{})

ErrorContextf ...

func (*NoneLogger) InfoContextf

func (w *NoneLogger) InfoContextf(ctx context.Context, format string, args ...interface{})

InfoContextf ...

func (*NoneLogger) WarnContextf

func (w *NoneLogger) WarnContextf(ctx context.Context, format string, args ...interface{})

WarnContextf ...

type OAILLM

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

base llm by openai input and output

func NewOAILLM

func NewOAILLM(url, apiKey string) *OAILLM

func (*OAILLM) ChatCompletions

func (o *OAILLM) ChatCompletions(ctx context.Context, req *ChatCompletionRequest) (io.ReadCloser, error)

ChatCompletions ...

func (*OAILLM) Invoke

func (o *OAILLM) Invoke(ctx context.Context, messages []Message, optFuncs ...InvokeOption) (*Response, error)

type Option

type Option func(*Options)

Option ...

func WithCache

func WithCache(c Cache) Option

WithCache ...

func WithDefaultModel

func WithDefaultModel(m string) Option

WithDefaultModel ...

func WithHook

func WithHook(hooks ...Hook) Option

WithHook ...

func WithLogger

func WithLogger(l logger) Option

WithLogger ...

type Options

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

Options ...

type OtelHook

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

OtelHook ...

func (*OtelHook) OnAfterInvoke

func (h *OtelHook) OnAfterInvoke(ctx context.Context, err error)

OnAfterInvoke ...

func (*OtelHook) OnBeforeInvoke

func (h *OtelHook) OnBeforeInvoke(ctx context.Context) context.Context

OnBeforeInvoke ...

func (*OtelHook) OnFirstChunk

func (h *OtelHook) OnFirstChunk(ctx context.Context, _ error) context.Context

OnFirstChunk ...

type PromptMessage

type PromptMessage struct {
	Name string
	// contains filtered or unexported fields
}

PromptMessage ...

func SystemPromptMessage

func SystemPromptMessage(text string) PromptMessage

SystemPromptMessage ...

func UserMultipartPromptMessage

func UserMultipartPromptMessage(contents ...*MultipartContent) PromptMessage

UserMultipartPromptMessage ...

func UserTextPromptMessage

func UserTextPromptMessage(text string) PromptMessage

UserTextPromptMessage ...

func (PromptMessage) Content

func (m PromptMessage) Content() string

func (PromptMessage) GetToolCalls

func (m PromptMessage) GetToolCalls() []*ToolCall

func (PromptMessage) MarshalJSON

func (m PromptMessage) MarshalJSON() ([]byte, error)

MarshalJSON 实现marshal

func (PromptMessage) MultipartContent

func (m PromptMessage) MultipartContent() []*MultipartContent

func (PromptMessage) Role

Role ...

func (PromptMessage) String

func (m PromptMessage) String() string

func (PromptMessage) ToolID

func (m PromptMessage) ToolID() string

ToolID ...

type PromptMessageRole

type PromptMessageRole string
const (
	PromptMessageRoleSystem    PromptMessageRole = "system"
	PromptMessageRoleUser      PromptMessageRole = "user"
	PromptMessageRoleAssistant PromptMessageRole = "assistant"
	PromptMessageRoleTool      PromptMessageRole = "tool"
)

type Provider

type Provider interface {
	Invoke(context.Context, []Message, ...InvokeOption) (*Response, error)
}

Provider ...

type QueryProcessor

type QueryProcessor func([]Message) (string, error)

QueryProcessor ...

type RedisCache

type RedisCache struct {
	BaseCache
	// contains filtered or unexported fields
}

RedisCache ...

func (*RedisCache) Fetch

func (m *RedisCache) Fetch(ctx context.Context, messages []Message) (*CachedDocument, bool, error)

Fetch ...

func (*RedisCache) Store

func (m *RedisCache) Store(ctx context.Context, document *CachedDocument, value string) error

Store ...

type Response

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

Response ...

func NewStreamResponse

func NewStreamResponse() *Response

NewStreamResponse ...

func (*Response) MakeResult

func (resp *Response) MakeResult() *Response

MakeResult ...

func (*Response) MakeStream

func (resp *Response) MakeStream() *Response

MakeStream ...

func (*Response) Result

func (resp *Response) Result() *Result

Result ...

func (*Response) Stream

func (resp *Response) Stream() *Stream

Stream ...

type Result

type Result struct {
	Model string `json:"model"`
	// Messages          []*PromptMessage
	Message           *assistantPromptMessage `json:"message"`
	Usage             *Usage                  `json:"usage"`
	SystemFingerprint string                  `json:"system_fingerprint"`
}

Result ...

func (*Result) String

func (r *Result) String() string

String ...

type Scorer

type Scorer interface {
	Eval(string) float64
}

Scorer 打分

type Stream

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

Stream ...

func NewStream

func NewStream() *Stream

NewStream ...

func (*Stream) Close

func (s *Stream) Close()

Close ...

func (*Stream) Next

func (s *Stream) Next() *Chunk

Next ...

func (*Stream) Push

func (s *Stream) Push(it *Chunk)

Push ...

func (*Stream) Read

func (s *Stream) Read(body io.ReadCloser, fn func(chunk []byte) (*Chunk, bool))

Read ...

type StreamOptions

type StreamOptions struct {
	// If set, an additional chunk will be streamed before the data: [DONE] message.
	// The usage field on this chunk shows the token usage statistics for the entire request,
	// and the choices field will always be an empty array.
	// All other chunks will also include a usage field, but with a null value.
	IncludeUsage bool `json:"include_usage,omitempty"`
}

type Tool

type Tool struct {
	// Type is the type of the tool.
	Type string `json:"type"`
	// Function is the function to call.
	Function *FunctionDefinition `json:"function,omitempty"`
}

Tool is a tool that can be used by the model.

type ToolCall

type ToolCall struct {
	ID       string           `json:"id"`
	Type     string           `json:"type"`
	Index    int              `json:"index"`
	Function ToolCallFunction `json:"function"`
}

ToolCall ...

type ToolCallFunction

type ToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

ToolCallFunction ...

type ToolChoice

type ToolChoice struct {
	// Type is the type of the tool.
	Type string `json:"type"`
	// Function is the function to call (if the tool is a function).
	Function *FunctionReference `json:"function,omitempty"`
}

ToolChoice is a specific tool to use.

type Usage

type Usage struct {
	PromptTokens     int    `json:"prompt_tokens"`
	CompletionTokens int    `json:"completion_tokens"`
	TotalTokens      int    `json:"total_tokens"`
	Currency         string `json:"currency"`
	Latency          int    `json:"latency"`
}

Usage ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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