My attempt at using the OpenAI API keeps returning errors.
I think the problem is that I am not retrieving the API key correctly.
Is there a way I can access the API without using the environ. method for storing the key
import os
import openai_secret_manager
import openai
# Add Key to environ
os.environ["api_key"] = "secret key....." # I got a real key from openai.com
# Retrieve key from environ.
openai.api_key = os.getenv("api_key")
# Test connectiom and list available API models
res = openai.Model.list()
print(res) # This works fine
# load the API key from the environment
secrets = openai_secret_manager.get_secret(openai.api_key) # L31 - this line triggers the error
# prompt for the API to generate text
prompt = "What is the meaning of life?"
# generate a response from the API
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=10,
n=1,
stop=None,
temperature=0.5,
)
# print the generated text
print(response.choices[0].text)
The error message I get is:
Traceback (most recent call last):
File "/Users/home/Dropbox/Python_general_work/work_in_progress/chatgpt/main.py", line 31, in <module>
secrets = openai_secret_manager.get_secret(openai.api_key)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai_secret_manager.py", line 6, in get_secret
return openai_secret_manager.get_secret(key_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai_secret_manager.py", line 6, in get_secret
return openai_secret_manager.get_secret(key_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai_secret_manager.py", line 6, in get_secret
return openai_secret_manager.get_secret(key_name)
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
Looks like there is a recursive function call:
Traceback (most recent call last):
File "/Users/home/Dropbox/Python_general_work/work_in_progress/chatgpt/main.py", line 31, in <module>
secrets = openai_secret_manager.get_secret(openai.api_key)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai_secret_manager.py", line 6, in get_secret
return openai_secret_manager.get_secret(key_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai_secret_manager.py", line 6, in get_secret
return openai_secret_manager.get_secret(key_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/openai_secret_manager.py", line 6, in get_secret
return openai_secret_manager.get_secret(key_name)
openai_secret_manager.get_secret
calls openai_secret_manager.get_secret
which calls openai_secret_manager.get_secret
and so on...
I don't know what this actually is, but following the official Usage examples would look like this. There also does not seem a need for you to programmatically set and retrieve the environment variable:
import openai
openai.api_key = "sk-..." # Your key
# prompt for the API to generate text
prompt = "What is the meaning of life?"
# generate a response from the API
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=10,
n=1,
stop=None,
temperature=0.5,
)
# print the generated text
print(response.choices[0].text)