|
| 1 | +""" |
| 2 | +Example demonstrating how to use the reasoning content feature with models that support it. |
| 3 | +
|
| 4 | +Some models, like deepseek-reasoner, provide a reasoning_content field in addition to the regular content. |
| 5 | +This example shows how to access and use this reasoning content from both streaming and non-streaming responses. |
| 6 | +
|
| 7 | +To run this example, you need to: |
| 8 | +1. Set your OPENAI_API_KEY environment variable |
| 9 | +2. Use a model that supports reasoning content (e.g., deepseek-reasoner) |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import os |
| 14 | +from typing import Any, cast |
| 15 | + |
| 16 | +from agents import ModelSettings |
| 17 | +from agents.models.interface import ModelTracing |
| 18 | +from agents.models.openai_provider import OpenAIProvider |
| 19 | +from agents.types import ResponseOutputRefusal, ResponseOutputText # type: ignore |
| 20 | + |
| 21 | +MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "deepseek-reasoner" |
| 22 | + |
| 23 | + |
| 24 | +async def stream_with_reasoning_content(): |
| 25 | + """ |
| 26 | + Example of streaming a response from a model that provides reasoning content. |
| 27 | + The reasoning content will be emitted as separate events. |
| 28 | + """ |
| 29 | + provider = OpenAIProvider() |
| 30 | + model = provider.get_model(MODEL_NAME) |
| 31 | + |
| 32 | + print("\n=== Streaming Example ===") |
| 33 | + print("Prompt: Write a haiku about recursion in programming") |
| 34 | + |
| 35 | + reasoning_content = "" |
| 36 | + regular_content = "" |
| 37 | + |
| 38 | + async for event in model.stream_response( |
| 39 | + system_instructions="You are a helpful assistant that writes creative content.", |
| 40 | + input="Write a haiku about recursion in programming", |
| 41 | + model_settings=ModelSettings(), |
| 42 | + tools=[], |
| 43 | + output_schema=None, |
| 44 | + handoffs=[], |
| 45 | + tracing=ModelTracing.DISABLED, |
| 46 | + previous_response_id=None, |
| 47 | + prompt=None |
| 48 | + ): |
| 49 | + if event.type == "response.reasoning_summary_text.delta": |
| 50 | + print( |
| 51 | + f"\033[33m{event.delta}\033[0m", end="", flush=True |
| 52 | + ) # Yellow for reasoning content |
| 53 | + reasoning_content += event.delta |
| 54 | + elif event.type == "response.output_text.delta": |
| 55 | + print(f"\033[32m{event.delta}\033[0m", end="", flush=True) # Green for regular content |
| 56 | + regular_content += event.delta |
| 57 | + |
| 58 | + print("\n\nReasoning Content:") |
| 59 | + print(reasoning_content) |
| 60 | + print("\nRegular Content:") |
| 61 | + print(regular_content) |
| 62 | + print("\n") |
| 63 | + |
| 64 | + |
| 65 | +async def get_response_with_reasoning_content(): |
| 66 | + """ |
| 67 | + Example of getting a complete response from a model that provides reasoning content. |
| 68 | + The reasoning content will be available as a separate item in the response. |
| 69 | + """ |
| 70 | + provider = OpenAIProvider() |
| 71 | + model = provider.get_model(MODEL_NAME) |
| 72 | + |
| 73 | + print("\n=== Non-streaming Example ===") |
| 74 | + print("Prompt: Explain the concept of recursion in programming") |
| 75 | + |
| 76 | + response = await model.get_response( |
| 77 | + system_instructions="You are a helpful assistant that explains technical concepts clearly.", |
| 78 | + input="Explain the concept of recursion in programming", |
| 79 | + model_settings=ModelSettings(), |
| 80 | + tools=[], |
| 81 | + output_schema=None, |
| 82 | + handoffs=[], |
| 83 | + tracing=ModelTracing.DISABLED, |
| 84 | + previous_response_id=None, |
| 85 | + prompt=None |
| 86 | + ) |
| 87 | + |
| 88 | + # Extract reasoning content and regular content from the response |
| 89 | + reasoning_content = None |
| 90 | + regular_content = None |
| 91 | + |
| 92 | + for item in response.output: |
| 93 | + if hasattr(item, "type") and item.type == "reasoning": |
| 94 | + reasoning_content = item.summary[0].text |
| 95 | + elif hasattr(item, "type") and item.type == "message": |
| 96 | + if item.content and len(item.content) > 0: |
| 97 | + content_item = item.content[0] |
| 98 | + if isinstance(content_item, ResponseOutputText): |
| 99 | + regular_content = content_item.text |
| 100 | + elif isinstance(content_item, ResponseOutputRefusal): |
| 101 | + refusal_item = cast(Any, content_item) |
| 102 | + regular_content = refusal_item.refusal |
| 103 | + |
| 104 | + print("\nReasoning Content:") |
| 105 | + print(reasoning_content or "No reasoning content provided") |
| 106 | + |
| 107 | + print("\nRegular Content:") |
| 108 | + print(regular_content or "No regular content provided") |
| 109 | + |
| 110 | + print("\n") |
| 111 | + |
| 112 | + |
| 113 | +async def main(): |
| 114 | + try: |
| 115 | + await stream_with_reasoning_content() |
| 116 | + await get_response_with_reasoning_content() |
| 117 | + except Exception as e: |
| 118 | + print(f"Error: {e}") |
| 119 | + print("\nNote: This example requires a model that supports reasoning content.") |
| 120 | + print("You may need to use a specific model like deepseek-reasoner or similar.") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + asyncio.run(main()) |
0 commit comments