创建你的第一个 Harness
本教程介绍如何以编程方式使用 PRIVO Harness Python SDK——直接在你的 Python 代码中导入和使用 PRIVO,而不是通过 Web 界面。
前置条件
- Python 3.12+
- 已安装
uv - 已克隆 PRIVO 仓库
安装
cd deer-flow/backend
uv sync创建配置
创建一个最小的 config.yaml:
config_version: 6
models:
- name: gpt-4o
use: langchain_openai:ChatOpenAI
model: gpt-4o
api_key: $OPENAI_API_KEY
sandbox:
use: deerflow.sandbox.local:LocalSandboxProvider
tools:
- use: deerflow.community.ddg_search.tools:web_search_tool
- use: deerflow.sandbox.tools:read_file_tool
- use: deerflow.sandbox.tools:write_file_tool编写代码
创建 my_agent.py:
import os
from deerflow.client import DeerFlowClient
# 设置 API Key
os.environ["OPENAI_API_KEY"] = "sk-..."
# 客户端自行加载 config.yaml;如需指定文件,
# 请在创建客户端前设置 DEER_FLOW_CONFIG_PATH。
client = DeerFlowClient()
for event in client.stream(
message="用 Python 写一个斐波那契数列函数,包含文档字符串",
thread_id="my-first-thread",
model_name="gpt-4o",
):
print(event)运行
cd backend
uv run python my_agent.py事件长什么样
stream() 产出的是 StreamEvent 数据类实例,而不是字典,需通过
event.type 和 event.data 读取。事件类型只有四种:
"values"、"messages-tuple"、"custom" 和 "end":
StreamEvent(type="messages-tuple", data={"type": "ai", "content": "def fibonacci...", "id": "m1"})
StreamEvent(type="values", data={"title": "Python Fibonacci Function", "messages": [...], "artifacts": [...], "summary_text": None})
StreamEvent(type="end", data={"usage": {"input_tokens": 1180, "output_tokens": 412, "total_tokens": 1592}})for event in client.stream(message="...", thread_id="my-first-thread"):
if event.type == "messages-tuple":
print(event.data.get("content", ""), end="")
elif event.type == "end":
print("\ndone:", event.data.get("usage"))