Eino中的組件

Eino中有哪些組件?

// Component the name of different kinds of components
type Component string

const (
    ComponentOfPrompt      Component = "ChatTemplate"
    ComponentOfChatModel   Component = "ChatModel"
    ComponentOfEmbedding   Component = "Embedding"
    ComponentOfIndexer     Component = "Indexer"
    ComponentOfRetriever   Component = "Retriever"
    ComponentOfLoader      Component = "Loader"
    ComponentOfTransformer Component = "DocumentTransformer"
    ComponentOfTool        Component = "Tool"
)

組件名 目錄/包名 職責(zé)
ChatModel components/model 對接 LLM,負(fù)責(zé)“生成回答”。
ChatTemplate components/prompt 把變量塞進(jìn)提示模板,生成最終 Prompt。
Embedding components/embedding 把文本變成向量。
Indexer components/indexer 把向量寫進(jìn)索引庫(如 Redis、VikingDB)。
Retriever components/retriever 根據(jù) query 向量去索引庫召回文檔。
DocumentLoader components/loader 從文件/URL/數(shù)據(jù)庫加載原始文檔。
DocumentTransformer components/transformer 對文檔做切塊、過濾、格式轉(zhuǎn)換等。
Tool components/tool 封裝外部 API 或函數(shù),供 LLM 調(diào)用。

不同場景下這些組件如何協(xié)同工作,

場景示例 組件協(xié)作鏈路
對話機(jī)器人 ChatTemplate → ChatModel
RAG 問答 DocumentLoader → DocumentTransformer → Embedding → Indexer → Retriever → ChatTemplate → ChatModel
工具調(diào)用 Agent ChatModel(綁定 Tool)→ Tool → ChatModel

可以通過chain , graph 或者工作流將這些組件串聯(lián)起來, 來實現(xiàn)對應(yīng)的業(yè)務(wù)編排, 如下圖使用graph(有向無環(huán)圖)來實現(xiàn)對話的場景,換成 RAG 只需在中間插入 Embedding、Indexer、Retriever 即可

g := compose.NewGraph[input, output]()
_ = g.AddChatTemplateNode("tpl", tpl)
_ = g.AddChatModelNode("model", model)
_ = g.AddEdge(compose.START, "tpl")
_ = g.AddEdge("tpl", "model")
_ = g.AddEdge("model", compose.END)
runnable, _ := g.Compile(ctx)
image.png

eino中不同組件之間的數(shù)據(jù)流動:

image.png
  • 通過DocumentLoader從文件/URL/數(shù)據(jù)庫加載原始文檔, 并通過Transformer 對文檔進(jìn)行預(yù)處理, 然后通過Embedding 模型將文檔轉(zhuǎn)換為向量(需要依賴第三方向量模型,如豆包的Doubao-embedding-vision或Doubao-embedding)并存入向量數(shù)據(jù)庫(redis等)
  • 基于用戶查詢query, 由Embedding 模型對 query 向量化, 然后ChatModel基于 query 向量從 Redis Vector Database 召回最相關(guān)的文檔, 召回流程參考如下:
階段 組件 作用
1. 用戶輸入 Lambda 將用戶原始 query 轉(zhuǎn)換為字符串
2. 向量化 Embedding 使用模型(如 doubao-embedding-large)將 query 轉(zhuǎn)為向量
3. 向量召回 Retriever 基于 query 向量從 Redis Vector Database 召回最相關(guān)的文檔
4. 返回結(jié)果 schema.Document 返回 []*schema.Document 列表,包含召回的文本內(nèi)容
  • ChatModel基于配置需要或者自行判斷是否使用tool中的函數(shù)調(diào)用,如果需要,由eino框架執(zhí)行工具調(diào)用 , 再把結(jié)果塞回 LLM 做最終回答。


    image.png

工具調(diào)用代碼示例:

// 1. 聲明工具
weatherTool := tools.NewTool(
    "get_weather",
    "查詢指定城市天氣",
    func(ctx context.Context, city string) (string, error) {
        // 這里可調(diào)用真實天氣 API
        return "北京:晴 28℃", nil
    },
)

// 2. 構(gòu)建 Agent
ag, _ := agent.NewAgent(
    chatModel,
    agent.WithTools(weatherTool),
)

// 3. 運(yùn)行
resp, _ := ag.Generate(ctx, []*schema.Message{
    schema.UserMessage("北京今天天氣如何?"),
})
fmt.Println(resp.Content) // → 北京今天晴,最高氣溫 28℃
角色 職責(zé) 代碼示例
Tool 聲明函數(shù)名、描述、參數(shù)結(jié)構(gòu),真正實現(xiàn)邏輯 func GetWeather(location string) (string, error)
Agent 負(fù)責(zé)循環(huán):監(jiān)聽 LLM → 解析 tool_calls → 執(zhí)行工具 → 回傳結(jié)果 agent.NewAgent(chatModel, tools...)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容