forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreasoning_agent.py
More file actions
71 lines (57 loc) · 2.45 KB
/
Copy pathreasoning_agent.py
File metadata and controls
71 lines (57 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Reasoning agent — backs `reasoning-default` and `reasoning-custom` cells.
Why this agent uses OpenAIChatClient (Responses API) instead of the
ChatCompletions client used by the other MAF agents in this showcase:
the OpenAI Responses API streams `response.reasoning_summary_text.delta`
items only for native reasoning models (gpt-5, o3, o4-mini, etc.). The
`agent_framework_openai` Responses client translates those into AG-UI
`REASONING_MESSAGE_*` events with `role: "reasoning"`, which the frontend
renders via the built-in `reasoningMessage` slot. Without the Responses
API path, the reasoning slot never lights up — that's the bug the LGP
parity port closes.
Mirrors LangGraph's `langgraph-python/src/agents/reasoning_agent.py`,
which configures `init_chat_model("openai:gpt-5", use_responses_api=True,
reasoning={"effort": "medium", "summary": "detailed"})`.
"""
from __future__ import annotations
import os
from textwrap import dedent
from agent_framework import Agent, BaseChatClient
from agent_framework.openai import OpenAIChatClient
from agent_framework_ag_ui import AgentFrameworkAgent
SYSTEM_PROMPT = dedent(
"""
You are a helpful assistant. For each user question, first think
step-by-step about the approach, then give a concise answer.
"""
).strip()
def _build_reasoning_chat_client() -> BaseChatClient:
"""Build a Responses-API chat client for reasoning-event streaming.
The model env var defaults to `gpt-5` to match the LGP reference; the
deployment can override via `OPENAI_REASONING_MODEL`.
"""
return OpenAIChatClient(
model=os.environ.get("OPENAI_REASONING_MODEL", "gpt-5.4"),
api_key=os.environ.get("OPENAI_API_KEY"),
)
def create_reasoning_agent(
_chat_client_ignored: BaseChatClient | None = None,
) -> AgentFrameworkAgent:
"""Instantiate the reasoning agent.
The shared `chat_client` from `agent_server.py` is intentionally
ignored — this cell needs the Responses API specifically.
"""
base_agent = Agent(
client=_build_reasoning_chat_client(),
name="reasoning_agent",
instructions=SYSTEM_PROMPT,
tools=[],
)
return AgentFrameworkAgent(
agent=base_agent,
name="ReasoningAgent",
description=(
"Reasoning-token streaming via the OpenAI Responses API. "
"Drives `reasoning-default` (built-in slot) and "
"`reasoning-custom` (custom amber ReasoningBlock) demos."
),
)