Skip to content

Error while using openai in Jupyter notebook for prompts repsonse. Please help #17

@mohitkothari0iit0isb

Description

@mohitkothari0iit0isb

I am trying to get openai in Jupyter notebook at local host. I used code mentioned in description but I am getting error as described below. Please tell me what can be done to run the code. Please help in solving this basic error (I am using paid version of chatgpt)

Code used in Jupyter notebook
`
!pip install openai certifi
!pip install certifi
!pip install openai
!pip install openai certifi requests pandas

import os
import openai
import certifi
import requests
import pandas as pd

Set your OpenAI API key as an environment variable

os.environ['OPENAI_API_KEY'] = Had put my key here

Retrieve the API key from environment variables

openai.api_key = os.getenv('OPENAI_API_KEY')

Create a custom requests session

session = requests.Session()
session.verify = certifi.where()

def query_openai(prompt):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai.api_key}',
}
json_data = {
'model': 'gpt-3.5-turbo',
'messages': [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': prompt}
],
}
response = session.post('https://api.openai.com/v1/chat/completions', headers=headers, json=json_data)
response.raise_for_status() # Raise an error for bad status codes
return response.json()

Test the function

prompt = "Explain the relationship between price and sales in a dataset."
response = query_openai(prompt)
print(response['choices'][0]['message']['content'].strip())
`

While running this code I am getting this error:
SSLError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)')))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions