Skip to content
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

Add openai key path #545

Merged
merged 3 commits into from
Sep 12, 2023
Merged
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
14 changes: 10 additions & 4 deletions pandasai/llm/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class OpenAI(BaseOpenAI):
def __init__(
self,
api_token: Optional[str] = None,
api_key_path: Optional[str] = None,
**kwargs,
):
"""
Expand All @@ -59,11 +60,16 @@ def __init__(
**kwargs: Extended Parameters inferred from BaseOpenAI class

"""

self.api_token = api_token or os.getenv("OPENAI_API_KEY") or None
if self.api_token is None:
raise APIKeyNotFoundError("OpenAI API key is required")
openai.api_key = self.api_token
self.api_key_path = api_key_path

if (not self.api_token) and (not self.api_key_path):
raise APIKeyNotFoundError("Either OpenAI API key or key path is required")

if self.api_token:
openai.api_key = self.api_token
else:
openai.api_key_path = self.api_key_path

self.openai_proxy = kwargs.get("openai_proxy") or os.getenv("OPENAI_PROXY")
if self.openai_proxy:
gventuri marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
3 changes: 3 additions & 0 deletions tests/llms/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def test_type_without_token(self):
def test_type_with_token(self):
assert OpenAI(api_token="test").type == "openai"

def test_type_with_key_path(self):
assert OpenAI(api_key_path=".key").type == "openai"
gventuri marked this conversation as resolved.
Show resolved Hide resolved

def test_proxy(self):
proxy = "http://proxy.mycompany.com:8080"
client = OpenAI(api_token="test", openai_proxy=proxy)
Expand Down