I want to start using the api of openai. Using google cloud functions and python scripts.
Got it set to python 3.11
I start out to install openai "pip install openai" or "pip3 install openai" and it has problems either way running this code. I've seen sometimes install directory causing problems so to rule out that as a possible situation I've tried uninstalling and reinstalling openai several ways pip vs pip3.
Before I move on to trying amazons version for running scripts id like to have it work in google cloud functions. I have plans on running it also on other systems like a home server as simple as a rasp pi for instance but I thought it would be nice to start here.
This code passes and deploys but things go into error after and before hand testing is odd as well.
I must be missing something. So to show the errors and code I am using:
To start off the entrypoint is set as "generate_response" which matches the function I would want to start in main.py.
requirements.txt has
Flask==2.3.3
requests==2.26.0
openai
Not sure if "openai" is needed..
Again python runtime environment is set to python 3.11
However openai installs in directory for python3.9
I have the same issues both runtimes python 3.9 and 3.11 so I don't think its a directory issue. But who knows. I am sure it could be anything.
main.py has
import os
import requests
from flask import jsonify
def generate_response(request):
# Define the OpenAI endpoint and your API key
OPENAI_URL = "https://api.openai.com/v1/engines/davinci/completions"
HEADERS = {
"Authorization": f"Bearer {os.environ.get('REPLACE_WITH_YOUR_OWN_API')}",
"Content-Type": "application/json"
}
# Define your question:
prompt_text = "How are you?"
# Construct the data to send to the API
data = {
"prompt": prompt_text,
"max_tokens": 100
}
# Make the API call
response = requests.post(OPENAI_URL, headers=HEADERS, json=data)
# Extract text from the API response
openai_response = response.json()['choices'][0]['text'].strip()
#Display on terminal shell for debug:
print(openai_response)
return jsonify({"response": openai_response })
errors are in url page it gives me. It deploys and builds without error but url shows:
"500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.""
For now I turned off authentication I have it set to "Allow unauthenticated invocations" since it's just initial testing.
Kind of seems like it just freezes and stops doing anything when testing. The Cloud Shell, In the GCF Testing terminal window: Says ready to test and when you hit run test the curser blinks just sits there and nothing else...
Additionally I get the same problem running this code all the same idea to talk to openai but still get all same errors after deployment. This version just takes the idea out of seeing all results in webpage and its more hard coded and static with the way to see results in terminal or log.
import os
import openai
import requests
#entry point ask_openai
# Initialize OpenAI API
openai.api_key = os.environ.get('REPLACE_WITH_YOUR_OWN_API')
def ask_openai(request):
# Hardcoded question
question = "What is the capital of France?"
# Call OpenAI API
response = openai.Completion.create(
model="davinci",
prompt=question,
max_tokens=150
)
# Extracting answer from the response
answer = response.choices[0].text.strip()
# Print the question and its answer
print("Question:", question)
print("Answer:", answer)
return "Check the logs for the answer."