PYTHON - OPENAI - API
Simon-Pierre Boucher
2024-09-14

Python Version Jupyter Notebook OpenAI OpenAI GPT-3 OpenAI GPT-4 OpenAI

In [1]:
import os
import requests
import json

def call_openai_api(
    model="gpt-4",
    messages=None,
    temperature=1,
    max_tokens=2048,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
):
    # Get the API key from environment variables
    api_key = os.getenv("OPENAI_API_KEY")
    
    if not api_key:
        raise ValueError("The API key was not found in the environment variables. Please set OPENAI_API_KEY.")
    
    url = "https://api.openai.com/v1/chat/completions"
    
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    
    body = {
        "model": model,
        "messages": messages,
        "temperature": temperature,
        "max_tokens": max_tokens,
        "top_p": top_p,
        "frequency_penalty": frequency_penalty,
        "presence_penalty": presence_penalty
    }
    
    response = requests.post(url, headers=headers, data=json.dumps(body))
    
    if response.status_code != 200:
        raise ValueError(f"The API request failed: {response.text}")
    
    result = response.json()
    return result
In [2]:
# Define the messages
messages = [
    {"role": "system", "content": "You are an assistant"},
    {"role": "user", "content": "give me a list of gift idea for my girlfriend"}
]
In [3]:
# Call the function
response = call_openai_api(
    model="gpt-4",
    messages=messages,
    temperature=1,
    max_tokens=2048,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
)

# Display the assistant's response
print(response['choices'][0]['message']['content'])
1. Jewelry: A delicate necklace, bracelet, earrings, or rings can be a charming gift. Remember to choose something that complements her style.
   
2. Customized Gifts: A heart-shaped pendant with her initial, a bracelet with a memorable date engraved, or a custom-made illustration of you two. 

3. Makeup or Skincare Products: If she loves beauty products, a high-end lipstick, or a highly-rated skincare product could be her perfect gift.

4. Perfume: A bottle of her favorite scent or a new one from a premium brand can make her day.

5. Personalized Photo Album: Gather photos of your special moments together and curate a photo album.

6. Spa Treatment: A relaxing day at a spa could be a great treat for her.

7. Designer Handbag: If she's into fashion, look for a designer handbag from her favorite brand.

8. Classes or Workshops: Whether she's into cooking, dancing, or painting, you can enroll her in a class to hone her skills.

9. Concert Tickets: Does she have a favorite band or artist? Check out their tour schedule!

10. Tech Gadget: If she loves tech, maybe a new smartwatch, headphones, or the latest iPhone would make her happy.

11. Books: If she’s a reader, a bestseller book from her favorite author, or a genre she loves is a sure win.

12. A Romantic Getaway: Plan a surprise weekend trip to her favorite place or somewhere she always wanted to go.

13. Subscription Service: This could be a subscription to her favorite magazine or a streaming service like Netflix.

14. Cooking Gadgets: If she enjoys cooking, some useful kitchen gadgets, or high-quality cooking tools would be appreciated.

15. A Bouquet of Her Favorite Flowers: You can never go wrong with this classic gift idea.

16. Handwritten Love Letter: This classic gift is timeless and priceless.

Remember, the best gift is something that addresses her interests, hobbies, or needs. Personal and thoughtful gifts are often more appreciated than general ones!
In [ ]: