Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1608f3a

Browse files
authored
docs(Eino): update doc about toolsNode (#1405)
1 parent 9721697 commit 1608f3a

File tree

1 file changed

+89
-13
lines changed
  • content/zh/docs/eino/core_modules/components/tools_node_guide

1 file changed

+89
-13
lines changed

‎content/zh/docs/eino/core_modules/components/tools_node_guide/_index.md‎

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type BaseTool interface {
3030
Info(ctx context.Context) (*schema.ToolInfo, error)
3131
}
3232

33-
// 可调用的工具接口,支持同步调用
33+
// 支持同步调用的工具接口
3434
type InvokableTool interface {
3535
BaseTool
3636
InvokableRun(ctx context.Context, argumentsInJSON string, opts ...Option) (string, error)
@@ -49,7 +49,7 @@ type StreamableTool interface {
4949
- 参数:
5050
- ctx:上下文对象
5151
- 返回值:
52-
- `*schema.ToolInfo`:工具的描述信息
52+
- `*schema.ToolInfo`:工具的描述信息,用于提供给大模型
5353
- error:获取信息过程中的错误
5454

5555
#### **InvokableRun 方法**
@@ -97,6 +97,39 @@ type ToolInfo struct {
9797

9898
Tool 组件使用 ToolOption 来定义可选参数, ToolsNode 没有抽象公共的 option。每个具体的实现可以定义自己的特定 Option,通过 WrapToolImplSpecificOptFn 函数包装成统一的 ToolOption 类型。
9999

100+
```go
101+
package tool
102+
103+
// Option defines call option for InvokableTool or StreamableTool component, which is part of component interface signature.
104+
// Each tool implementation could define its own options struct and option funcs within its own package,
105+
// then wrap the impl specific option funcs into this type, before passing to InvokableRun or StreamableRun.
106+
type Option struct {
107+
implSpecificOptFn any
108+
}
109+
110+
// WrapImplSpecificOptFn wraps the impl specific option functions into Option type.
111+
// T: the type of the impl specific options struct.
112+
// Tool implementations are required to use this function to convert its own option functions into the unified Option type.
113+
// For example, if the tool defines its own options struct:
114+
//
115+
// type customOptions struct {
116+
// conf string
117+
// }
118+
//
119+
// Then the tool needs to provide an option function as such:
120+
//
121+
// func WithConf(conf string) Option {
122+
// return WrapImplSpecificOptFn(func(o *customOptions) {
123+
// o.conf = conf
124+
// }
125+
// }
126+
func WrapImplSpecificOptFn[T any](optFn func(*T)) Option {
127+
return Option{
128+
implSpecificOptFn: optFn,
129+
}
130+
}
131+
```
132+
100133
## **使用方式**
101134

102135
```go
@@ -107,11 +140,16 @@ import (
107140
)
108141

109142
// 创建工具节点
110-
toolsNode := compose.NewToolsNode([]tool.Tool{
111-
searchTool, // 搜索工具
112-
weatherTool, // 天气查询工具
113-
calculatorTool, // 计算器工具
143+
toolsNode, err := compose.NewToolNode(ctx, &compose.ToolsNodeConfig{
144+
Tools: []tool.BaseTool{
145+
searchTool, // 搜索工具
146+
weatherTool, // 天气查询工具
147+
calculatorTool, // 计算器工具
148+
},
114149
})
150+
if err != nil {
151+
return err
152+
}
115153

116154
// Mock LLM 输出作为输入
117155
input := &schema.Message{
@@ -131,6 +169,8 @@ toolMessages, err := toolsNode.Invoke(ctx, input)
131169

132170
ToolsNode 通常不会被单独使用,一般用于编排之中接在 ChatModel 之后。
133171

172+
如果要和 ChatModel 共同使用,即 ChatModel 产生tool call调用指令,Eino解析tool call指令来调用 ToolsNode, 需要调用 ChatModel 的 WithTools() 函数将工具描述信息传递给大模型。
173+
134174
### **在编排中使用**
135175

136176
```go
@@ -140,12 +180,17 @@ import (
140180
"github.com/cloudwego/eino/schema"
141181
)
142182

143-
// 创建工具节点
144-
toolsNode := compose.NewToolsNode([]tool.Tool{
145-
searchTool, // 搜索工具
146-
weatherTool, // 天气查询工具
147-
calculatorTool, // 计算器工具
183+
// 创建工具节点,一个工具节点可包含多种工具
184+
toolsNode, err := compose.NewToolNode(ctx, &compose.ToolsNodeConfig{
185+
Tools: []tool.BaseTool{
186+
searchTool, // 搜索工具
187+
weatherTool, // 天气查询工具
188+
calculatorTool, // 计算器工具
189+
},
148190
})
191+
if err != nil {
192+
return err
193+
}
149194

150195
// 在 Chain 中使用
151196
chain := compose.NewChain[*schema.Message, []*schema.Message]()
@@ -156,7 +201,9 @@ graph := compose.NewGraph[*schema.Message, []*schema.Message]()
156201
graph.AddToolsNode(toolsNode)
157202
```
158203

159-
## **Option 机制**
204+
## **Option 和 Callback 使用**
205+
206+
### **Option 使用示例**
160207

161208
自定义 Tool 可根据自己需要实现特定的 Option:
162209

@@ -176,9 +223,38 @@ func WithTimeout(timeout time.Duration) tool.Option {
176223
o.Timeout = timeout
177224
})
178225
}
226+
227+
type MyTool struct {
228+
options MyToolOptions
229+
}
230+
231+
func (t *MyTool) Info(ctx context.Context) (*schema.ToolInfo, error) {
232+
// 省略具体实现
233+
return nil, err
234+
}
235+
func (t *MyTool) StreamableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (*schema.StreamReader[string], error) {
236+
// 省略具体实现
237+
return nil, err
238+
}
239+
240+
func (t *MyTool) InvokableRun(ctx context.Context, argument string, opts ...tool.Option) (string, error) {
241+
// 将执行编排时传入的自定义配置设置到MyToolOptions中
242+
tmpOptions := tool.GetImplSpecificOptions(&t.options, opts...)
243+
244+
// 根据tmpOptions中Timeout的值处理Timeout逻辑
245+
return "", nil
246+
}
179247
```
180248

181-
## **Option 和 Callback 使用**
249+
执行编排时,可以使用 compose.WithToolsNodeOption() 传入 ToolsNode 相关的Option设置,ToolsNode下的所有 Tool 都能接收到
250+
```go
251+
streamReader, err := graph.Stream(ctx, []*schema.Message{
252+
{
253+
Role: schema.User,
254+
Content: "hello",
255+
},
256+
}, compose.WithToolsNodeOption(compose.WithToolOption(WithTimeout(10 * time.Second))))
257+
```
182258

183259
### **Callback 使用示例**
184260

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /