Skip to content

Instantly share code, notes, and snippets.

@gkluoe
Last active October 3, 2023 08:24
Show Gist options
  • Save gkluoe/d944b975ff334fc76028d1473ed5a4cf to your computer and use it in GitHub Desktop.
Save gkluoe/d944b975ff334fc76028d1473ed5a4cf to your computer and use it in GitHub Desktop.
import os
import openai
import http
# This is our experimental API gateway
openai.api_base = 'https://apimgmtpoc001.azure-api.net/openai'
# This needs to be set or the module complains, but we can leave it empty.
openai.api_key = ''
# A key to access the API gateway
api_gateway_key = os.getenv('API_GATEWAY_KEY')
# We need to inject this header to veery request so that we can authenticate to our APi gateway
gateway_auth_header = {"GATEWAY_AUTH": api_gateway_key}
# Turn on some useful debig info so that we can see headers etc
http.client.HTTPConnection.debuglevel = 1
completion = openai.ChatCompletion.create(headers=gateway_auth_header, model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
print(completion.choices[0].message.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment