Skip to content

Add configuration option for how to turn an API key into a request header #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

# Configuration variables

# Set this to the API key for whatever API provider you're using.
api_key = os.environ.get("OPENAI_API_KEY")
# Provide a function that takes the above key and returns a dictionary
# with the appropriate header(s) for whatever API provider you're using.
api_key_to_header_fn = lambda key: {"Authorization": f"Bearer {key}"}
organization = os.environ.get("OPENAI_ORGANIZATION")
client_id = None
api_base = os.environ.get("OPENAI_API_BASE", "https://api.openai.com")
Expand Down
2 changes: 1 addition & 1 deletion openai/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def request_headers(self, api_key, method, extra):
headers = {
"X-OpenAI-Client-User-Agent": json.dumps(ua),
"User-Agent": user_agent,
"Authorization": "Bearer %s" % (api_key,),
}
headers.update(openai.api_key_to_header_fn(api_key))

if self.organization:
headers["OpenAI-Organization"] = self.organization
Expand Down