导入
文本生成与对话
学习如何使用 OpenAI SDK 中的内置功能自定义响应。
...
1from openai import OpenAI 2client = OpenAI() 3 4response = client.responses.create( 5 model="gpt-5.2", 6 input=[ 7 {"role": "system", "content": "You are a helpful tutor who explains complex topics in simple terms with analogies."}, 8 {"role": "user", "content": "What is machine learning?"} 9 ] 10) 11 12print(response.output_text)
1conversation = [ 2 {"role": "system", "content": "You are a helpful assistant."}, 3 {"role": "user", "content": "My favorite color is blue."}, 4 {"role": "assistant", "content": "That's nice! Blue is a beautiful color. It's often associated with calmness and serenity."}, 5 {"role": "user", "content": "What are some things in nature that match my favorite color?"} 6] 7 8response = client.responses.create( 9 model="gpt-5.2", 10 input=conversation 11) 12 13print(response.output_text)
1# Conservative, factual response 2 3print("Conservative,factual response:") 4response_conservative = client.responses.create( 5 model="gpt-4o", 6 input="What is the name of the manga where we have a white haird character as the main protagonist? Only provide two names!", 7 temperature=0.1 8) 9 10print("-" * 100) 11print(response_conservative.output_text) 12print("-" * 100) 13print("Creative, varied response:") 14print("-" * 100) 15# Creative, varied response 16response_creative = client.responses.create( 17 model="gpt-4o", 18 input="What is the name of the manga where we have a white haird character as the main protagonist? Only provide two names!", 19 temperature=0.9 20) 21 22print(response_creative.output_text) 23print("-" * 100)
1# Short response 2print("Short Response:") 3response = client.responses.create( 4 model="gpt-4o", 5 input="Explain artificial intelligence.", 6 max_output_tokens=30 # About 1-2 sentences 7) 8print(response.output_text) 9print("-" * 100) 10 11# Longer response 12print("Longer Response:") 13response = client.responses.create( 14 model="gpt-4o", 15 input="Explain artificial intelligence.", 16 max_output_tokens=200 # About 1 paragraph 17) 18 19print(response.output_text) 20print("-" * 100)
1""" 2GPT-5 Reasoning Effort Demo 3=========================== 4 5This script demonstrates how the reasoning_effort parameter controls 6the quality and depth of GPT-5's thinking process, and measures the 7performance impact of different reasoning levels. 8""" 9import time 10# Set up OpenAI client 11# Make sure your OPENAI_API_KEY environment variable is set 12def demo_reasoning_effort(): 13 """ 14 Demonstrates the reasoning_effort parameter. 15 16 reasoning_effort controls how much computational "thinking" the model does: 17 - minimal: Fastest, for simple deterministic tasks 18 - low: Light reasoning 19 - medium: Default balanced reasoning 20 - high: Most thorough reasoning and analysis 21 """ 22 print("=" * 60) 23 print("GPT-5 REASONING EFFORT DEMONSTRATION") 24 print("=" * 60) 25 26 # Test prompt that benefits from reasoning 27 problem = "If a train leaves Station A at 2:00 PM traveling at 60 mph, and another train leaves Station B at 2:30 PM traveling at 80 mph toward Station A, and the stations are 200 miles apart, when will they meet?" 28 29 print("Problem:", problem) 30 print("\n" + "-" * 60) 31 32 # Minimal reasoning effort - fast but simple 33 print("MINIMAL REASONING EFFORT (fastest):") 34 print("-" * 40) 35 start_time = time.time() 36 response_minimal = client.responses.create( 37 model="gpt-5.2", 38 input=problem, 39 reasoning={"effort": "minimal"} 40 ) 41 minimal_time = time.time() - start_time 42 print(response_minimal.output_text) 43 print(f"\n⏱️ Time taken: {minimal_time:.2f} seconds") 44 45 print("\n" + "-" * 60) 46 47 # High reasoning effort - thorough analysis 48 print("HIGH REASONING EFFORT (most thorough):") 49 print("-" * 40) 50 start_time = time.time() 51 response_high = client.responses.create( 52 model="gpt-5.2", 53 input=problem, 54 reasoning={"effort": "high"} 55 ) 56 high_time = time.time() - start_time 57 print(response_high.output_text) 58 print(f"\n⏱️ Time taken: {high_time:.2f} seconds") 59 print(f"📊 Speed difference: {high_time/minimal_time:.1f}x slower than minimal") 60 61 print("\n" + "=" * 60) 62 63if __name__ == "__main__": 64 """ 65 Run the reasoning effort demonstration. 66 67 This shows how different reasoning levels affect both the quality 68 of responses and the time taken to generate them. 69 """ 70 71 print("GPT-5 Reasoning Effort Demo") 72 print("This demo shows how reasoning_effort controls the depth of GPT-5's thinking.\n") 73 74 try: 75 demo_reasoning_effort() 76 77 except Exception as e: 78 print(f"Error: {e}") 79 print("Make sure your OPENAI_API_KEY environment variable is set correctly.")
1""" 2GPT-5 Verbosity Demo 3==================== 4 5This script demonstrates how the verbosity parameter controls 6the length and depth of GPT-5's responses, and measures the 7performance impact of different verbosity levels. 8""" 9 10import time 11 12def demo_verbosity(): 13 """ 14 Demonstrates the verbosity parameter. 15 16 verbosity controls the natural length and depth of responses: 17 - low: Concise, minimal responses 18 - medium: Default balanced responses 19 - high: Comprehensive, detailed responses 20 """ 21 print("=" * 60) 22 print("GPT-5 VERBOSITY DEMONSTRATION") 23 print("=" * 60) 24 25 # Topic that can be explained at different depths 26 topic = "Explain how machine learning algorithms learn from data" 27 28 print("Topic:", topic) 29 print("\n" + "-" * 60) 30 31 # Low verbosity - concise explanation 32 print("LOW VERBOSITY (concise):") 33 print("-" * 40) 34 start_time = time.time() 35 response_low = client.responses.create( 36 model="gpt-5.2", 37 input=topic, 38 text={"verbosity": "low"} 39 ) 40 low_time = time.time() - start_time 41 print(response_low.output_text) 42 print(f"\n⏱️ Time taken: {low_time:.2f} seconds") 43 44 print("\n" + "-" * 60) 45 46 # Medium verbosity - comprehensive explanation 47 print("MEDIUM VERBOSITY (comprehensive):") 48 print("-" * 40) 49 start_time = time.time() 50 response_medium = client.responses.create( 51 model="gpt-5.2", 52 input=topic, 53 text={"verbosity": "medium"} 54 ) 55 medium_time = time.time() - start_time 56 print(response_medium.output_text) 57 print(f"\n⏱️ Time taken: {medium_time:.2f} seconds") 58 print(f"📊 Length difference: {medium_time/low_time:.1f}x longer than low verbosity") 59 60 print("\n" + "=" * 60) 61 62if __name__ == "__main__": 63 """ 64 Run the verbosity demonstration. 65 66 This shows how different verbosity levels affect both the length 67 of responses and the time taken to generate them. 68 """ 69 70 print("GPT-5 Verbosity Demo") 71 print("This demo shows how verbosity controls the length and depth of GPT-5's responses.\n") 72 73 try: 74 demo_verbosity() 75 76 except Exception as e: 77 print(f"Error: {e}") 78 print("Make sure your OPENAI_API_KEY environment variable is set correctly.")