Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions chatgpt-python-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# How to Integrate ChatGPT's API With Python Projects

This folder contains supporting materials for the Real Python tutorial [How to Integrate ChatGPT's API With Python Projects](https://realpython.com/chatgpt-python-api/).
9 changes: 9 additions & 0 deletions chatgpt-python-api/basic_chatgpt_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from openai import OpenAI

client = OpenAI()

text_response = client.responses.create(
model="gpt-5", input="Tell me a joke about Python programming"
)

print(f"Joke:\n{text_response.output_text}")
24 changes: 24 additions & 0 deletions chatgpt-python-api/coding_assistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from openai import OpenAI

user_input = input("How can I help you? ")

client = OpenAI()

code_response = client.responses.create(
model="gpt-5",
input=[
{
"role": "developer",
"content": (
"You are a Python coding assistant. "
"Only accept Python related questions."
),
},
{
"role": "user",
"content": f"{user_input}",
},
],
)

print(f"\n{code_response.output_text}")
38 changes: 38 additions & 0 deletions chatgpt-python-api/structured_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from openai import OpenAI
from pydantic import BaseModel

client = OpenAI()


class CodeOutput(BaseModel):
function_name: str
code: str
explanation: str
example_usage: str


code_response = client.responses.parse(
model="gpt-5",
input=[
{
"role": "developer",
"content": (
"You are a coding assistant. Generate clean,"
"well-documented Python code."
),
},
{
"role": "user",
"content": "Write a simple Python function to add two numbers",
},
],
text_format=CodeOutput,
)

code_result = code_response.output_parsed

print(f"Function Name: {code_result.function_name}")
print("\nCode:")
print(code_result.code)
print(f"\nExplanation: {code_result.explanation}")
print(f"\nExample Usage:\n{code_result.example_usage}")
5 changes: 5 additions & 0 deletions chatgpt-python-api/verify_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from openai import OpenAI

client = OpenAI()
print("OpenAI client created successfully!")
print(f"Using API key: {client.api_key[:8]}...")