From f32533612f4953e1dbfffca50b4bda2db69d6b15 Mon Sep 17 00:00:00 2001 From: adyouri Date: Sun, 7 Dec 2025 08:54:42 +0100 Subject: [PATCH 1/3] add chatgpt-python-api materials --- chatgpt-python-api/basic_chatgpt_call.py | 10 +++++++ chatgpt-python-api/coding_assistant.py | 24 ++++++++++++++++ chatgpt-python-api/structured_output.py | 35 ++++++++++++++++++++++++ chatgpt-python-api/verify_setup.py | 5 ++++ 4 files changed, 74 insertions(+) create mode 100644 chatgpt-python-api/basic_chatgpt_call.py create mode 100644 chatgpt-python-api/coding_assistant.py create mode 100644 chatgpt-python-api/structured_output.py create mode 100644 chatgpt-python-api/verify_setup.py diff --git a/chatgpt-python-api/basic_chatgpt_call.py b/chatgpt-python-api/basic_chatgpt_call.py new file mode 100644 index 0000000000..e7b248f06f --- /dev/null +++ b/chatgpt-python-api/basic_chatgpt_call.py @@ -0,0 +1,10 @@ +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}") diff --git a/chatgpt-python-api/coding_assistant.py b/chatgpt-python-api/coding_assistant.py new file mode 100644 index 0000000000..b6fe126e1a --- /dev/null +++ b/chatgpt-python-api/coding_assistant.py @@ -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}") diff --git a/chatgpt-python-api/structured_output.py b/chatgpt-python-api/structured_output.py new file mode 100644 index 0000000000..423de73298 --- /dev/null +++ b/chatgpt-python-api/structured_output.py @@ -0,0 +1,35 @@ +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}") diff --git a/chatgpt-python-api/verify_setup.py b/chatgpt-python-api/verify_setup.py new file mode 100644 index 0000000000..e74ddefca7 --- /dev/null +++ b/chatgpt-python-api/verify_setup.py @@ -0,0 +1,5 @@ +from openai import OpenAI + +client = OpenAI() +print("OpenAI client created successfully!") +print(f"Using API key: {client.api_key[:8]}...") From d42bc43171f1de15716b696d302f8f26238e8f88 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 12 Dec 2025 13:11:09 +0100 Subject: [PATCH 2/3] Fix linter errors --- chatgpt-python-api/basic_chatgpt_call.py | 3 +-- chatgpt-python-api/structured_output.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/chatgpt-python-api/basic_chatgpt_call.py b/chatgpt-python-api/basic_chatgpt_call.py index e7b248f06f..d7c127cd1c 100644 --- a/chatgpt-python-api/basic_chatgpt_call.py +++ b/chatgpt-python-api/basic_chatgpt_call.py @@ -3,8 +3,7 @@ client = OpenAI() text_response = client.responses.create( - model="gpt-5", - input="Tell me a joke about Python programming" + model="gpt-5", input="Tell me a joke about Python programming" ) print(f"Joke:\n{text_response.output_text}") diff --git a/chatgpt-python-api/structured_output.py b/chatgpt-python-api/structured_output.py index 423de73298..c2d7c7e6c6 100644 --- a/chatgpt-python-api/structured_output.py +++ b/chatgpt-python-api/structured_output.py @@ -3,25 +3,28 @@ 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." - ) + "content": ( + "You are a coding assistant. Generate clean," + "well-documented Python code." + ), }, { "role": "user", - "content": "Write a simple Python function to add two numbers" - } + "content": "Write a simple Python function to add two numbers", + }, ], text_format=CodeOutput, ) From c287a2b0ec20280bba628f3d0cdcdf85cd125940 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 12 Dec 2025 13:12:57 +0100 Subject: [PATCH 3/3] Add README --- chatgpt-python-api/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 chatgpt-python-api/README.md diff --git a/chatgpt-python-api/README.md b/chatgpt-python-api/README.md new file mode 100644 index 0000000000..dbb4daa89c --- /dev/null +++ b/chatgpt-python-api/README.md @@ -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/).