Skip to content

Commit

Permalink
feat: support console base url request custom
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhjz committed Aug 30, 2024
1 parent e83ae31 commit 588eecb
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
30 changes: 24 additions & 6 deletions python/qianfan/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,41 @@ def __init__(
*,
access_key: Optional[str] = None,
secret_key: Optional[str] = None,
api_key: Optional[str] = None,
bearer_token: Optional[str] = None,
app_id: Optional[str] = None,
base_url: Optional[str] = None,
console_base_url: Optional[str] = None,
console_api_base_url: Optional[str] = None,
request_timeout: Optional[int] = None,
retry_count: int = DefaultValue.RetryCount,
**kwargs: Any,
) -> None:
if kwargs.get("api_key"):
bearer_token = kwargs.get("api_key")
"""
Construct a new qianfan client
This automatically infers the following arguments from their corresponding
environment variables if they are not provided:
- `api_key` from `QIANFAN_BEARER_TOKEN`
- `access_key` from `QIANFAN_ACCESS_KEY`
- `secret_key` from `QIANFAN_SECRET_KEY`
- `app_id` from `QIANFAN_APP_ID`
Args:
access_key (Optional[str], optional): iam access key.
secret_key (Optional[str], optional): iam secret key.
api_key (Optional[str], optional): api_key.
bearer_token (Optional[str], optional): same with api_key.
app_id (Optional[str], optional): qianfan app v2 id.
console_api_base_url (Optional[str], optional): api base url.
"""
if api_key:
bearer_token = api_key
self.config = Config(
ACCESS_KEY=access_key or get_config().ACCESS_KEY,
SECRET_KEY=secret_key or get_config().SECRET_KEY,
BEARER_TOKEN=bearer_token or get_config().BEARER_TOKEN,
APP_ID=app_id or get_config().APP_ID,
BASE_URL=base_url or get_config().BASE_URL,
CONSOLE_API_BASE_URL=console_base_url or get_config().CONSOLE_API_BASE_URL,
CONSOLE_API_BASE_URL=console_api_base_url
or get_config().CONSOLE_API_BASE_URL,
LLM_API_RETRY_COUNT=retry_count or get_config().LLM_API_RETRY_COUNT,
LLM_API_RETRY_TIMEOUT=request_timeout or get_config().LLM_API_RETRY_TIMEOUT,
**kwargs,
Expand Down
1 change: 1 addition & 0 deletions python/qianfan/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Config:
# 缓存文件路径配置
DISABLE_CACHE: bool = Field(default=DefaultValue.DisableCache)
CACHE_DIR: str = Field(default=DefaultValue.CacheDir)
CHAT_V2_API_ROUTE: str = Field(default=DefaultValue.ChatV2ApiRoute)

def auth_key(self) -> str:
auth_keys = (
Expand Down
1 change: 1 addition & 0 deletions python/qianfan/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ class DefaultValue:
FileEncoding: str = "utf-8"
CacheDir: str = str(Path.home() / ".qianfan_cache")
DisableCache: bool = False
ChatV2ApiRoute: str = Consts.ChatV2API


class DefaultLLMModel:
Expand Down
15 changes: 11 additions & 4 deletions python/qianfan/resources/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,17 @@ def _generate_body(
del kwargs[key]

kwargs["stream"] = stream
if "extra_parameters" not in kwargs:
kwargs["extra_parameters"] = {}
if kwargs["extra_parameters"].get("request_source") is None:
kwargs["extra_parameters"]["request_source"] = f"qianfan_py_sdk_v{VERSION}"
if kwargs.get("_no_extra_parameters", False):
if "extra_parameters" in kwargs:
del kwargs["extra_parameters"]
del kwargs["_no_extra_parameters"]
else:
if "extra_parameters" not in kwargs:
kwargs["extra_parameters"] = {}
if kwargs["extra_parameters"].get("request_source") is None:
kwargs["extra_parameters"][
"request_source"
] = f"qianfan_py_sdk_v{VERSION}"
return kwargs

def _data_postprocess(self, data: QfResponse) -> QfResponse:
Expand Down
8 changes: 4 additions & 4 deletions python/qianfan/resources/llm/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)

import qianfan.errors as errors
from qianfan.consts import Consts, DefaultLLMModel, DefaultValue
from qianfan.consts import DefaultLLMModel, DefaultValue
from qianfan.resources.llm.base import (
UNSPECIFIED_MODEL,
BaseResourceV1,
Expand Down Expand Up @@ -1534,7 +1534,7 @@ def api_type(cls) -> str:
return "chat"

def _api_path(self) -> str:
return Consts.ChatV2API
return self.config.CHAT_V2_API_ROUTE

def do(
self,
Expand Down Expand Up @@ -1642,7 +1642,7 @@ def do(
**kwargs: Any,
) -> Union[QfResponse, Iterator[QfResponse]]:
system, messages = self._adapt_messages_format(messages)
if "system" not in kwargs:
if "system" not in kwargs and system:
kwargs["system"] = system
if model is not None or endpoint is not None:
# TODO兼容 v2调用ernie-func-8k
Expand Down Expand Up @@ -1697,7 +1697,7 @@ async def ado(
**kwargs: Any,
) -> Union[QfResponse, AsyncIterator[QfResponse]]:
system, messages = self._adapt_messages_format(messages)
if "system" not in kwargs:
if "system" not in kwargs and system:
kwargs["system"] = system
if model is not None or endpoint is not None:
# TODO兼容 v2调用ernie-func-8k
Expand Down
16 changes: 8 additions & 8 deletions python/qianfan/resources/requestor/openapi_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _check_error(self, body: Dict[str, Any]) -> None:
possible_reason = (
"IAM 鉴权失败,请检查 Access Key 与 Secret Key 是否正确,"
"当前使用的 Access Key 为"
f" `{_masked_ak(get_config().ACCESS_KEY or '')}`"
f" `{_masked_ak(self.config.ACCESS_KEY or '')}`"
)
elif error_code == APIErrorCode.DailyLimitReached.value:
possible_reason = "未开通所调用服务的付费权限,或者账户已欠费"
Expand Down Expand Up @@ -629,7 +629,7 @@ def _add_access_token(
"""
add access token to QfRequest
"""
if get_config().NO_AUTH:
if self.config.NO_AUTH:
# 配置无鉴权,不签名,不抛出需要刷新token的异常,直接跳出。
return req
if auth is None:
Expand All @@ -653,7 +653,7 @@ async def _async_add_access_token(
"""
async add access token to QfRequest
"""
if get_config().NO_AUTH:
if self.config.NO_AUTH:
# 配置无鉴权,不签名,不抛出需要刷新token的异常,直接跳出。
return req
if auth is None:
Expand All @@ -676,8 +676,8 @@ def _llm_api_url(self, endpoint: str) -> str:
convert endpoint to llm api url
"""
return "{}{}{}".format(
get_config().BASE_URL,
get_config().MODEL_API_PREFIX,
self.config.BASE_URL,
self.config.MODEL_API_PREFIX,
endpoint,
)

Expand Down Expand Up @@ -717,7 +717,7 @@ def _refresh_bearer_token(self, *args: Any, **kwargs: Any) -> Dict:
from qianfan.resources.console.iam import IAM

resp = IAM.create_bearer_token(
expire_in_seconds=get_config().BEARER_TOKEN_EXPIRED_INTERVAL,
expire_in_seconds=self.config.BEARER_TOKEN_EXPIRED_INTERVAL,
ak=self._auth._access_key,
sk=self._auth._secret_key,
)
Expand All @@ -737,7 +737,7 @@ def _llm_api_url(self, endpoint: str) -> str:
convert endpoint to llm api url
"""
return "{}{}".format(
get_config().CONSOLE_API_BASE_URL,
self.config.CONSOLE_API_BASE_URL,
endpoint,
)

Expand All @@ -747,7 +747,7 @@ def _add_access_token(
"""
add bearer token to QfRequest V2
"""
if get_config().NO_AUTH:
if self.config.NO_AUTH:
# 配置无鉴权,不签名,不抛出需要刷新token的异常,直接跳出。
return req
if auth is None:
Expand Down
2 changes: 1 addition & 1 deletion python/qianfan/resources/typing_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Choice(BaseModel):
message: ChatCompletionMessage
"""A chat completion message generated by the model."""

need_clear_history: bool
need_clear_history: Optional[bool] = None

ban_round: Optional[int] = None

Expand Down

0 comments on commit 588eecb

Please sign in to comment.