Skip to content

Commit f325336

Browse files
committed
add chatgpt-python-api materials
1 parent 0e25d33 commit f325336

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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}")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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}")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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("\nCode:")
33+
print(code_result.code)
34+
print(f"\nExplanation: {code_result.explanation}")
35+
print(f"\nExample Usage:\n{code_result.example_usage}")

chatgpt-python-api/verify_setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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]}...")

0 commit comments

Comments
 (0)