Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Description: ""
date: "2025-03-12"
date: "2025-04-21"
lastmod: ""
tags: []
title: 'Eino: ChatTemplate guide'
Expand Down Expand Up @@ -62,6 +62,23 @@ The Prompt component uses Options to define optional parameters. ChatTemplate do

ChatTemplate is generally used for context preparation before ChatModel.

### create chat template

- `prompt.FromMessages()`
- Used to create a template.
- `schema.Message{}`
- schema.Message is a struct that implements the Format interface, so you can directly construct `schema.Message{}` as a template.
- `schema.SystemMessage()`
- This method is a shortcut for constructing a message with the role of "system".
- `schema.AssistantMessage()`
- This method is a shortcut for constructing a message with the role of "assistant".
- `schema.UserMessage()`
- This method is a shortcut for constructing a message with the role of "user".
- `schema.ToolMessage()`
- This method is a shortcut for constructing a message with the role of "tool".
- `schema.MessagesPlaceholder()`
- Can be used to insert a `[]*schema.Message` into a message list, often used for inserting historical conversations.

### **Standalone Usage**

```go
Expand All @@ -72,10 +89,8 @@ import (

// Create template
template := prompt.FromMessages(schema.FString,
&schema.Message{
Role: schema.System,
Content: "You are a {role}.",
},
schema.SystemMessage("You are a {role}."),
schema.MessagesPlaceholder("history_key", false),
&schema.Message{
Role: schema.User,
Content: "Please help me {task}.",
Expand All @@ -86,13 +101,11 @@ template := prompt.FromMessages(schema.FString,
variables := map[string]any{
"role": "professional assistant",
"task": "write a poem",
"history_key": []*schema.Message{{Role: schema.User, Content: "what is golang?"}, {Role: schema.Assistant, Content: "golang is xxx"}},
}

// Format template
messages, err := template.Format(ctx, variables)
if err != nil {
return err
}
messages, err := template.Format(context.Background(), variables)
```

### **Usage in Orchestration**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Description: ""
date: "2025-02-11"
date: "2025-04-21"
lastmod: ""
tags: []
title: 'Eino: Document Parser guide'
Expand Down Expand Up @@ -166,6 +166,7 @@ log.Printf("===== call File Loader directly =====")
loader, err := file.NewFileLoader(ctx, &file.FileLoaderConfig{
// Configuration parameters
UseNameAsID: true,
Parser: &parser.TextParser{}, // use TextParser as default parser
})
if err != nil {
log.Fatalf("file.NewFileLoader failed, err=%v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ _, err = cli.Initialize(ctx, initRequest)
Then use the Client to create the adapter , which implements Eino ChatTemplate:

```go
import "github.com/cloudwego/eino-ext/components/tool/mcp"
import "github.com/cloudwego/eino-ext/components/prompt/mcp"

tpl, err := mcp.NewPromptTemplate(ctx, &mcp.Config{
Cli: cli,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Description: ""
date: "2025-03-12"
date: "2025-04-21"
lastmod: ""
tags: []
title: 'Eino: ChatTemplate 使用说明'
Expand Down Expand Up @@ -65,6 +65,23 @@ Prompt 组件使用 Option 来定义可选参数, ChatTemplate 没有公共的

ChatTemplate 一般用于 ChatModel 之前做上下文准备的。

### 创建方法

- `prompt.FromMessages()`
- 用于把多个 message 变成一个 chat template。
- `schema.Message{}`
- schema.Message 是实现了 Format 接口的结构体,因此可直接构建 `schema.Message{}` 作为 template
- `schema.SystemMessage()`
- 此方法是构建 role 为 "system" 的 message 快捷方法
- `schema.AssistantMessage()`
- 此方法是构建 role 为 "assistant" 的 message 快捷方法
- `schema.UserMessage()`
- 此方法是构建 role 为 "user" 的 message 快捷方法
- `schema.ToolMessage()`
- 此方法是构建 role 为 "tool" 的 message 快捷方法
- `schema.MessagesPlaceholder()`
- 可用于把一个 `[]*schema.Message` 插入到 message 列表中,常用于插入历史对话

### **单独使用**

```go
Expand All @@ -75,10 +92,8 @@ import (

// 创建模板
template := prompt.FromMessages(schema.FString,
&schema.Message{
Role: schema.System,
Content: "你是一个{role}。",
},
schema.SystemMessage("你是一个{role}。"),
schema.MessagesPlaceholder("history_key", false),
&schema.Message{
Role: schema.User,
Content: "请帮我{task}。",
Expand All @@ -89,13 +104,11 @@ template := prompt.FromMessages(schema.FString,
variables := map[string]any{
"role": "专业的助手",
"task": "写一首诗",
"history_key": []*schema.Message{{Role: schema.User, Content: "告诉我油画是什么?"}, {Role: schema.Assistant, Content: "油画是xxx"}},
}

// 格式化模板
messages, err := template.Format(ctx, variables)
if err != nil {
return err
}
messages, err := template.Format(context.Background(), variables)
```

### **在编排中使用**
Expand Down Expand Up @@ -123,6 +136,21 @@ graph := compose.NewGraph[map[string]any, []*schema.Message]()
graph.AddChatTemplateNode("template_node", template)
```

### 从前驱节点的输出中获取数据

在 AddNode 时,可以通过添加 WithOutputKey 这个 Option 来把节点的输出转成 Map:

```go
// 这个节点的输出,会从 string 改成 map[string]any,
// 且 map 中只有一个元素,key 是 your_output_key,value 是实际的的节点输出的 string
graph.AddLambdaNode("your_node_key", compose.InvokableLambda(func(ctx context.Context, input []*schema.Message) (str string, err error) {
// your logic
return
}), compose.WithOutputKey("your_output_key"))
```

把前驱节点的输出转成 map[string]any 并设置好 key 后,在后置的 ChatTemplate 节点中使用该 key 对应的 value。

## **Option 和 Callback 使用**

### **Callback 使用示例**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Description: ""
date: "2025-01-22"
date: "2025-04-21"
lastmod: ""
tags: []
title: 'Eino: Document Parser 接口使用说明'
Expand Down Expand Up @@ -166,6 +166,7 @@ log.Printf("===== call File Loader directly =====")
loader, err := file.NewFileLoader(ctx, &file.FileLoaderConfig{
// 配置参数
UseNameAsID: true,
Parser: &parser.TextParser{}, // 使用 TextParser 作为默认解析器, 可自定义,例如使用 parser.NewExtParser() 创建不同文件类型的解析器
})
if err != nil {
log.Fatalf("file.NewFileLoader failed, err=%v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ _, err = cli.Initialize(ctx, initRequest)
之后使用 Client 创建 Eino ChatTemplate:

```go
import "github.com/cloudwego/eino-ext/components/tool/mcp"
import "github.com/cloudwego/eino-ext/components/prompt/mcp"

tpl, err := mcp.NewPromptTemplate(ctx, &mcp.Config{
Cli: cli,
Expand Down
2 changes: 1 addition & 1 deletion content/zh/docs/eino/overview/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ compiledGraph.Invoke(ctx, input, WithCallbacks(handler).DesignateNode("node_1"))
### 强大的编排 (Graph/Chain/Workflow)

- 数据从 Retriever / Document Loader / ChatTemplate 流向 ChatModel,接着流向 Tool ,并被解析为最终答案。这种通过多个组件的有向、可控的数据流,可以通过**图编排**来实现。
- 组件实例是图的**节点(Node)**,而**边(Edge)**则是数据流通道。
- 组件实例是图的 **节点(Node)** ,而 **边(Edge)** 则是数据流通道。
- 图编排功能强大且足够灵活,能够实现复杂的业务逻辑:
- **类型检查、流处理、并发管理、切面注入和选项分配**都由框架处理。
- 在运行时进行**分支(Branch)**执行、读写全局**状态(State)**,或者使用工作流进行字段级别的数据映射。
Expand Down