Skip to content

Instantly share code, notes, and snippets.

@aandvalenzuela
Last active March 1, 2023 15:55
Show Gist options
  • Select an option

  • Save aandvalenzuela/3131376ddb35fb85b5d50186a08dfa4d to your computer and use it in GitHub Desktop.

Select an option

Save aandvalenzuela/3131376ddb35fb85b5d50186a08dfa4d to your computer and use it in GitHub Desktop.
#!/bin/env python
import openai
from openai_secret_manager import get_secret
# Load the API key from the file
secret = get_secret("openai")["api_key"]
openai.api_key = secret
# Define the prompt for the text generation
prompt = "Once upon a time"
# Set the parameters for the text generation
model_name = "text-davinci-002"
temperature = 0.5
max_tokens = 100
try:
# Generate text using the OpenAI API
response = openai.Completion.create(
engine=model_name,
prompt=prompt,
temperature=temperature,
max_tokens=max_tokens
)
# Print the generated text
print(response.choices[0].text.strip())
except openai.error.RateLimitError:
print("You have exceeded your API rate limit. Please try again later.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment