engine

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 13 Imported by: 0

README

# Engine

The engine is the core of the app. It is responsible for build an AI app, the
rendering, the input, the sound, the physics, the AI, the networking, etc.

Documentation

Index

Constants

View Source
const (
	// BotModeUnknown TODO
	BotModeUnknown = iota
	// BotModeProxy TODO
	BotModeProxy
	// BotModeChat TODO
	BotModeChat
	// BotModeWorkflow TODO
	BotModeWorkflow
	// BotModeSingleAgent TODO
	BotModeSingleAgent
	// BotModeMultiAgent TODO
	BotModeMultiAgent
	// BotModeSpeech TODO
	BotModeSpeech = 99
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentEngine

type AgentEngine struct {
	ConvID    int64
	MessageID int64

	BotEngine
	// contains filtered or unexported fields
}

AgentEngine ...

func (*AgentEngine) Execute

func (engine *AgentEngine) Execute(ctx context.Context, input Input) *EventStream

Execute ... return channel ReAct mode or Function Call mode.

type BotEngine

type BotEngine struct {
	SessionID string
	MessageID int64

	HistoryMessages []llm.Message

	Settings *Settings
	Hooks    []Hook
	// contains filtered or unexported fields
}

BotEngine ...

func NewBotEngine

func NewBotEngine(opts ...Option) *BotEngine

NewBotEngine ...

func (*BotEngine) AfterLLMFinish

func (r *BotEngine) AfterLLMFinish(ctx context.Context, err error)

AfterLLMFinish ...

func (*BotEngine) BeforeLLMStart

func (r *BotEngine) BeforeLLMStart(ctx context.Context) context.Context

BeforeLLMStart ...

func (*BotEngine) Context

func (r *BotEngine) Context() context.Context

Context ...

func (*BotEngine) FetchHistoryMessages

func (r *BotEngine) FetchHistoryMessages(ctx context.Context) []llm.Message

FetchHistoryMessages ...

func (*BotEngine) HookOnFinish

func (r *BotEngine) HookOnFinish(ctx context.Context, err error)

HookOnFinish ...

func (*BotEngine) HookOnStart

func (r *BotEngine) HookOnStart(ctx context.Context) context.Context

HookOnStart ...

func (*BotEngine) RenderMessages

func (r *BotEngine) RenderMessages(ctx context.Context, preset string,
	inputs map[string]any, query string, contexts string) ([]llm.Message, []string)

RenderMessages ...

func (*BotEngine) WithContext

func (r *BotEngine) WithContext(ctx context.Context) *BotEngine

WithContext ...

type ChatEngine

type ChatEngine struct {
	*BotEngine
}

ChatEngine ...

func NewChatEngine

func NewChatEngine(settings *Settings, opts ...Option) *ChatEngine

NewChatEngine ...

func (*ChatEngine) Execute

func (r *ChatEngine) Execute(ctx context.Context, input Input) *EventStream

Execute ... return channel

func (*ChatEngine) Invoke

func (r *ChatEngine) Invoke(ctx context.Context, input Input) (any, error)

Invoke ... return channel

type Engine

type Engine interface {
	Execute(ctx context.Context, input Input) *EventStream
}

Engine ...

func BuildEngine

func BuildEngine(mode int, settings *Settings, opts ...Option) Engine

BuildEngine ...

func NewAgentEngine

func NewAgentEngine(settings *Settings, opts ...Option) Engine

NewAgentEngine ...

func NewProxyEngine

func NewProxyEngine(settings *Settings, opts ...Option) Engine

NewProxyEngine ...

type Event

type Event struct {
	Data     any
	Name     EventName
	Error    error
	Source   string
	SourceID string
}

Event 消息事件接口 *llm.Chunk ThoughID string

func EndEvent

func EndEvent(data string) *Event

EndEvent ...

func EndEventWithSource

func EndEventWithSource(data any, source, sourceID string) *Event

EndEventWithSource ...

func ErrorEvent

func ErrorEvent(data error) *Event

ErrorEvent ...

func LLMChunkEvent

func LLMChunkEvent(data *llm.Chunk) *Event

LLMChunkEvent ...

func LLMResultEvent

func LLMResultEvent(data *llm.Result) *Event

LLMResultEvent ...

func ToastEvent

func ToastEvent(data any) *Event

ToastEvent ...

func WorkflowEvent

func WorkflowEvent(evt *workflow.Event) *Event

WorkflowEvent ...

func WorkflowResultEvent

func WorkflowResultEvent(data *workflow.Result) *Event

WorkflowResultEvent ...

func (Event) String

func (e Event) String() string

type EventName

type EventName string

EventName 枚举类型

const (
	EventToast   EventName = "toast"
	EventThought EventName = "thought"
	EventEnd     EventName = "end"
	EventError   EventName = "error"
	Ping         EventName = "ping"
	Stop         EventName = "stop"
)

枚举值列表

type EventStream

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

EventStream ...

func NewEventStream

func NewEventStream() *EventStream

NewEventStream ...

func (*EventStream) Close

func (s *EventStream) Close()

Close ...

func (*EventStream) Next

func (s *EventStream) Next() *Event

Next ...

func (*EventStream) Push

func (s *EventStream) Push(e *Event)

Push ...

type Extra

type Extra struct {
	Responder   string
	ResponderID string
}

Extra ...

type Hook

type Hook interface {
	OnStart(context.Context) context.Context
	OnFinish(context.Context, error)

	BeforeRetrieveStart(context.Context)
	AfterRetrieveFinish(context.Context)

	BeforeToolStart(context.Context, int64, map[string]any)
	AfterToolFinish(context.Context, int64, map[string]any, error)

	OnReasonStart(context.Context)
	OnReasonFinish(context.Context)

	BeforeLLMStart(context.Context) context.Context
	AfterLLMFinish(context.Context, error)
}

Hook ...

type Input

type Input struct {
	Inputs    map[string]any `json:"inputs"`
	Query     string         `json:"query"`
	TurnID    string         `json:"turn_id"`
	SceneID   string         `json:"scene_id"`
	Turnround int            `json:"turnround"`
	ChunkID   string         `json:"chunk_id"`
}

Input ...

type Memory

type Memory interface {
	FetchMemories(context.Context, int64) ([]llm.Message, error)
	SaveMemory(context.Context, int64, string, *Extra) error
}

Memory ...

type MemoryOption

type MemoryOption func(*MemoryOptions)

type MemoryOptions

type MemoryOptions struct {
	Limit int
}

type Option

type Option func(o *Options)

Option ...

func WithConversation

func WithConversation(m int64) Option

WithConversation ...

func WithHook

func WithHook(h Hook) Option

WithHook ...

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger ...

func WithMemory

func WithMemory(m Memory) Option

WithMemory ...

func WithMessageID

func WithMessageID(m int64) Option

WithMessageID ...

func WithRag

func WithRag(rag *retrival.Retrival) Option

WithRag ...

type Options

type Options struct {
	Rag            *retrival.Retrival
	Memory         Memory
	ConversationID int64
	MessageID      int64
	Hooks          []Hook
	// contains filtered or unexported fields
}

Options ...

type ProxyEngine

type ProxyEngine struct {
	*BotEngine
}

ProxyEngine ...

func (*ProxyEngine) Execute

func (engine *ProxyEngine) Execute(ctx context.Context, input Input) *EventStream

Execute ... return channel, not support streaming

func (*ProxyEngine) Invoke

func (engine *ProxyEngine) Invoke(ctx context.Context, input Input) (any, error)

Invoke ... return channel, 不支持streaming

type Settings

type Settings struct {
	PresetPrompt string        `json:"preset_prompt"`
	Preamble     string        `json:"preamble"` // 机器人开场白
	Knowledge    []rag.Options `json:"knowledge"`
	LLMModel     struct {
		Provider string `json:"provider"`
		Name     string `json:"name"`
	} `json:"llm_model"`
	Agent struct {
		Mode         string `json:"mode"`
		MaxIteration int    `json:"max_iteration"`
	}
	Workflow *workflow.Workflow `json:"workflow"`
	Tools    []string           `json:"tools"`
	Stream   bool               `json:"stream"`
}

Settings ...

func DefaultSettings

func DefaultSettings() *Settings

DefaultSettings ...

func (*Settings) String

func (s *Settings) String() string

String ... return json string

type WorkflowEngine

type WorkflowEngine struct {
	BotEngine
}

WorkflowEngine ...

func NewWorkflowEngine

func NewWorkflowEngine(settings *Settings, opts ...Option) *WorkflowEngine

NewWorkflowEngine ...

func (*WorkflowEngine) Execute

func (engine *WorkflowEngine) Execute(ctx context.Context, input Input) *EventStream

Execute ... return channel, 不支持streaming

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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