Documentation
¶
Index ¶
- Variables
- func CompressQueryMessage(messages []Message) (string, error)
- func ConcatQueryMessage(messages []Message) (string, error)
- func LastQueryMessage(messages []Message) (string, error)
- func Register(name string, provider ProviderConstructor)
- func SummarizeQueryMessage(messages []Message) (string, error)
- func WithConfigSupplier(c ConfigSupplier)
- func WithConfigs(c map[string]any)
- func WithSingleConfig(c any)
- type AssistantMessage
- type BaseCache
- type Cache
- type CacheConfig
- type CacheFactory
- type CachedDocument
- type ChatCompletionMessage
- type ChatCompletionRequest
- type ChatCompletionResponseFormat
- type ChatCompletionResponseFormatJSONSchema
- type ChatCompletionResponseFormatType
- type ChatContent
- type ChatImageURL
- type Chunk
- type ChunkChoice
- type ConfigSupplier
- type ConfigSupplierFunc
- type ExactMatchScorer
- type FunctionCallBehavior
- type FunctionDefinition
- type FunctionReference
- type Hook
- type HookOption
- type Instance
- type InvokeOption
- func WithFrequencyPenalty(frequencyPenalty float64) InvokeOption
- func WithMaxTokens(maxTokens int) InvokeOption
- func WithModel(model string) InvokeOption
- func WithStream(stream bool) InvokeOption
- func WithTemperature(temperature float64) InvokeOption
- func WithTools(tools ...*Tool) InvokeOption
- func WithTopK(topK int) InvokeOption
- func WithTopP(topP float64) InvokeOption
- type InvokeOptions
- type MemoCache
- type Message
- type MessageRole
- type Messages
- type Metrics
- type MockLLMModel
- type MultipartContent
- type NoneLogger
- func (w *NoneLogger) DebugContextf(ctx context.Context, format string, args ...interface{})
- func (w *NoneLogger) ErrorContextf(ctx context.Context, format string, args ...interface{})
- func (w *NoneLogger) InfoContextf(ctx context.Context, format string, args ...interface{})
- func (w *NoneLogger) WarnContextf(ctx context.Context, format string, args ...interface{})
- type OAILLM
- type Option
- type Options
- type OtelHook
- type PromptMessage
- func (m PromptMessage) Content() string
- func (m PromptMessage) GetToolCalls() []*ToolCall
- func (m PromptMessage) MarshalJSON() ([]byte, error)
- func (m PromptMessage) MultipartContent() []*MultipartContent
- func (m PromptMessage) Role() MessageRole
- func (m PromptMessage) String() string
- func (m PromptMessage) ToolID() string
- type Provider
- type ProviderConstructor
- type ProviderOptions
- type QueryProcessor
- type RedisCache
- type Response
- type Result
- type Scorer
- type Stream
- type StreamOptions
- type Tool
- type ToolCall
- type ToolCallFunction
- type ToolChoice
- type ToolPromptMessage
- type Usage
Constants ¶
This section is empty.
Variables ¶
var ( ErrCacheMiss = errors.New("cache miss") ErrCacheFull = errors.New("cache is full") )
添加自定义错误类型
var MockLLMModelName = "mock"
Functions ¶
func CompressQueryMessage ¶
CompressQueryMessage ...
func ConcatQueryMessage ¶
ConcatQueryMessage ...
func LastQueryMessage ¶
LastQueryMessage ...
func SummarizeQueryMessage ¶
SummarizeQueryMessage extracts an intelligent summary from conversation messages
Types ¶
type AssistantMessage ¶ added in v0.0.8
type AssistantMessage struct {
PromptMessage
ToolCalls []*ToolCall `json:"tool_calls"`
ReasoningContent string `json:"reasoning_content"`
}
AssistantMessage ...
func NewAssistantMessage ¶ added in v0.0.8
func NewAssistantMessage(text string) *AssistantMessage
NewAssistantMessage ...
func NewAssistantReasoningMessage ¶ added in v0.0.8
func NewAssistantReasoningMessage(text string) *AssistantMessage
NewAssistantReasoningMessage ...
func (AssistantMessage) GetToolCalls ¶ added in v0.0.8
func (m AssistantMessage) GetToolCalls() []*ToolCall
func (*AssistantMessage) String ¶ added in v0.0.8
func (m *AssistantMessage) String() string
func (*AssistantMessage) WithReasoningContent ¶ added in v0.0.8
func (m *AssistantMessage) WithReasoningContent(content string) *AssistantMessage
func (*AssistantMessage) WithToolCalls ¶ added in v0.0.8
func (m *AssistantMessage) WithToolCalls(toolCalls []*ToolCall) *AssistantMessage
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 ...
type CacheConfig ¶
type CacheConfig struct {
TTL time.Duration
MaxEntries int
CleanupInterval time.Duration
EnableMetrics bool
QueryProcessor QueryProcessor
}
CacheConfig 定义缓存的配置选项
type CachedDocument ¶
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 ChatCompletionResponseFormat ¶
type ChatCompletionResponseFormat struct {
Type ChatCompletionResponseFormatType `json:"type,omitempty"`
JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"`
}
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 []*ChunkChoice `json:"choices"`
Usage *Usage `json:"usage"`
}
Chunk ...
type ChunkChoice ¶ added in v0.0.8
type ChunkChoice struct {
Index int `json:"index"`
FinishReason string `json:"finish_reason"`
Logprobs any `json:"logprobs"`
Delta *AssistantMessage `json:"delta"`
}
ChunkChoice ...
type ConfigSupplier ¶
ConfigSupplier ...
var Config ConfigSupplier = &envConfig{}
Config default cache
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
OnLastChunk(context.Context, error)
}
Hook ...
type InvokeOption ¶
type InvokeOption func(*InvokeOptions)
InvokeOption is a function that configures a InvokeOptions.
func WithFrequencyPenalty ¶ added in v0.0.7
func WithFrequencyPenalty(frequencyPenalty float64) InvokeOption
func WithMaxTokens ¶ added in v0.0.7
func WithMaxTokens(maxTokens int) InvokeOption
func WithModel ¶
func WithModel(model string) InvokeOption
WithModel specifies which model name to use.
func WithTemperature ¶ added in v0.0.7
func WithTemperature(temperature float64) InvokeOption
func WithTools ¶
func WithTools(tools ...*Tool) InvokeOption
WithTools specifies which tools to use.
func WithTopK ¶ added in v0.0.7
func WithTopK(topK int) InvokeOption
func WithTopP ¶ added in v0.0.7
func WithTopP(topP float64) InvokeOption
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,omitempty"`
// 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 ...
type Message ¶
type Message interface {
Content() string
MultipartContent() []*MultipartContent
Role() MessageRole
ToolID() string
GetToolCalls() []*ToolCall
String() string
}
Message ...
type MessageRole ¶ added in v0.0.8
type MessageRole string
const ( MessageRoleSystem MessageRole = "system" MessageRoleUser MessageRole = "user" MessageRoleAssistant MessageRole = "assistant" MessageRoleTool MessageRole = "tool" )
type MockLLMModel ¶ added in v0.0.8
type MockLLMModel struct {
}
func (*MockLLMModel) Invoke ¶ added in v0.0.8
func (m *MockLLMModel) Invoke(ctx context.Context, messages []Message, opts *InvokeOptions) (*Response, error)
func (*MockLLMModel) Name ¶ added in v0.0.8
func (m *MockLLMModel) Name() string
type MultipartContent ¶
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 NewOAILLMWithClient ¶ added in v0.0.9
func (*OAILLM) ChatCompletions ¶
func (o *OAILLM) ChatCompletions(ctx context.Context, req ChatCompletionRequest) (io.ReadCloser, error)
ChatCompletions ...
type Option ¶
type Option func(*Options)
Option ...
func WithAPIKey ¶ added in v0.1.0
func WithBaseURL ¶ added in v0.1.0
func WithInvokeOptions ¶ added in v0.0.7
func WithInvokeOptions(o *InvokeOptions) Option
WithInvokeOptions ...
type Options ¶
type Options struct {
*InvokeOptions
// contains filtered or unexported fields
}
Options ...
type OtelHook ¶
type OtelHook struct {
// contains filtered or unexported fields
}
OtelHook ...
func (*OtelHook) OnAfterInvoke ¶
OnAfterInvoke ...
func (*OtelHook) OnBeforeInvoke ¶
OnBeforeInvoke ...
func (*OtelHook) OnFirstChunk ¶
OnFirstChunk ...
type PromptMessage ¶
type PromptMessage struct {
Name string
// contains filtered or unexported fields
}
PromptMessage ...
func NewSystemMessage ¶ added in v0.0.8
func NewSystemMessage(text string) PromptMessage
NewSystemMessage ...
func NewUserMultipartMessage ¶ added in v0.0.8
func NewUserMultipartMessage(contents ...*MultipartContent) PromptMessage
NewUserMultipartMessage ...
func NewUserTextMessage ¶ added in v0.0.8
func NewUserTextMessage(text string) PromptMessage
NewUserTextMessage ...
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) String ¶
func (m PromptMessage) String() string
type ProviderConstructor ¶ added in v0.1.0
type ProviderConstructor func(*ProviderOptions) Provider
type ProviderOptions ¶ added in v0.1.0
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 Result ¶
type Result struct {
Model string `json:"model"`
// Messages []*PromptMessage
Message *AssistantMessage `json:"message"`
Usage *Usage `json:"usage"`
SystemFingerprint string `json:"system_fingerprint"`
}
Result ...
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream ...
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 ¶
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 ToolPromptMessage ¶
type ToolPromptMessage struct {
PromptMessage
// contains filtered or unexported fields
}
ToolPromptMessage ...
func NewToolMessage ¶ added in v0.0.8
func NewToolMessage(text string, toolID string) *ToolPromptMessage
NewToolMessage ...
func (ToolPromptMessage) ToolID ¶ added in v0.0.8
func (m ToolPromptMessage) ToolID() string
ToolID ...