OpenProgram Docs

嵌入你自己的技术栈

库调用不会启动 Web UI 或终端 UI,也不能代替完整安装的产品。

宿主 Runtime

from openprogram import Agent, Runtime


def model_call(content, model="host-model", response_format=None):
    # Replace this offline response with the host's model client.
    return "Host model response"

runtime = Runtime(call=model_call, model="host-model")

class ReviewAgent(Agent):
    tools = []

    def classify(self, review: str) -> str:
        """Classify review sentiment."""
        return self(f"Classify review sentiment:\n{review}")

    def summarize(self, review: str) -> str:
        """Summarize a review."""
        sentiment = self.classify(review)
        return self(f"Summarize this {sentiment} review:\n{review}")

reviewer = ReviewAgent(runtime=runtime)
try:
    summary = reviewer.summarize("The service was fast.")
finally:
    runtime.close()

回调收到内容块、模型名和可选的响应格式;这个示例返回固定响应,无需联网。构造 Runtime 不发模型请求。宿主负责关闭自己提供的 Runtime。Agent 方法自动继承它;Context 和函数作用域是自动的。

显式会话

from openprogram import Agent, Context
from openprogram.state.store import SessionStore, SessionNodeWriter

store = SessionStore(root_path="/var/lib/myapp/sessions")
store.create_session("research", agent_id="main")
writer = SessionNodeWriter(store, "research")
researcher = Agent(context=Context(store=writer), tools=[])
try:
    first = researcher("List the project requirements.")
    second = researcher("Review the requirements from the previous call.")
finally:
    store.close()

默认的 Agent 调用彼此独立;Context(store=writer) 延续这个会话。head_id 可选择已有分支末端。store 和 Runtime 归宿主所有。

查看调用

from openprogram.state.store import SessionNodeWriter

for node in SessionNodeWriter(store, "review-42").load():
    print(node.name, node.caller, node.output)

要把方法暴露为模型工具,在 method_options 里设 tool=True,它提供 .spec 和 .execute。仅被记录不授予工具权限。

宿主钩子

钩子位置
set_cancellation_check、CancelledError、check_cancelledopenprogram.host.cancellation
set_session_id_provider(问题路由)openprogram.runtime.agent.call_state

导入检查:test_standalone_embed.py。

Runtime · Agent 与 Context API · Agent 调用与方法

Last updated · 2026-11-25