-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add client for new entry * feat: support chat.do call model as param * feat: support chat.do call model as param * fix: client * fix: client * fix: ut function * feat: support console base url request custom * debug try test * fix: add thread debug info * fix: conftest * fix: rate limiter * 设置限流器销毁时不等待线程退出 * 设置 poetry lock --no-update * 删除 go_ci 中对 python 的路径判断 * 替换 golang rand.Seed * fix: llm type for _local_models --------- Co-authored-by: Dobiichi-Origami <[email protected]>
- Loading branch information
1 parent
3a8d7e1
commit 2c90822
Showing
52 changed files
with
1,175 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from qianfan.client.client import Qianfan | ||
|
||
__all__ = ["Qianfan"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Any, Optional | ||
|
||
from qianfan.config import Config, get_config | ||
from qianfan.consts import DefaultValue | ||
from qianfan.resources import ChatCompletion | ||
|
||
|
||
class Qianfan: | ||
config: Config | ||
|
||
def __init__( | ||
self, | ||
*, | ||
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, | ||
console_api_base_url: Optional[str] = None, | ||
request_timeout: Optional[int] = None, | ||
retry_count: int = DefaultValue.RetryCount, | ||
**kwargs: Any, | ||
) -> None: | ||
""" | ||
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, | ||
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, | ||
) | ||
|
||
def __setattr__(self, name: str, value: Any) -> None: | ||
if hasattr(self, name) or name in ["config", "chat", "completions"]: | ||
object.__setattr__(self, name, value) | ||
return | ||
if name == "api_key": | ||
self.config.BEARER_TOKEN = value | ||
return | ||
self.config.__setattr__(name, value) | ||
|
||
@property | ||
def chat(self) -> "Chat": | ||
return Chat(self) | ||
|
||
@property | ||
def completions(self) -> "ChatCompletion": | ||
return ChatCompletion(config=self.config, version=2) | ||
|
||
|
||
class Chat: | ||
_client: Qianfan | ||
|
||
def __init__(self, client: "Qianfan") -> None: | ||
self._client = client | ||
|
||
@property | ||
def completions(self) -> ChatCompletion: | ||
return ChatCompletion(config=self._client.config, version=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import time | ||
|
||
import typer | ||
|
||
from qianfan import resources | ||
from qianfan.common.cli.utils import ( | ||
credential_required, | ||
) | ||
from qianfan.resources.console.utils import call_action | ||
|
||
api_app = typer.Typer( | ||
no_args_is_help=True, | ||
help="Qianfan api", | ||
context_settings={"help_option_names": ["-h", "--help"]}, | ||
) | ||
|
||
|
||
@api_app.command(name="finetune.task.detail") | ||
@credential_required | ||
def task_info( | ||
task_id: str = typer.Option(..., help="task id"), | ||
) -> None: | ||
""" | ||
get a finetune task info from local cache | ||
""" | ||
resp = resources.FineTune.V2.task_detail(task_id=task_id) | ||
json_str = json.dumps(resp.body, ensure_ascii=False, indent=2) | ||
print(json_str) | ||
|
||
# wait a second for the log to be flushed | ||
time.sleep(0.1) | ||
|
||
|
||
@api_app.command(name="raw.console") | ||
@credential_required | ||
def call_console( | ||
route: str = typer.Option(..., help="route, e.g. /v2/finetuning"), | ||
action: str = typer.Option(..., help="action, e.g. DescribeFineTuningTask"), | ||
data: str = typer.Option(..., help="req body"), | ||
) -> None: | ||
""" | ||
create a console action api call | ||
""" | ||
d = json.loads(data) | ||
assert isinstance(d, dict) | ||
resp = call_action(base_url_route=route, action=action, params=d) | ||
json_str = json.dumps(resp.body, ensure_ascii=False, indent=2) | ||
print(json_str) | ||
|
||
# wait a second for the log to be flushed | ||
time.sleep(0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.