File tree Expand file tree Collapse file tree 4 files changed +74
-0
lines changed
Expand file tree Collapse file tree 4 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+ from openai import OpenAI
2+
3+ client = OpenAI ()
4+
5+ text_response = client .responses .create (
6+ model = "gpt-5" ,
7+ input = "Tell me a joke about Python programming"
8+ )
9+
10+ print (f"Joke:\n { text_response .output_text } " )
Original file line number Diff line number Diff line change 1+ from openai import OpenAI
2+
3+ user_input = input ("How can I help you? " )
4+
5+ client = OpenAI ()
6+
7+ code_response = client .responses .create (
8+ model = "gpt-5" ,
9+ input = [
10+ {
11+ "role" : "developer" ,
12+ "content" : (
13+ "You are a Python coding assistant. "
14+ "Only accept Python related questions."
15+ ),
16+ },
17+ {
18+ "role" : "user" ,
19+ "content" : f"{ user_input } " ,
20+ },
21+ ],
22+ )
23+
24+ print (f"\n { code_response .output_text } " )
Original file line number Diff line number Diff line change 1+ from openai import OpenAI
2+ from pydantic import BaseModel
3+
4+ client = OpenAI ()
5+
6+ class CodeOutput (BaseModel ):
7+ function_name : str
8+ code : str
9+ explanation : str
10+ example_usage : str
11+
12+ code_response = client .responses .parse (
13+ model = "gpt-5" ,
14+ input = [
15+ {
16+ "role" : "developer" ,
17+ "content" : ("You are a coding assistant. Generate clean,"
18+ "well-documented Python code."
19+ )
20+ },
21+ {
22+ "role" : "user" ,
23+ "content" : "Write a simple Python function to add two numbers"
24+ }
25+ ],
26+ text_format = CodeOutput ,
27+ )
28+
29+ code_result = code_response .output_parsed
30+
31+ print (f"Function Name: { code_result .function_name } " )
32+ print ("\n Code:" )
33+ print (code_result .code )
34+ print (f"\n Explanation: { code_result .explanation } " )
35+ print (f"\n Example Usage:\n { code_result .example_usage } " )
Original file line number Diff line number Diff line change 1+ from openai import OpenAI
2+
3+ client = OpenAI ()
4+ print ("OpenAI client created successfully!" )
5+ print (f"Using API key: { client .api_key [:8 ]} ..." )
You can’t perform that action at this time.
0 commit comments