Define a reasoning schema
from pydantic import BaseModel, Field
class ReasoningOutput(BaseModel):
problem: str = Field(description="The original problem")
thinking_steps: list[str] = Field(description="Step-by-step reasoning")
conclusion: str = Field(description="Final answer")
confidence: str = Field(description="high, medium, or low")
import { z } from 'zod';
const reasoningOutputSchema = z.object({
problem: z.string().describe('The original problem statement'),
thinking_steps: z.array(z.string()).describe('Step-by-step reasoning process'),
conclusion: z.string().describe('The final answer or conclusion'),
confidence: z.string().describe('Confidence level: high, medium, or low'),
});
type ReasoningOutput = z.infer<typeof reasoningOutputSchema>;
Create a thinking agent
from polos import Agent, max_steps, MaxStepsConfig
thinking_agent = Agent(
id="thinking_agent",
provider="openai",
model="gpt-4o-mini",
system_prompt="""You are a logical reasoning expert. When given a problem:
1. Restate the problem
2. Break down your thinking into clear steps
3. Consider potential pitfalls
4. Arrive at a conclusion
5. State your confidence level""",
output_schema=ReasoningOutput,
stop_conditions=[max_steps(MaxStepsConfig(limit=20))],
)
import { defineAgent, maxSteps } from '@polos/sdk';
import { openai } from '@ai-sdk/openai';
const thinkingAgent = defineAgent({
id: 'thinking_agent',
model: openai('gpt-4o-mini'),
systemPrompt:
'You are a logical reasoning expert. When given a problem:\n' +
'1. Restate the problem\n' +
'2. Break down your thinking into clear steps\n' +
'3. Consider potential pitfalls\n' +
'4. Arrive at a conclusion\n' +
'5. State your confidence level',
outputSchema: reasoningOutputSchema,
stopConditions: [maxSteps({ count: 20 })],
});
Get structured reasoning
result = await thinking_agent.run(polos, "If all roses are flowers and some flowers fade quickly, can we conclude that some roses fade quickly?")
print(f"Problem: {result.result.problem}")
for i, step in enumerate(result.result.thinking_steps):
print(f"Step {i+1}: {step}")
print(f"Conclusion: {result.result.conclusion}")
print(f"Confidence: {result.result.confidence}")
const result = await thinkingAgent.stream(polos, {
input: 'If all roses are flowers and some flowers fade quickly, can we conclude that some roses fade quickly?',
});
for await (const chunk of result.textChunks) {
process.stdout.write(chunk);
}
Run it
git clone https://github.com/polos-dev/polos.git
cd polos/python-examples/05-thinking-agent
cp .env.example .env # Add your POLOS_PROJECT_ID and API key
uv sync
python main.py
git clone https://github.com/polos-dev/polos.git
cd polos/typescript-examples/05-thinking-agent
cp .env.example .env # Add your POLOS_PROJECT_ID and API key
npm install
npx tsx main.ts