Skip to content

Commit

Permalink
feat: optimize client traceback and input ak/sk (#182)
Browse files Browse the repository at this point in the history
* optimize client traceback

* fix lint

* not showing message when accesskey is set
  • Loading branch information
ZingLix authored Jan 10, 2024
1 parent 91c7c6d commit 2be6cfd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ $ qianfan [OPTIONS] COMMAND [ARGS]...
* `--secret-key TEXT`:百度智能云安全认证 Secret Key,获取方式参考 [文档](https://cloud.baidu.com/doc/Reference/s/9jwvz2egb)
* `--ak TEXT` [过时]:千帆平台应用的 API Key,仅能用于模型推理部分 API,获取方式参考 [文档](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Slkkydake)
* `--sk TEXT` [过时]:千帆平台应用的 Secret Key,仅能用于模型推理部分 API,获取方式参考 [文档](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Slkkydake)
* `--enable-traceback`:打印完整的错误堆栈信息,仅在发生异常时有效。
* `--version -v`:打印版本信息。
* `--install-completion`:为当前 shell 安装自动补全脚本。
* `--show-completion`:展示自动补全脚本。
Expand Down
56 changes: 54 additions & 2 deletions src/qianfan/common/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
from typing import Optional

import typer
from rich.prompt import Prompt

import qianfan
from qianfan.common.client.chat import chat_entry
from qianfan.common.client.completion import completion_entry
from qianfan.common.client.dataset import dataset_app
from qianfan.common.client.embedding import embedding_entry
from qianfan.common.client.txt2img import txt2img_entry
from qianfan.common.client.utils import print_error_msg, print_info_msg

app = typer.Typer(
no_args_is_help=True,
Expand All @@ -37,6 +39,8 @@
app.command(name="embedding", no_args_is_help=True)(embedding_entry)
app.add_typer(dataset_app, name="dataset")

_enable_traceback = False


def version_callback(value: bool) -> None:
"""
Expand Down Expand Up @@ -65,7 +69,13 @@ def main() -> None:
"""
Main function of qianfan client.
"""
app()
try:
app()
except Exception as e:
if _enable_traceback:
raise
else:
print_error_msg(str(e))


@app.callback()
Expand Down Expand Up @@ -122,11 +132,53 @@ def entry(
is_eager=True,
help="Print version.",
),
enable_traceback: bool = typer.Option(
False, "--enable-traceback", help="Print traceback when exception is thrown."
),
) -> None:
"""
Qianfan CLI which provides access to various Qianfan services.
"""
pass
global _enable_traceback
_enable_traceback = enable_traceback

ak = qianfan.get_config().AK
sk = qianfan.get_config().SK
access_key = qianfan.get_config().ACCESS_KEY
secret_key = qianfan.get_config().SECRET_KEY

if ak is None or sk is None:
if access_key is None or secret_key is None:
print_info_msg(
'No enough credential found. Please provide your "access key" and'
' "secret key".'
)
print_info_msg(
"You can find your key at"
" https://console.bce.baidu.com/iam/#/iam/accesslist"
)
print_info_msg(
"You can also set the credential using environment variable"
' "QIANFAN_ACCESS_KEY" and "QIANFAN_SECRET_KEY".'
)
print()
if access_key is None:
while True:
access_key = Prompt.ask("Please input your [b i]Access Key[/b i]")
if len(access_key) != 0:
qianfan.get_config().ACCESS_KEY = access_key
break
else:
print_error_msg("Access key cannot be empty.")
if secret_key is None:
while True:
secret_key = Prompt.ask("Please input your [b i]Secret Key[/b i]")
if len(secret_key) != 0:
qianfan.get_config().SECRET_KEY = secret_key
break
else:
print_error_msg("Secret key cannot be empty.")
print()


if __name__ == "__main__":
Expand Down

0 comments on commit 2be6cfd

Please sign in to comment.