Using OpenAI API.
Using OpenAI API.¶
While accessing ChatGPT through the OpenAI portal is engaging, you can also interact with it programmatically using their API. Let’s use an API key and handle everything directly in Python!Install and import modules¶
# try:
import openai
# except ImportError:
# # !pip install openai%load_ext autoreload
%autoreload 2import os
import urllib
from PIL import Image
import matplotlib.pyplot as plt
import io
from typing import List, DictGetting ready¶
- First go to the OpenAI website, signing up for an account, and obtaining an API key from the View API Keys section.
- Set your API key as an environment variable.
os.environ["OPENAI_API_KEY"] = "<your_api_key_here>"Question answering using ChatGPT Agent¶
# Create an agent.
agent = openai.OpenAI()# Example function for querying the assistant
def get_assistant_response(
model: str,
messages: List[Dict[str, str]],
temperature: float = 0.7,
max_tokens: int = 150,
) -> str:
"""
Queries the OpenAI API to get a response from the assistant.
param model: model to use, e.g., "gpt-4".
param messages: list of messages defining the conversation.
param temperature: sampling temperature, controls randomness.
param max_tokens: maximum number of tokens for the response.
Returns: assistant's response.
"""
response = agent.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
return response.choices[0].message.content# Example scenario: Customer inquiry about product return policy
messages = [
{
"role": "system",
"content": "You are a friendly and professional customer support assistant.",
},
{
"role": "user",
"content": "Hi, I need to return a product I purchased last week. Can you tell me the process?",
},
]
response = get_assistant_response(
model="gpt-4o-mini", # Use a robust model for nuanced conversations
messages=messages,
)
print("Assistant's Response:")
print(response)Assistant's Response:
Of course! To assist you better, could you please provide the name of the product and the retailer or website from which you purchased it? Different stores may have different return policies and processes.
# Escalation for complex issues
follow_up = [
{
"role": "system",
"content": "You are a customer support assistant. If the issue is complex, provide a response and escalate to a human agent.",
},
{
"role": "user",
"content": "The product I received is damaged, and I need it replaced immediately.",
},
]
response = get_assistant_response(
model="gpt-4o-mini", messages=follow_up, temperature=0.6
)
print("\nEscalation Scenario:")
print(response)
Escalation Scenario:
I’m sorry to hear that you received a damaged product. I understand how frustrating this can be. To assist you better, could you please provide me with your order number and some details about the damage? Once I have that information, I can help initiate the replacement process for you. If the issue requires further assistance, I will escalate it to a human agent.
Coding Assistant¶
def query_coding_agent(task: str, model_name: str = "gpt-4o-mini") -> str:
"""
Interacts with the coding assistant to complete a given task.
param task: detailed description of the coding task or question.
param model_name: name of the model to use.
Returns: assistant's response.
"""
try:
# Step 1: Create the coding assistant
coding_agent = agent.beta.assistants.create(
model=model_name,
name="Coding Assistant",
description="An AI assistant skilled in programming, debugging, and code documentation.",
instructions="You are a helpful coding assistant. You are an expert in Python, JavaScript, and debugging common errors. Assist the user by generating code snippets, fixing errors, and explaining concepts in simple terms.",
)
# Step 2: Create a thread for the task (conversation with the assistant)
thread = agent.beta.threads.create()
# Step 3: Send the coding task to the assistant
message = agent.beta.threads.messages.create(
thread_id=thread.id, role="user", content=task
)
# Step 4: Run the assistant to execute the task and get the response
run = agent.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=coding_agent.id,
instructions="Please respond with the solution to the user's coding problem.",
)
# Step 5: Check if the task was completed and retrieve the assistant's response
if run.status == "completed":
messages = agent.beta.threads.messages.list(thread_id=thread.id)
# Return the first message from the assistant's response
return messages.data[0].content[0].text.value
else:
return f"Task not completed. Status: {run.status}"
except Exception as e:
return f"An error occurred: {str(e)}"Example: Generating a Python function¶
task_description = "Write a Python function to calculate the factorial of a number using recursion."
response = query_coding_agent(task_description)
print("Response from Coding Agent:")
print(response)/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:20: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
thread = agent.beta.threads.create()
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:23: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
message = agent.beta.threads.messages.create(
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:30: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
run = agent.beta.threads.runs.create_and_poll(
Response from Coding Agent:
Certainly! Here’s a Python function that calculates the factorial of a number using recursion:
```python
def factorial(n):
# Check if the input is a non-negative integer
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
elif n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Example usage
try:
num = 5
print(f"The factorial of {num} is {factorial(num)}")
except ValueError as e:
print(e)
```
### Explanation:
- The function `factorial` takes an integer `n` as an argument.
- It first checks if `n` is negative. If it is, it raises an error since factorials are not defined for negative numbers.
- If `n` is `0` or `1`, it returns `1`, since the factorial of both `0` and `1` is `1`.
- For other positive integers, it calls itself recursively, multiplying `n` by the factorial of `n-1` until it reaches the base case.
You can test the function with different values of `num` to see how it works!
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:38: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
messages = agent.beta.threads.messages.list(
Example: Advanced Features¶
Expand the assistant’s capabilities:
Debugging Errors¶
Provide a code snippet with an error and ask for fixes.
debug_task = """
Here is my code:
def add_numbers(a, b):
print(a + b)
I'm getting an IndentationError. Can you fix it?
"""
debug_response = query_coding_agent(debug_task)
print("Debugging Response:")
print(debug_response)/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:20: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
thread = agent.beta.threads.create()
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:23: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
message = agent.beta.threads.messages.create(
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:30: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
run = agent.beta.threads.runs.create_and_poll(
Debugging Response:
The `IndentationError` occurs because the `print` statement is not properly indented within the `add_numbers` function. In Python, the code inside the function must be indented. Here's the corrected version of your code:
```python
def add_numbers(a, b):
print(a + b)
```
Make sure to use consistent indentation (either spaces or tabs) throughout your code to avoid any further indentation errors.
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:38: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
messages = agent.beta.threads.messages.list(
Code Refactoring¶
Ask the assistant to optimize inefficient code.
refactor_task = """
Here is my code:
numbers = [1, 2, 3, 4, 5]
sum = 0
for num in numbers:
sum += num
print(sum)
Can you refactor it to use a more Pythonic approach?
"""
refactor_response = query_coding_agent(refactor_task)
print("Refactored Code:")
print(refactor_response)/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:20: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
thread = agent.beta.threads.create()
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:23: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
message = agent.beta.threads.messages.create(
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:30: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
run = agent.beta.threads.runs.create_and_poll(
Refactored Code:
Certainly! You can use the built-in `sum()` function in Python, which provides a more concise and Pythonic way to calculate the sum of elements in a list. Here’s the refactored code:
```python
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)
```
In this version, we directly use `sum(numbers)` to calculate the sum of the list, which makes the code cleaner and easier to read. Also, it's a good practice to avoid using built-in names like `sum` for variable names, so I changed `sum` to `total`.
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:38: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
messages = agent.beta.threads.messages.list(
3. Documentation Drafting¶
Request documentation for a given function.
doc_task = """
Can you write docstrings for this Python function?
def greet_user(name):
print(f"Hello, {name}!")
"""
doc_response = query_coding_agent(doc_task)
print("Generated Docstring:")
print(doc_response)/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:20: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
thread = agent.beta.threads.create()
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:23: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
message = agent.beta.threads.messages.create(
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:30: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
run = agent.beta.threads.runs.create_and_poll(
/var/folders/h_/84_b04zd7_q8186rs2qwqkt40000gn/T/ipykernel_22644/2830054414.py:38: DeprecationWarning: The Assistants API is deprecated in favor of the Responses API
messages = agent.beta.threads.messages.list(
Generated Docstring:
Certainly! Here’s a docstring for the `greet_user` function:
```python
def greet_user(name):
"""
Prints a greeting message to the user.
Parameters:
name (str): The name of the user to greet.
Example:
>>> greet_user("Alice")
Hello, Alice!
"""
print(f"Hello, {name}!")
```
This docstring provides a brief description of what the function does, describes the parameter it takes, and includes an example of how to use the function.
Using DALL-E for Image Creation with OpenAI¶
OpenAI offers access to its DALL-E model through an API. DALL-E is a multimodal version of GPT-3, containing 12 billion parameters, designed to convert text descriptions into images. It has been trained on a vast collection of text-image pairs sourced from the web (including Wikipedia), allowing it to generate images based on written prompts. This model is accessible through the same API.
def generate_image(prompt: str):
com = agent.images.generate(prompt=prompt, n=1, size="512x512")
url = com.data[0].url
image_data = urllib.request.urlopen(url).read()
image_file = io.BytesIO(image_data)
image = Image.open(image_file)
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
plt.xticks([])
plt.yticks([])
# Display the image
plt.imshow(image)
plt.show()prompt = "A futuristic cyberpunk cityscape at night, with neon lights reflecting off wet streets, flying cars, and towering skyscrapers, in a vibrant color palette."
generate_image(prompt)