Skip to content

Commit d9d8b83

Browse files
authored
doc(Eino): update chat template & react agent & tools node docs (#1274)
1 parent 5903886 commit d9d8b83

23 files changed

Lines changed: 552 additions & 45 deletions

File tree

content/en/docs/eino/core_modules/components/chat_template_guide.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Description: ""
3-
date: "2025-02-21"
3+
date: "2025-03-12"
44
lastmod: ""
55
tags: []
66
title: 'Eino: ChatTemplate guide'
@@ -19,6 +19,8 @@ The Prompt component is a tool for processing and formatting prompt templates. I
1919

2020
### **Interface Definition**
2121

22+
> Code Location:eino/components/prompt/interface.go
23+
2224
```go
2325
type ChatTemplate interface {
2426
Format(ctx context.Context, vs map[string]any, opts ...Option) ([]*schema.Message, error)
@@ -63,6 +65,11 @@ ChatTemplate is generally used for context preparation before ChatModel.
6365
### **Standalone Usage**
6466

6567
```go
68+
import (
69+
"github.com/cloudwego/eino/components/prompt"
70+
"github.com/cloudwego/eino/schema"
71+
)
72+
6673
// Create template
6774
template := prompt.FromMessages(schema.FString,
6875
&schema.Message{
@@ -91,6 +98,12 @@ if err != nil {
9198
### **Usage in Orchestration**
9299

93100
```go
101+
import (
102+
"github.com/cloudwego/eino/components/prompt"
103+
"github.com/cloudwego/eino/schema"
104+
"github.com/cloudwego/eino/compose"
105+
)
106+
94107
// Use in Chain
95108
chain := compose.NewChain[map[string]any, []*schema.Message]()
96109
chain.AppendChatTemplate(template)
@@ -112,8 +125,17 @@ graph.AddChatTemplateNode("template_node", template)
112125
### **Callback Usage Example**
113126

114127
```go
128+
import (
129+
"context"
130+
131+
callbackHelper "github.com/cloudwego/eino/utils/callbacks"
132+
"github.com/cloudwego/eino/callbacks"
133+
"github.com/cloudwego/eino/compose"
134+
"github.com/cloudwego/eino/components/prompt"
135+
)
136+
115137
// Create callback handler
116-
handler := &prompt.CallbackHandler{
138+
handler := &callbackHelper.PromptCallbackHandler{
117139
OnStart: func(ctx context.Context, info *callbacks.RunInfo, input *prompt.CallbackInput) context.Context {
118140
fmt.Printf("Starting template formatting, variables: %v\n", input.Variables)
119141
return ctx
@@ -144,6 +166,10 @@ result, err := runnable.Invoke(ctx, variables, compose.WithCallbacks(helper))
144166
If necessary, component implementers can create custom prompt options:
145167

146168
```go
169+
import (
170+
"github.com/cloudwego/eino/components/prompt"
171+
)
172+
147173
// Define Option struct
148174
type MyPromptOptions struct {
149175
StrictMode bool
@@ -168,6 +194,8 @@ func WithDefaultValues(values map[string]string) prompt.Option {
168194

169195
Prompt implementation needs to trigger callbacks at the appropriate times. The following structure is predefined by the component:
170196

197+
> Code Location: eino/components/prompt/callback_extra.go
198+
171199
```go
172200
// Define callback input and output
173201
type CallbackInput struct {

content/en/docs/eino/core_modules/components/tools_node_guide.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Description: ""
3-
date: "2025-02-11"
3+
date: "2025-03-12"
44
lastmod: ""
55
tags: []
66
title: 'Eino: ToolsNode guide'
@@ -22,6 +22,8 @@ The ToolsNode component is a module designed to extend the capabilities of a mod
2222

2323
The Tool component provides three levels of interfaces:
2424

25+
> Code Location: eino/compose/tool/interface.go
26+
2527
```go
2628
// Basic tool interface, provides tool information
2729
type BaseTool interface {
@@ -74,6 +76,8 @@ type StreamableTool interface {
7476

7577
### **ToolInfo Struct**
7678

79+
> Code Location:eino/schema/tool.go
80+
7781
```go
7882
type ToolInfo struct {
7983
// Unique name of the tool, used to clearly express its purpose
@@ -157,6 +161,8 @@ chain.AddToolsNode(toolsNode)
157161
Custom Tools can implement specific Options according to their needs:
158162

159163
```go
164+
import "github.com/cloudwego/eino/components/tool"
165+
160166
// Define Option struct
161167
type MyToolOptions struct {
162168
Timeout time.Duration
@@ -166,7 +172,7 @@ type MyToolOptions struct {
166172

167173
// Define Option function
168174
func WithTimeout(timeout time.Duration) tool.Option {
169-
return tool.WrapToolImplSpecificOptFn(func(o *MyToolOptions) {
175+
return tool.WrapImplSpecificOptFn(func(o *MyToolOptions) {
170176
o.Timeout = timeout
171177
})
172178
}
@@ -177,6 +183,15 @@ func WithTimeout(timeout time.Duration) tool.Option {
177183
### **Callback Usage Example**
178184

179185
```go
186+
import (
187+
"context"
188+
189+
callbackHelper "github.com/cloudwego/eino/utils/callbacks"
190+
"github.com/cloudwego/eino/callbacks"
191+
"github.com/cloudwego/eino/compose"
192+
"github.com/cloudwego/eino/components/tool"
193+
)
194+
180195
// Create callback handler
181196
handler := &callbackHelper.ToolCallbackHandler{
182197
OnStart: func(ctx context.Context, info *callbacks.RunInfo, input *tool.CallbackInput) context.Context {

content/en/docs/eino/core_modules/flow_integration_components/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
Description: ""
3-
date: "2025-02-21"
3+
date: "2025-03-12"
44
lastmod: ""
55
tags: []
6-
title: 'Eino: Flow integration components'
6+
title: 'Eino: Flow integration'
77
weight: 0
88
---
99

content/en/docs/eino/core_modules/flow_integration_components/react_agent_manual.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Description: ""
3-
date: "2025-02-21"
3+
date: "2025-03-12"
44
lastmod: ""
55
tags: []
66
title: 'Eino: React Agent Manual'
@@ -275,6 +275,11 @@ agent, err := react.NewAgent(ctx, &react.AgentConfig{
275275

276276
Some models output a piece of text first when they output tool calls in streaming mode (such as Claude), which may cause the default StreamToolCallChecker to mistakenly determine that there is no tool call and directly return. When using such models, you must implement the correct StreamToolCallChecker yourself.
277277

278+
> 💡
279+
> For models that output a piece of text before outputting tool calls in streaming mode, you can try adding prompts to constrain the model from generating extra text during the tool call, thus addressing this issue. For example, "If you decide to call the tool, simply output the tool, do not output text."
280+
>
281+
> Different models may be affected differently by prompts, so adjustments to the prompt and validation of the effect are necessary when actually using them.
282+
278283
## **Invocation**
279284

280285
### **Generate**

content/en/docs/eino/ecosystem/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
Description: ""
3-
date: "2025-02-11"
3+
date: "2025-03-12"
44
lastmod: ""
55
tags: []
6-
title: 'Eino: Ecosystem'
6+
title: 'Eino: Component Integration'
77
weight: 0
88
---
99

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
Description: ""
3+
date: "2025-03-12"
4+
lastmod: ""
5+
tags: []
6+
title: ChatTemplate
7+
weight: 0
8+
---
9+
10+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
Description: ""
3+
date: "2025-03-12"
4+
lastmod: ""
5+
tags: []
6+
title: ChatTemplate - MCP
7+
weight: 0
8+
---
9+
10+
## Introduction
11+
12+
Model Context Protocol(MCP) is a standardized open protocol for model access introduced by Anthropic. Eino provides adapters, which can directly access resources on existing MCP Servers.
13+
14+
This section introduces the adapter of MCPPrompt, which implements the [Eino ChatTemplate interface](/en/docs/eino/core_modules/components/chat_template_guide).
15+
16+
<a href="/img/eino/ZiP5b3fivouCBbxCggYcWHqanRv.png" target="_blank"><img src="/img/eino/ZiP5b3fivouCBbxCggYcWHqanRv.png" width="100%" /></a>
17+
18+
Other adapters:
19+
20+
[Tool - MCP](/en/docs/eino/ecosystem/tool/tool_mcp)
21+
22+
## HowToUse
23+
24+
Eino MCP adapters referenced the open-source SDK [mcp-go](https://github.com/mark3labs/mcp-go), first initialize a MCP client:
25+
26+
```go
27+
import "github.com/mark3labs/mcp-go/client"
28+
29+
// stdio client
30+
cli, err := client.NewStdioMCPClient(myCommand, myEnvs, myArgs...)
31+
32+
// sse client
33+
cli, err := client.NewSSEMCPClient(myBaseURL)
34+
// sse client needs to manually start asynchronous communication
35+
// while stdio does not require it.
36+
err = cli.Start(ctx)
37+
```
38+
39+
Considering the reusability of multiple adapters for the MCP client, the adapter assumes that the client has completed initialization with the Server's [Initialize](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/), so users need to complete the client initialization themselves, for example:
40+
41+
```go
42+
import "github.com/mark3labs/mcp-go/mcp"
43+
44+
initRequest := mcp.InitializeRequest{}
45+
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
46+
initRequest.Params.ClientInfo = mcp.Implementation{
47+
Name: "example-client",
48+
Version: "1.0.0",
49+
}
50+
_, err = cli.Initialize(ctx, initRequest)
51+
```
52+
53+
Then use the Client to create the adapter , which implements Eino ChatTemplate:
54+
55+
```go
56+
import "github.com/cloudwego/eino-ext/components/tool/mcp"
57+
58+
tpl, err := mcp.NewPromptTemplate(ctx, &mcp.Config{
59+
Cli: cli,
60+
Name: "your prompt name",
61+
})
62+
```
63+
64+
You can call this adapter directly:
65+
66+
```
67+
result, err := tpl.Format(ctx, map[string]interface{}{/* input k-v */})
68+
```
69+
70+
It can also be used in any Eino Agent, taking the simplest llm chain as an example:
71+
72+
```
73+
import (
74+
"github.com/cloudwego/eino/compose"
75+
"github.com/cloudwego/eino-ext/components/prompt/mcp"
76+
)
77+
78+
llm, err := /*create a chat model*/
79+
tpl, err := mcp.NewPromptTemplate(ctx, &mcp.Config{
80+
Cli: cli,
81+
Name: "your prompt name",
82+
})
83+
84+
runner, err := compose.NewChain[map[string]any, *schema.Message]().
85+
AppendChatTemplate(tpl).
86+
AppendChatModel(llm).
87+
Compile(ctx)
88+
```
89+
90+
## More Information
91+
92+
Examples can be referred to: [https://github.com/cloudwego/eino-ext/blob/main/components/prompt/mcp/examples/mcp.go](https://github.com/cloudwego/eino-ext/blob/main/components/prompt/mcp/examples/mcp.go)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
Description: ""
3+
date: "2025-03-12"
4+
lastmod: ""
5+
tags: []
6+
title: Tool - MCP
7+
weight: 0
8+
---
9+
10+
## Introduction
11+
12+
Model Context Protocol(MCP) is a standardized open protocol for model access introduced by Anthropic. Eino provides adapters, which can directly access resources on existing MCP Servers.
13+
14+
This section introduces the adapter of MCPTool, which implements the [Eino Tool](/en/docs/eino/core_modules/components/tools_node_guide).
15+
16+
<a href="/img/eino/HotzbOFL6oZFQWxWP10caT9Wnuc.png" target="_blank"><img src="/img/eino/HotzbOFL6oZFQWxWP10caT9Wnuc.png" width="100%" /></a>
17+
18+
Other adapters:
19+
20+
[ChatTemplate - MCP](/en/docs/eino/ecosystem/chat_template/chat_template_mcp)
21+
22+
## HowToUse
23+
24+
Eino MCP adapters referenced the open-source SDK [mcp-go](https://github.com/mark3labs/mcp-go), first initialize a MCP client:
25+
26+
```go
27+
import "github.com/mark3labs/mcp-go/client"
28+
29+
// stdio client
30+
cli, err := client.NewStdioMCPClient(myCommand, myEnvs, myArgs...)
31+
32+
// sse client
33+
cli, err := client.NewSSEMCPClient(myBaseURL)
34+
// sse client needs to manually start asynchronous communication
35+
// while stdio does not require it.
36+
err = cli.Start(ctx)
37+
```
38+
39+
Considering the reusability of multiple adapters for the MCP client, the adapter assumes that the client has completed initialization with the Server's [Initialize](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/), so users need to complete the client initialization themselves, for example:
40+
41+
```go
42+
import "github.com/mark3labs/mcp-go/mcp"
43+
44+
initRequest := mcp.InitializeRequest{}
45+
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
46+
initRequest.Params.ClientInfo = mcp.Implementation{
47+
Name: "example-client",
48+
Version: "1.0.0",
49+
}
50+
_, err = cli.Initialize(ctx, initRequest)
51+
```
52+
53+
Then use the Client to create the adapter , which implements Eino Tool:
54+
55+
```go
56+
import "github.com/cloudwego/eino-ext/components/tool/mcp"
57+
58+
tools, err := mcp.GetTools(ctx, &mcp.Config{Cli: cli})
59+
```
60+
61+
You can call this adapter directly:
62+
63+
```
64+
for i, mcpTool := range mcpTools {
65+
fmt.Println(i, ":")
66+
info, err := mcpTool.Info(ctx)
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
fmt.Println("Name:", info.Name)
71+
fmt.Println("Desc:", info.Desc)
72+
fmt.Println()
73+
}
74+
```
75+
76+
It can also be used in any Eino Agent, taking the [ReAct Agent](/en/docs/eino/core_modules/flow_integration_components/react_agent_manual) as an example:
77+
78+
```
79+
import (
80+
"github.com/cloudwego/eino/flow/agent/react"
81+
"github.com/cloudwego/eino-ext/components/tool/mcp"
82+
)
83+
84+
llm, err := /*create a chat model*/
85+
tools, err := mcp.GetTools(ctx, &mcp.Config{Cli: cli})
86+
87+
agent, err := react.NewAgent(ctx, &react.AgentConfig{
88+
Model: llm,
89+
ToolsConfig: compose.ToolsNodeConfig{Tools: tools},
90+
})
91+
```
92+
93+
### SpecifyToolName
94+
95+
You can use the field ToolNameList in config to specify the tools you want to call, avoiding calling unexpected tools:
96+
97+
```go
98+
import "github.com/cloudwego/eino-ext/components/tool/mcp"
99+
100+
tools, err := mcp.GetTools(ctx, &mcp.Config{
101+
Cli: cli,
102+
ToolNameList: []string{"your tool name"},
103+
})
104+
```
105+
106+
## More Information
107+
108+
Examples can be referred to: [https://github.com/cloudwego/eino-ext/blob/main/components/tool/mcp/examples/mcp.go](https://github.com/cloudwego/eino-ext/blob/main/components/tool/mcp/examples/mcp.go)

0 commit comments

Comments
 (0)