agentscope使用相关 ”调用工具前,请使用提供的工具(view_text_file)查看相关技能文件。“
”调用工具前,请使用提供的工具(view_text_file)查看相关技能文件。“
agentscope-ai/agentscope | DeepWiki
来个使用skill然后可以在agentscope studio里可以对话的示例代码,ollama服务
可在 AgentScope Studio 中对话的示例代码:
# -*- coding: utf-8 -*-
"""The main entry point of the agent skill example."""
import asyncio
import os
from agentscope.agent import ReActAgent
from agentscope.formatter import DashScopeChatFormatter
from agentscope.memory import InMemoryMemory
from agentscope.message import Msg
from agentscope.model import DashScopeChatModel
from agentscope.tool import (
Toolkit,
execute_shell_command,
execute_python_code,
view_text_file,
)
async def main() -> None:
"""The main entry point for the ReAct agent example."""
toolkit = Toolkit()
# To use agent skills, your agent must be equipped with text file viewing
# tools.
toolkit.register_tool_function(execute_shell_command)
toolkit.register_tool_function(execute_python_code)
toolkit.register_tool_function(view_text_file)
# Register the agent skill
toolkit.register_agent_skill(
"./skill/analyzing-agentscope-library",
)
agent = ReActAgent(
name="Friday",
sys_prompt="""You are a helpful assistant named Friday.
# IMPORTANT
- Don't make any assumptions. All your knowledge about AgentScope library must come from your equipped skills.
""", # noqa: E501
model=DashScopeChatModel(
api_key=os.environ.get("DASHSCOPE_API_KEY"),
model_name="qwen3-max",
enable_thinking=False,
stream=True,
),
formatter=DashScopeChatFormatter(),
toolkit=toolkit,
memory=InMemoryMemory(),
)
# First, let's take a look at the agent's system prompt
print("\033[1;32mAgent System Prompt:\033[0m")
print(agent.sys_prompt)
print("\n")
print(
"\033[1;32mResponse to Question 'What skills do you have?':\033[0m",
)
# We prepare two questions
await agent(
Msg("user", "What skills do you have?", "user"),
)
print(
"\n\033[1;32mResponse to Question 'How to create my own tool function "
"for the agent in agentscope?':\033[0m",
)
# The second question
await agent(
Msg(
"user",
"How to custom tool function for the agent in agentscope?",
"user",
),
)
asyncio.run(main())
使用说明:
- 准备 Skill 目录:
- 在项目根目录下创建 skill/analyzing-agentscope-library/ 文件夹
- 在其中放置 SKILL.md 文件,内容参考官方示例中的结构
- Ollama 配置:
- 确保 Ollama 服务正在运行(默认端口 11434)
- 将 model_name 替换为你本地已下载的模型名称
- 在 AgentScope Studio 中运行:
- 将此代码保存为 .py 文件
- 在 Studio 中加载并运行
- Agent 会根据 Skill 定义自动调用相应工具来回答问题完整示例代码结构可参考:https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/agent_skill
- 注意:Skill 的核心是 SKILL.md 文件,其中的 YAML 元数据(name/description)会被自动加载到系统提示词中,而具体内容需要通过 view_text_file 工具在运行时按需读取。
agent怎么访问skill的详细描述
@feudal 在 AgentScope 中,Agent 默认仅加载 Skill 的元数据(如名称和描述),若需访问完整详细内容(如 SKILL.md),必须为其配备文件查看工具(如 view_text_file),由 Agent 在运行时主动调用读取。
这种“按需加载”机制可有效节省 Token、提升推理效率,同时确保在必要时能获取完整的操作指南或 SOP 内容。
相关示例可参考: https://github.com/agentscope-ai/agentscope/blob/main/examples/functionality/agent_skill/README.md
怎么给agent接本地知识文件txt或者md
在 AgentScope 中,为 Agent 接入本地知识文件(如 .txt 或 .md)的核心方法是为其配备 文件查看工具,并让 Agent 在运行时主动调用该工具读取文件内容。
推荐方案:使用内置 view_text_file 工具
AgentScope 框架已内置 view_text_file 工具,专门用于读取文本文件。您只需在初始化 Agent 时将其注册为可用工具即可。
步骤如下:
1. 导入工具:
from agentscope.tool import view_text_file
2. 创建并配置 Agent: 在定义 Agent 时,将 view_text_file 添加到其工具列表中。
from agentscope.agents import DialogAgent
agent = DialogAgent(
name="知识助手",
sys_prompt="你是一个能查阅本地知识库的助手。",
model_config_name="your_model_config", # 替换为您的模型配置名
tools=[view_text_file], # 注册文件查看工具
)
3. 在对话中调用: 当用户提问需要参考本地文件时,Agent 会自动决定调用 view_text_file 来读取指定路径的 .txt 或 .md 文件,并基于文件内容生成回复。
示例代码
from agentscope.agents import DialogAgent
from agentscope.tool import view_text_file
from agentscope.pipelines import sequentialpipeline
# 初始化 Agent
agent = DialogAgent(
name="文档专家",
sys_prompt="请根据提供的本地文档内容回答问题。",
model_config_name="gpt-4", # 请替换为您实际使用的模型配置
tools=[view_text_file],
)
# 运行对话
pipeline = sequentialpipeline([agent])
pipeline("请阅读 ./knowledge/faq.md 并总结其主要内容。")
注意事项
• 文件路径:确保文件路径对运行环境可见且可访问(如相对路径或绝对路径)。
• 按需加载:此机制遵循“按需加载”原则,仅在必要时读取文件,节省 Token 并提升效率。
• 安全限制:若在沙箱环境中运行,请确保文件所在目录已正确挂载(参见 https://runtime.agentscope.io/zh/sandbox/advanced.html#runtime-manager)。
通过这种方式,您可以轻松让 Agent 动态访问和利用本地知识文件,实现灵活的知识增强与上下文支持。
更多推荐



所有评论(0)