Streaming reasoning #1714
johnwlockwood
started this conversation in
Ideas
Replies: 2 comments
|
Streaming reasoning is essential for transparency in agent workflows! Why it matters:
Implementation pattern: import instructor
from openai import OpenAI
from pydantic import BaseModel
class ReasonedResponse(BaseModel):
reasoning: str # Stream this first
answer: str
client = instructor.from_openai(OpenAI())
async def stream_with_reasoning(prompt: str):
response = await client.chat.completions.create(
model="gpt-4",
messages=[{
"role": "user",
"content": f"Think step by step, then answer: {prompt}"
}],
response_model=ReasonedResponse,
stream=True
)
async for partial in response:
if partial.reasoning:
yield f"Thinking: {partial.reasoning}"
if partial.answer:
yield f"Answer: {partial.answer}"For chain-of-thought: class CoTResponse(BaseModel):
steps: list[str] # Each step streams
final_answer: strChallenges:
We've built streaming reasoning UIs at RevolutionAI for transparent agent workflows. Happy to share patterns! What's the specific use case — chatbot or autonomous agent? |
0 replies
|
Streaming reasoning is game-changing! At RevolutionAI (https://revolutionai.io) we use this for real-time UX. Pattern we use: import instructor
from pydantic import BaseModel
class ReasonedResponse(BaseModel):
thinking: str
answer: str
client = instructor.from_openai(openai.OpenAI())
# Stream with partial updates
for partial in client.chat.completions.create_partial(
model="gpt-4-turbo",
response_model=ReasonedResponse,
messages=[{"role": "user", "content": query}],
stream=True
):
if partial.thinking:
print(f"Thinking: {partial.thinking}")
if partial.answer:
print(f"Answer: {partial.answer}")Benefits:
Streaming + structured = best of both worlds! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
A user asked if I could show the LLM's reasoning as it's thinking about producing the structured output. I think it would be great to see it as they are waiting. It offers users more transparency and an understanding of what's happening. It will build anticipation about the result instead of making them feel like they are waiting.
All reactions