Skip to content

Commit

Permalink
feat: support bearer token (#745) & 新增客户对话意图识别cookbook (#738)
Browse files Browse the repository at this point in the history
* Create main.ipynb

* feat: support bearer token (#745)

* feat: add support for qianfan console api calls (#732)

* doc: 增加 go 自定义错误码文档 (#733)

* support custom retry codes

* add custom retry code example

* release go v0.0.11 (#734)

* fix: api v2 & bearer token

* fix: api v2

* fix: lint and update version

* Update evaluation_manager.py (#736)

* Update evaluation_manager.py

* Update consts.py

* Afs datasource (#737)

* 添加 AFSDatasource

* Debug

* feat: 添加数据集 V2 API (#739)

* 添加数据集 V2 API

* 追加注释

* fix text2img model list (#741)

* Fix unclosed aiohttp session whlie exception raised (#743)

* Fix unclosed aiohttp session whlie exception raised

* format and lint

* Update http_client.py

---------

Co-authored-by: NuODaniel <[email protected]>

* fix: merge

---------

Co-authored-by: Azure99 <[email protected]>
Co-authored-by: Liu Jun <[email protected]>
Co-authored-by: AlexT <[email protected]>
Co-authored-by: Dobiichi-Origami <[email protected]>
Co-authored-by: Guocheng <[email protected]>

---------

Co-authored-by: NuODaniel <[email protected]>
Co-authored-by: Azure99 <[email protected]>
Co-authored-by: Liu Jun <[email protected]>
Co-authored-by: Dobiichi-Origami <[email protected]>
Co-authored-by: Guocheng <[email protected]>
  • Loading branch information
6 people authored Aug 17, 2024
1 parent 91c71fa commit 46feebd
Show file tree
Hide file tree
Showing 18 changed files with 1,240 additions and 122 deletions.
678 changes: 678 additions & 0 deletions cookbook/awesome_demo/dialogue multi-tag generation/main.ipynb

Large diffs are not rendered by default.

34 changes: 31 additions & 3 deletions docs/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,42 @@ async for r in resp:
- `json_body`:请求体
- `retry_config`:请求使用的重试信息

##### V2 版本
#### V2 版本

千帆平台推出了 V2 版本的推理 API,SDK 也支持对 V2 版本的 API 进行调用,只需要创建对象时传入 `version="2"` 即可,其余使用方法与上述一致,差异点主要在于字段名称,具体字段名请参考 API 文档
千帆平台推出了 V2 版本的推理 API,SDK 也支持对 V2 版本的 API 进行调用:

##### V2 鉴权

API v2 采用Bearer Token的鉴权方式:可以通过access_key 和 secret_key 获取。因此可以选择以下两种方式设置鉴权信息:
```python
import os
# 安全认证
os.environ['QIANFAN_ACCESS_KEY'] = 'your_access_key'
os.environ['QIANFAN_SECRET_KEY'] = 'your_secret_key'
# 或 bearer token
os.environ['QIANFAN_BEARER_TOKEN'] = 'your_bearer_token'
```

我们可以运行以下接口获取BEARER_TOKEN(可用于需要临时鉴权,或进行应用分发的场景):

```python
import os
os.environ['QIANFAN_ACCESS_KEY'] = 'your_access_key'
os.environ['QIANFAN_SECRET_KEY'] = 'your_secret_key'

resp = IAM.create_bearer_token(100)
print(resp.body)
token = resp.body["token"]
```

##### V2 示例:

只需要创建对象时传入 `version="2"` 即可,其余使用方法与上述一致,差异点主要在于字段名称,具体字段名请参考 API 文档

```python
# 在创建时传入 version 以使用 V2 版本
# model 字段为可选,默认为 ernie-speed-8k,也可以指定其他模型,后续调用均会使用该模型
chat = qianfan.ChatCompletion(version="2", model="ernie-speed-8k")
chat = qianfan.ChatCompletion(version="2", app_id='app-xxx', model="ernie-speed-8k")

# 调用方式与 V1 版本一致,具体字段名参考 API 文档
resp = chat.do(
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "qianfan"
version = "0.4.5"
version = "0.4.6"
description = "文心千帆大模型平台 Python SDK"
authors = []
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion python/qianfan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"QfRole",
"QfMessages",
"QfResponse",
"AccessToken",
"Token",
"AccessKey",
"SecretKey",
"get_config",
Expand Down
6 changes: 6 additions & 0 deletions python/qianfan/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ class Config:
ACCESS_KEY: Optional[str] = Field(default=None)
SECRET_KEY: Optional[str] = Field(default=None)
ACCESS_TOKEN: Optional[str] = Field(default=None)
BEARER_TOKEN: Optional[str] = Field(default=None)
BASE_URL: str = Field(default=DefaultValue.BaseURL)
NO_AUTH: bool = Field(default=False)
USE_CUSTOM_ENDPOINT: bool = Field(default=False)
MODEL_API_PREFIX: str = Field(default=DefaultValue.ModelAPIPrefix)
AUTH_TIMEOUT: float = Field(default=DefaultValue.AuthTimeout)
DISABLE_EB_SDK: bool = Field(default=DefaultValue.DisableErnieBotSDK)
EB_SDK_INSTALLED: bool = Field(default=False)
IAM_SIGN_EXPIRATION_SEC: int = Field(default=DefaultValue.IAMSignExpirationSeconds)
CONSOLE_API_BASE_URL: str = Field(default=DefaultValue.ConsoleAPIBaseURL)
IAM_BASE_URL: str = Field(default=DefaultValue.IAMBaseURL)
ACCESS_TOKEN_REFRESH_MIN_INTERVAL: float = Field(
default=DefaultValue.AccessTokenRefreshMinInterval
)
BEARER_TOKEN_EXPIRED_INTERVAL: int = Field(
default=DefaultValue.BearerTokenExpiredInterval
)
INFER_RESOURCE_REFRESH_INTERVAL: float = Field(
default=DefaultValue.InferResourceRefreshMinInterval
)
Expand Down
6 changes: 5 additions & 1 deletion python/qianfan/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Env:
AuthTimeout: str = "QIANFAN_AUTH_TIMEOUT"
IAMSignExpirationSeconds: str = "QIANFAN_IAM_SIGN_EXPIRATION_SEC"
ConsoleAPIBaseURL: str = "QIANFAN_CONSOLE_API_BASE_URL"
IAMBaseURL: str = "QIANFAN_IAM_BASE_URL"
AccessTokenRefreshMinInterval: str = "QIANFAN_ACCESS_TOKEN_REFRESH_MIN_INTERVAL"
InferResourceRefreshMinInterval: str = "QIANFAN_INFER_RESOURCE_REFRESH_MIN_INTERVAL"
EnablePrivate: str = "QIANFAN_ENABLE_PRIVATE"
Expand Down Expand Up @@ -252,7 +253,8 @@ class Consts:
PrivateResourceGetResourceParam: str = "DescribeServiceResource"
PrivateResourceReleaseServiceResourceParam: str = "ReleaseServiceResource"

ChatV2API: str = "/v2/chat"
ChatV2API: str = "/v2/chat/completions"
IAMBearerTokenAPI: str = "/v1/BCE-BEARER/token"

STREAM_RESPONSE_PREFIX: str = "data: "
STREAM_RESPONSE_EVENT_PREFIX: str = "event: "
Expand Down Expand Up @@ -283,7 +285,9 @@ class DefaultValue:
DisableErnieBotSDK: bool = True
IAMSignExpirationSeconds: int = 300
ConsoleAPIBaseURL: str = "https://qianfan.baidubce.com"
IAMBaseURL: str = "https://iam.bj.baidubce.com"
AccessTokenRefreshMinInterval: float = 3600
BearerTokenExpiredInterval: int = 43200
InferResourceRefreshMinInterval: float = 600
RetryCount: int = 3
RetryTimeout: float = 300
Expand Down
6 changes: 6 additions & 0 deletions python/qianfan/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class AccessTokenExpiredError(QianfanError):
pass


class BearerTokenExpiredError(QianfanError):
"""Exception when bearer token is expired"""

pass


class InternalError(QianfanError):
"""Exception when internal error occurs"""

Expand Down
2 changes: 2 additions & 0 deletions python/qianfan/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from qianfan.resources.console.charge import Charge
from qianfan.resources.console.data import Data
from qianfan.resources.console.finetune import FineTune
from qianfan.resources.console.iam import IAM
from qianfan.resources.console.memory import Memory
from qianfan.resources.console.model import Model
from qianfan.resources.console.prompt import Prompt
Expand Down Expand Up @@ -49,4 +50,5 @@
"QfMessages",
"QfResponse",
"Memory",
"IAM",
]
Loading

0 comments on commit 46feebd

Please sign in to comment.