Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mohitkothari0iit0isb opened this issue Jul 17, 2024 · 0 comments

Comments

@mohitkothari0iit0isb
Copy link

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)')))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant