llm

package
v0.0.0-...-20e2264 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 21 Imported by: 0

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 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 Chunk

type Chunk struct {
	Model             string           `json:"model"`
	Messages          []*PromptMessage `json:"-"`
	SystemFingerprint string           `json:"system_fingerprint"`

	Delta *ChunkDelta `json:"delta"`
}

Chunk ...

func NewChunk

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

NewChunk ...

type ChunkDelta

type ChunkDelta struct {
	Index        int                     `json:"index"`
	Message      *assistantPromptMessage `json:"message"`
	Usage        *Usage                  `json:"usage"`
	FinishReason string                  `json:"finish_reason"`
	Done         bool                    `json:"done"`
}

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 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, tools []PromptMessageTool, 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.

type InvokeOptions

type InvokeOptions struct {
	// Model is the model to use.
	Model string `json:"model"`
	// Stream is the stream output.
	Stream bool `json:"stream"`
	// CandidateCount is the number of response candidates to generate.
	CandidateCount int `json:"candidate_count"`
	// MaxTokens is the maximum number of tokens to generate.
	MaxTokens int `json:"max_tokens"`
	// Temperature is the temperature for sampling, between 0 and 1.
	Temperature float64 `json:"temperature"`
	// StopWords is a list of words to stop on.
	StopWords []string `json:"stop_words"`
	// 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"`
	// TopP is the cumulative probability for top-p sampling.
	TopP float64 `json:"top_p"`
	// Seed is a seed for deterministic sampling.
	Seed int `json:"seed"`
	// MinLength is the minimum length of the generated text.
	MinLength int `json:"min_length"`
	// MaxLength is the maximum length of the generated text.
	MaxLength int `json:"max_length"`
	// N is how many chat completion choices to generate for each input message.
	N int `json:"n"`
	// RepetitionPenalty is the repetition penalty for sampling.
	RepetitionPenalty float64 `json:"repetition_penalty"`
	// FrequencyPenalty is the frequency penalty for sampling.
	FrequencyPenalty float64 `json:"frequency_penalty"`
	// PresencePenalty is the presence penalty for sampling.
	PresencePenalty float64 `json:"presence_penalty"`

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

	// 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"`

	// Function defitions to include in the request.
	// Deprecated: Use Tools instead.
	Functions []FunctionDefinition `json:"functions,omitempty"`
	// FunctionCallBehavior is the behavior to use when calling functions.
	//
	// If a specific function should be invoked, use the format:
	// `{"name": "my_function"}`
	// Deprecated: Use ToolChoice instead.
	FunctionCallBehavior FunctionCallBehavior `json:"function_call,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() *PromptMessageContent
	Role() PromptMessageRole
	ToolID() string
	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 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 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 UserPromptMessage

func UserPromptMessage(text string) PromptMessage

UserPromptMessage ...

func (PromptMessage) Content

func (m PromptMessage) Content() *PromptMessageContent

Content ...

func (PromptMessage) MarshalJSON

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

MarshalJSON 实现marshal

func (PromptMessage) Role

Role ...

func (PromptMessage) String

func (m PromptMessage) String() string

func (PromptMessage) ToolID

func (m PromptMessage) ToolID() string

ToolID ...

func (*PromptMessage) UnmarshalJSON

func (m *PromptMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现unmarshal

type PromptMessageContent

type PromptMessageContent struct {
	Type int    `json:"type"`
	Data string `json:"data"`
}

PromptMessageContent ...

func TextPromptMessageContent

func TextPromptMessageContent(text string) *PromptMessageContent

TextPromptMessageContent ...

type PromptMessageRole

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

type PromptMessageTool

type PromptMessageTool struct {
	Name        string
	Description string
	Parameters  map[string]any
}

PromptMessageTool for prompt message tool

type Provider

type Provider interface {
	Invoke(context.Context, []Message, []PromptMessageTool, ...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 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"`
	Function ToolCallFunction `json:"function"`
}

ToolCall ...

type ToolCallFunction

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

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
	CompletionTokens int
	TotalTokens      int
	Currency         string
	Latency          float64
}

Usage ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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