agentscope1.0安装与测试
安装
# 从 GitHub 拉取源码
git clone https://github.com/agentscope-ai/agentscope.git# 以可编辑模式安装包
cd agentscope
source .venv/Scripts/activate
uv pip install -e . --index-url https://pypi.tuna.tsinghua.edu.cn/simple
uv pip install python-dotenv
测试
1. 环境变量
.env文件
- DASHSCOPE_API_KEY=sk-xxxx
2. 示例代码
hello.py
from agentscope.agent import ReActAgent, UserAgent
from agentscope.model import DashScopeChatModel
from agentscope.formatter import DashScopeChatFormatter
from agentscope.memory import InMemoryMemory
from agentscope.tool import Toolkit, execute_python_code, execute_shell_command
import os, asynciofrom dotenv import load_dotenv
load_dotenv()async def main():toolkit = Toolkit()toolkit.register_tool_function(execute_python_code)toolkit.register_tool_function(execute_shell_command)agent = ReActAgent(name="Friday",sys_prompt="You're a helpful assistant named Friday.",model=DashScopeChatModel(model_name="qwen-turbo",api_key=os.environ["DASHSCOPE_API_KEY"],stream=True,),memory=InMemoryMemory(),formatter=DashScopeChatFormatter(),toolkit=toolkit,)user = UserAgent(name="user")msg = Nonewhile True:msg = await agent(msg)msg = await user(msg)if msg.get_text_content() == "exit":breakasyncio.run(main())
运行示例
uv run hello.py
输出结构
(agentscope) PS D:\agent-llm\agentscope> uv run .\mytest\hello.pyFriday: Hello! How can I assist you today?User Input: tell me who you areuser: tell me who you areFriday: I am Qwen, a large-scale language model developed by Alibaba Cloud. I can help you with a wide range of tasks, including answering questions, generating text, and more. How can I assist you today?User Input: what's your abilityuser: what's your abilityFriday: I am capable of a wide range of tasks, including but not limited to: answering questions, generating text, creating stories, writing code, and providing explanations. I can also assist with language translation, summarization, and more. How can I help you today?User Input: 帮我写一个简单的Python函数来计算斐波那契数列user: 帮我写一个简单的Python函数来计算斐波那契数列Friday: {"type": "tool_use","id": "call_7be7de124fdc4e99bfb1f0","name": "execute_python_code","input": {"code": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n \n fib_sequence = [0, 1]\n for i in range(2, n):\n fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])\n return fib_sequence\n\n# Example usage: print the first 10 Fibonacci numbers\nprint(fibonacci(10))","timeout": 300}}system: {"type": "tool_result","id": "call_7be7de124fdc4e99bfb1f0","name": "execute_python_code","output": [{"type": "text","text": "<returncode>0</returncode><stdout>[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\r\n</stdout><stderr></stderr>"}]}Friday: 这里是一个简单的Python函数,用于计算斐波那契数列:```pythondef fibonacci(n):if n <= 0:return []elif n == 1:return [0]fib_sequence = [0, 1]for i in range(2, n):fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])return fib_sequence# 示例:打印前10个斐波那契数print(fibonacci(10))```运行结果是:```[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]```这个函数会返回一个包含前 `n` 个斐波那契数的列表。如果有其他问题或需要进一步的帮助,请告诉我!User Input: 创建一个名为 test.txt 的文件user: 创建一个名为 test.txt 的文件Friday: {"type": "tool_use","id": "call_490958d83b914e999e6211","name": "execute_shell_command","input": {"command": "touch test.txt","timeout": 300}}system: {"type": "tool_result","id": "call_490958d83b914e999e6211","name": "execute_shell_command","output": [{"type": "text","text": "Error: 'utf-8' codec can't decode byte 0xb2 in position 8: invalid start byte"}]}Friday: 可能由于编码问题导致错误。我们可以尝试使用不同的方法来创建文件。请告诉我您使用的操作系统,以便我提供更准确的解决方案。User Input: windowsuser: windowsFriday: {"type": "tool_use","id": "call_8c442c5a592d4bb5832d82","name": "execute_shell_command","input": {"command": "type nul > test.txt","timeout": 300}}system: {"type": "tool_result","id": "call_8c442c5a592d4bb5832d82","name": "execute_shell_command","output": [{"type": "text","text": "<returncode>0</returncode><stdout></stdout><stderr></stderr>"}]}Friday: 文件 `test.txt` 已成功在 Windows 系统中创建。如果需要进一步帮助,请随时告诉我!User Input: exituser: exit