Hi
just start using api of openai
i cannot get answer from my request via a python script ,i tried everything but still the error persist
Welcome to the forum!
Can you show your source code please?
simple code just starting using openai
openai.api_key = "sk-XXXXXX"
import openai
import os
response = openai.Completion.create(
model="text-davinci-003",
prompt="how do you say hello in italian"
)
ok, give this a try, I think you were missing a print statement
openai.api_key = "sk-XXXXXX"
import openai
import os
response = openai.Completion.create(
model="text-davinci-003",
prompt="how do you say hello in italian"
)
print(response.choices[0].text.strip())
I want to also point out that storing API keys in your source code is a bad idea, anyone with coding skills could find your key and misuse it this way, one of the common ways to do it is to store your key in environment variable and then pull them back in your code, that way the code itself never knows what the key is until it gets it from the system it’s on.
that would make your code look like this
import os
import openai
openai.api_key = os.getenv('OPENAI_API_KEY')
response = openai.Completion.create(
model="text-davinci-003",
prompt="how do you say hello in italian"
)
print(response.choices[0].text.strip())
In a UNIX/Linux system, you can set environment variables in the terminal as follows:
In bash, Copy this code
export OPENAI_API_KEY="sk-XXXXXX"
If you are using a Windows system, you can set an environment variable in the command line like this:
Run cmd and Copy this code
setx OPENAI_API_KEY "sk-XXXXXX"
With linux you can put the export command into your .bashrc so it gets executed whenever you start the server, in windows the setx will remain memorised over restarts.