Skip to content

Instantly share code, notes, and snippets.

@monsieurpablo
Created January 18, 2023 17:45
Show Gist options
  • Save monsieurpablo/39d43f9908bcbb23103b087716065e53 to your computer and use it in GitHub Desktop.
Save monsieurpablo/39d43f9908bcbb23103b087716065e53 to your computer and use it in GitHub Desktop.
Integrate open AI API using Iron Python (Can be used inside Grasshopper3D)
import urllib2
import json
# Set the endpoint URL and the prompt text
url = "https://api.openai.com/v1/completions"
# Set the API key and other parameters
data = json.dumps({ "model": "text-davinci-003", "prompt": prompt, "temperature": 0.5, "max_tokens": 1024})
headers = { "Content-Type": "application/json", "Authorization": "Bearer {0}".format(api_key) }
answer = "Send to prompt to Open AI"
if send:
# Make the request
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
# Parse the response
response_text = response.read()
response_json = json.loads(response_text)
# Read the answer
answer = response_json["choices"][0]["text"]
print(answer)
@seghier
Copy link

seghier commented May 8, 2023

now don't work, i don't know why
always:
urllib2.HTTPError: HTTP Error 429: Too Many Requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment