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

feat: add url_suffix parameter to predictor.openai #30

Merged
merged 1 commit into from
Jul 1, 2024
Merged
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
11 changes: 9 additions & 2 deletions pai/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,16 @@ def raw_predict(
)
return resp

def openai(self, **kwargs) -> "OpenAI":
def openai(self, url_suffix: str = "v1", **kwargs) -> "OpenAI":
"""Initialize an OpenAI client from the predictor.

Only used for OpenAI API compatible services, such as Large Language Model
service from PAI QuickStart.

Args:
url_suffix (str, optional): URL suffix that will be appended to the
EAS service endpoint to form the base URL for the OpenAI client.
(Default "v1").
**kwargs: Additional keyword arguments for the OpenAI client.

Returns:
Expand All @@ -749,7 +752,11 @@ def openai(self, **kwargs) -> "OpenAI":
"openai package is not installed, install it with `pip install openai`."
)

base_url = kwargs.pop("base_url", posixpath.join(self.endpoint + "v1/"))
if url_suffix.startswith("/"):
default_base_url = posixpath.join(self.endpoint, url_suffix[1:])
else:
default_base_url = posixpath.join(self.endpoint, url_suffix)
base_url = kwargs.pop("base_url", default_base_url)
api_key = kwargs.pop("api_key", self.access_token)

return OpenAI(base_url=base_url, api_key=api_key, **kwargs)
Expand Down