bot

package
v0.0.0-...-849288d Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 19 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ActAsTellStart = `# 现在支持了以下 Agents
`
	ActAsTellTail = `
当且仅当要使用 agent 时,回复 agent_call::name(问题),比如:
` + CallPrefix + `botheater_basic("接下来查询今天的交易信息")
注意:
- 要调用 agent 时不要回复除调用 agent 以外的内容
- 如果不需要调用 agent, 你的回复一定不要包含这种格式
`
)
View Source
const (

	// FunctionModePrivateOnly 遗忘模式, function 调用过程不会到原始上下文
	FunctionModePrivateOnly FunctionMode = "private"
	// FunctionModeSampleOnly 采样模式, 要求 agent 将 function 调用总结成 sample,只有 sample 会到原始上下文
	FunctionModeSampleOnly FunctionMode = "sample"
	// FunctionModeDump 复制模式, 将这个过程携带在返回中
	FunctionModeDump FunctionMode = "dump"

	FunctionCtxLocal FunctionCtx = "local"
	FunctionCtxAll   FunctionCtx = "all"
)

Variables

View Source
var (
	ErrPrefabNotFound = irr.Error("prefab not found")
	ErrBotNotFound    = irr.Error("bot not found")
)
View Source
var Caller = &call.Caller{Prefix: CallPrefix, Regex: regexp.MustCompile(`agent_call::(\w+)\((.*?)\)`)}

Functions

func InitActAsForBots

func InitActAsForBots(ctx context.Context, allBots ...*Bot)

InitActAsForBots 初始化所有 ActAs

Types

type ActAs

type ActAs string
const (
	ActAsCoordinator ActAs = "coordinator"
	ActAsEvaluator   ActAs = "evaluator"

	CallPrefix = "agent_call::"
)

type Bot

type Bot struct {
	UUID string `yaml:"uuid" json:"uuid"`
	*Config
	// contains filtered or unexported fields
}

func New

func New(conf Config, driver driver.Driver, tm *tool.Manager) *Bot

func (*Bot) ExecuteFunctions

func (b *Bot) ExecuteFunctions(ctx context.Context, historyBeforeFunctionCall history.Messages, trigger string, tempMessages *history.Messages) (string, error)

func (*Bot) InjectCoordinatorPrompt

func (b *Bot) InjectCoordinatorPrompt(allBotConfigs []*Config)

InjectCoordinatorPrompt 注入所有机器人的信息到 Coordinator 的 prompt

func (*Bot) Introduce

func (b *Bot) Introduce(ctx context.Context, historyMessages history.Messages) (string, error)

func (*Bot) Logger

func (b *Bot) Logger(ctx context.Context, funcName string) (*logrus.Entry, context.Context)

func (*Bot) MakeSystemMessage

func (b *Bot) MakeSystemMessage(ctx context.Context, appends ...string) *history.Message

func (*Bot) Messages

func (b *Bot) Messages(ctx context.Context, globalHistory *history.History) history.Messages

func (*Bot) NormalReq

func (b *Bot) NormalReq(ctx context.Context, mergedHistory history.Messages) (string, error)

NormalReq 递归结构,会处理函数调用,不会改变 History

func (*Bot) Question

func (b *Bot) Question(ctx context.Context, h *history.History, question string) (string, error)

Question - 只是一个和 bot 聊天的快捷方式

func (*Bot) SendChat

func (b *Bot) SendChat(ctx context.Context, globalHistory *history.History) (string, error)

func (*Bot) String

func (b *Bot) String() string

func (*Bot) Summarize

func (b *Bot) Summarize(ctx context.Context, messages2Summary history.Messages) (string, error)

func (*Bot) WithArgsReplacer

func (b *Bot) WithArgsReplacer(argsReplacer map[string]any) *Bot

WithArgsReplacer 注入参数替换器,用于替换 prompt 中的占位符

type Config

type Config struct {
	DriverConf driver.Config `yaml:",inline" json:",inline"`

	PrefabName string `yaml:"prefab_name,omitempty" json:"prefab_name,omitempty"`
	Usage      string `yaml:"usage,omitempty" json:"usage,omitempty"`

	Prompt *Prompt `yaml:"prompt,omitempty" json:"prompt,omitempty"`

	// AckAs 表示这个 agent 的固有角色,用于支持多 Agent 模式
	// 根据不同的角色,调度系统将 1. 启用特殊流程 2. 注入信息到 prompt (类似于 function)
	AckAs        ActAs  `yaml:"ack_as,omitempty" json:"ack_as,omitempty"`
	ActAsContext string `yaml:"act_as_context,omitempty" json:"act_as_context,omitempty"`
}

type FunctionCtx

type FunctionCtx string

type FunctionMode

type FunctionMode string

type Loader

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

Loader is a struct that helps to load bots with chainable methods and error handling.

func NewBotLoader

func NewBotLoader(tm *tool.Manager) *Loader

NewBotLoader creates a new Loader instance.

func (*Loader) Error

func (bl *Loader) Error() error

func (*Loader) GetBot

func (bl *Loader) GetBot(name string) (*Bot, error)

GetBot returns the loaded bots and any error encountered.

func (*Loader) GetBots

func (bl *Loader) GetBots() ([]*Bot, error)

GetBots returns the loaded bots and any error encountered.

func (*Loader) LoadBot

func (bl *Loader) LoadBot(ctx context.Context, conf *Config) *Loader

LoadBot loads a bot and adds it to the Loader.

func (*Loader) LoadBots

func (bl *Loader) LoadBots(ctx context.Context, configs map[string]*Config) *Loader

LoadBots loads bots

func (*Loader) StaplingBots

func (bl *Loader) StaplingBots(ctx context.Context, botsStructure any) error

StaplingBots loads bots based on struct tags.

type Prompt

type Prompt struct {
	Content string `yaml:"content,omitempty" json:"content,omitempty" `

	// Functions 放在 Prompt 里,处于对于 Functions 调用是 prompt 的一部分来理解
	// todo: 这个有待考虑,因为实际上一切 Agent 行为都会体现在 Prompt 上,且 Example 可以基于知识库生成,因此这一层或许未必需要
	Functions []string `yaml:"functions,omitempty" json:"functions,omitempty"`

	FunctionCtx  `yaml:"function_ctx,omitempty" json:"function_ctx,omitempty"`
	FunctionMode `yaml:"function_mode,omitempty" json:"function_mode,omitempty"`
}

func (*Prompt) BuildSystemMessage

func (p *Prompt) BuildSystemMessage(ctx context.Context, tm *tool.Manager, arguments map[string]any) *history.Message

func (*Prompt) ReplaceContentBetween

func (p *Prompt) ReplaceContentBetween(startStr, endStr, newContent string) *Prompt

ReplaceContentBetween replaces the content between startStr and endStr with newContent

Jump to

Keyboard shortcuts

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