Skip to content

Commit

Permalink
Merge branch 'baidubce:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
HorseDream authored Jan 3, 2024
2 parents 3666fe5 + af2adc5 commit a009a0b
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 85 deletions.
9 changes: 4 additions & 5 deletions appbuilder/core/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(self,
if not self.secret_key:
raise ValueError("secret_key is empty, please pass a nonempty secret_key "
"or set a secret_key in environment variable")
if not self.secret_key.startswith("Bearer"):
self.secret_key = "Bearer {}".format(self.secret_key)

if not gateway and not os.getenv("GATEWAY_URL"):
self.gateway = GATEWAY_URL
Expand Down Expand Up @@ -101,13 +103,10 @@ def check_response_json(data: dict):

def auth_header(self):
r"""auth_header is a helper method return auth info"""

if self.secret_key.startswith("Bearer "):
return {"X-Appbuilder-Authorization": self.secret_key}
else:
return {"X-Appbuilder-Authorization": "Bearer {}".format(self.secret_key)}
return {"X-Appbuilder-Authorization": self.secret_key}

@staticmethod
def response_request_id(response: requests.Response):
r"""response_request_id is a helper method get unique request id"""
return response.headers.get("X-Appbuilder-Request-Id", "")

10 changes: 7 additions & 3 deletions appbuilder/core/components/asr/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ def _recognize(self, request: ShortSpeechRecognitionRequest, timeout: float = No
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__._check_service_error(data)
request_id = self.http_client.response_request_id(response)
self.__class__._check_service_error(request_id,data)
response = ShortSpeechRecognitionResponse.from_json(payload=json.dumps(data))
response.request_id = request_id
return response

@staticmethod
def _check_service_error(data: dict):
def _check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
Expand All @@ -111,4 +111,8 @@ def _check_service_error(data: dict):
"""
if "err_no" in data and "err_msg" in data:
if data["err_no"] != 0:
raise AppBuilderServerException(service_err_code=data["err_no"], service_err_message=data["err_msg"])
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data["err_no"],
service_err_message=data["err_msg"]
)
6 changes: 3 additions & 3 deletions appbuilder/core/components/dish_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def _recognize(self, request: DishRecognitionRequest, timeout: float = None,

url = self.http_client.service_url("/v1/bce/aip/image-classify/v2/dish")
response = self.http_client.session.post(url, headers=headers, data=request_data, timeout=timeout)

self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
request_id = self.http_client.response_request_id(response)
if "error_code" in data and "error_msg" in data:
raise AppBuilderServerException(service_err_code=data["error_code"], service_err_message=data["error_msg"])
return DishRecognitionResponse(data, request_id=self.http_client.response_request_id(response))
raise AppBuilderServerException(request_id=request_id, service_err_code=data["error_code"], service_err_message=data["error_msg"])
return DishRecognitionResponse(data)
5 changes: 1 addition & 4 deletions appbuilder/core/components/doc_splitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

基本用法
---
参考tests目录下的[test_doc_splitter.py](../../../tests/test_doc_splitter.py),可快速搭建自己的文档切分用例。
参考tests目录下的[test_doc_splitter.py](https://github.com/baidubce/app-builder/blob/master/appbuilder/tests/test_doc_splitter.py),可快速搭建自己的文档切分用例。

以下是DocSplitter快速开始的一个示例。

Expand Down Expand Up @@ -94,7 +94,4 @@ print(res_paras.content)

暂无

## 更新记录和贡献

- 初始版本发布。
- 如您希望为会话小结组件贡献代码或反馈,请参考 [贡献指南](#)
11 changes: 7 additions & 4 deletions appbuilder/core/components/general_ocr/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ def _recognize(self, request: GeneralOCRRequest, timeout: float = None,
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__._check_service_error(data)
request_id = self.http_client.response_request_id(response)
self.__class__._check_service_error(request_id, data)
ocr_response = GeneralOCRResponse.from_json(payload=json.dumps(data))
ocr_response.request_id = request_id
return ocr_response

@staticmethod
def _check_service_error(data: dict):
def _check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
request (dict) : 通用文字识别body返回
返回:
"""
if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
14 changes: 9 additions & 5 deletions appbuilder/core/components/landmark_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ def __recognize(self, request: LandmarkRecognitionRequest, timeout: float = None
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__.__check_service_error(data)
return LandmarkRecognitionResponse(data, request_id=self.http_client.response_request_id(response))
request_id = self.http_client.response_request_id(response)
self.__class__.__check_service_error(request_id, data)
return LandmarkRecognitionResponse(data, request_id=request_id)

@staticmethod
def __check_service_error(data: dict):
def __check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
Expand All @@ -99,5 +100,8 @@ def __check_service_error(data: dict):
"""

if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
14 changes: 7 additions & 7 deletions appbuilder/core/components/llms/mrc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import os
os.environ["APPBUILDER_TOKEN"] = '...'

# 创建MRC对象
mrc_component = appbuilder.MRC()
mrc_component = appbuilder.MRC(model="eb-turbo-appbuilder")

# 初始化参数
msg = "残疾人怎么办相关证件"
Expand Down Expand Up @@ -43,11 +43,11 @@ print(result)

- `msg (obj:Message)`: 输入消息,包含用户提出的问题。这是一个必需的参数。
- `context_list (obj:Message)`: 用户输入的问题对应的段落文本列表。这是一个必需的参数。
- `reject (bool, 可选)`: 拒绝开关,如果为 True,则启用拒绝能力。默认为 False。
- `clarify (bool, 可选)`: 澄清开关,如果为 True,则启用澄清能力。默认为 False。
- `highlight (bool, 可选)`: 重点强调开关,如果为 True,则启用重点强调能力。默认为 False。
- `friendly (bool, 可选)`: 友好性提升开关,如果为 True,则启用友好性提升能力。默认为 False。
- `cite (bool, 可选)`: 溯源开关,如果为 True,则启用溯源能力。默认为 False。
- `reject (bool, 可选)`: 拒绝开关,如果为 True,则启用该能力。默认为 False。当输入的问题在context_list中没有找到答案时,开关开启时,模型会用特定话术("当前文档库找不到对应的答案,我可以尝试用我的常识来回答你。")做回复的开头,并后接自有知识做回复内容
- `clarify (bool, 可选)`: 澄清开关,如果为 True,则启用该能力。默认为 False。 当输入的问题比较模糊、或者主体指代不清晰,且context_list中包含有可以回答该模糊问题的多种潜在备选答案时,开启该开关,大模型会以特定的话术做澄清反问,引导用户继续补充问题发问。举例子,query:发电机的续航时间? Answer: 根据搜索结果得到了xx和xx两种型号的发电机,您的问题具体涉及到哪一个?请补充关键信息,作为完整的问题重新发问
- `highlight (bool, 可选)`: 重点强调开关,如果为 True,则启用该能力。默认为 False。开启该功能时,回复结果中会高亮显示关键部分的内容
- `friendly (bool, 可选)`: 友好性提升开关,如果为 True,则启用该能力。默认为 False。开关开启时,部分回复的开头会加礼貌用语。且如果回答涉及到大段的信息,会倾向于以<总-分>或者<总-分-总>的形式做分点论述,使得答案的格式更规整,可读性更强
- `cite (bool, 可选)`: 溯源开关,如果为 True,则启用该能力。默认为 False。开关开启时,回复内容后会接形如(^[1]^)的标记来表示回答内容在原文(context_list)中的来源索引。例如:按照当地公安机关出入境管理部门规定的其他材料办理^[2]^
- `stream (bool, 可选)`: 指定是否以流式形式返回响应。默认为 False。
- `temperature (float, 可选)`: 模型配置的温度参数,用于调整模型的生成概率。取值范围为 0.0 到 1.0,其中较低的值使生成更确定性,较高的值使生成更多样性。默认值为 1e-10。

Expand All @@ -74,7 +74,7 @@ import os
os.environ["APPBUILDER_TOKEN"] = '...'

# 创建MRC对象
mrc_component = appbuilder.MRC()
mrc_component = appbuilder.MRC(model="eb-turbo-appbuilder")

# 初始化参数
msg = "残疾人怎么办相关证件"
Expand Down
5 changes: 3 additions & 2 deletions appbuilder/core/components/llms/query_rewrite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import appbuilder
os.environ["APPBUILDER_TOKEN"] = '...'

# 初始化并使用 QueryRewrite 组件
query_rewrite = appbuilder.QueryRewrite(model="ernie-bot-4")
query_rewrite = appbuilder.QueryRewrite(model="eb-turbo-appbuilder")
answer = query_rewrite(appbuilder.Message(['我应该怎么办理护照?', '您可以查询官网或人工咨询', '我需要准备哪些材料?', '身份证、免冠照片一张以及填写完整的《中国公民因私出国(境)申请表》', '在哪里办']), rewrite_type="带机器人回复")
```
print(answer)
```

## 参数说明

### 初始化参数
Expand Down
7 changes: 0 additions & 7 deletions appbuilder/core/components/llms/tag_extraction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,4 @@ result = tag_extraction(appbuilder.Message("自定义模型抽取的标签"))

在实际应用中,`TagExtraction` 可以用于新闻文章、社交媒体帖子或其他任何文本内容的关键标签提取,帮助内容创建者或营销分析师快速了解主要主题和趋势。

## API文档

更详细的API文档,请参考 [AppBuilder TagExtraction API Documentation](#).

## 更新记录和贡献

- v1.0: 初始版本,提供基本的标签抽取功能。
如果您有兴趣贡献代码或提供反馈,请访问 [GitHub repository](#)
11 changes: 7 additions & 4 deletions appbuilder/core/components/object_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ def _recognize(self, request: ObjectRecognitionRequest, timeout: float = None,
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__._check_service_error(data)
request_id = self.http_client.response_request_id(response)
self.__class__._check_service_error(request_id,data)
object_response = ObjectRecognitionResponse.from_json(payload=json.dumps(data))
object_response.request_id = request_id
return object_response

@staticmethod
def _check_service_error(data: dict):
def _check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
request (dict) : 通用物体与场景识别body返回
返回:
"""
if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
17 changes: 10 additions & 7 deletions appbuilder/core/components/text_to_image/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def submitText2ImageTask(self, request: Text2ImageSubmitRequest, timeout: float
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__.check_service_error(data)
request_id = response.headers.get('X-Appbuilder-Request-Id')
request_id = self.http_client.response_request_id(response)
self.__class__.check_service_error(request_id, data)
response = Text2ImageSubmitResponse.from_json(payload=json.dumps(data))
response.request_id = request_id
return response
Expand Down Expand Up @@ -135,8 +135,8 @@ def queryText2ImageData(self, request: Text2ImageQueryRequest, timeout: float =
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__.check_service_error(data)
request_id = response.headers.get('X-Appbuilder-Request-Id')
request_id = self.http_client.response_request_id(response)
self.__class__.check_service_error(request_id, data)
response = Text2ImageQueryResponse.from_json(payload=json.dumps(data))
response.request_id = request_id
return response
Expand All @@ -161,7 +161,7 @@ def extract_img_urls(self, response: Text2ImageQueryResponse):
return img_urls

@staticmethod
def check_service_error(data: dict):
def check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
Expand All @@ -170,5 +170,8 @@ def check_service_error(data: dict):
"""
if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
3 changes: 2 additions & 1 deletion appbuilder/core/components/translate/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ def _translate(self, request: TranslateRequest, timeout: float = None,

self.http_client.check_response_header(response)
data = response.json()
request_id = self.http_client.response_request_id(response)
self.http_client.check_response_json(data)
if "error_code" in data and "error_msg" in data:
raise AppBuilderServerException(service_err_code=data["error_code"], service_err_message=data["error_msg"])
raise AppBuilderServerException(request_id=request_id, service_err_code=data["error_code"], service_err_message=data["error_msg"])

json_str = json.dumps(data)
return TranslateResponse(TranslateResponse.from_json(json_str))
8 changes: 5 additions & 3 deletions appbuilder/core/components/tts/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,19 @@ def __synthesis(self,
response = self.http_client.session.post(url, json=TTSRequest.to_dict(request), timeout=timeout, headers=auth_header)
self.http_client.check_response_header(response)
content_type = response.headers.get("Content-Type", "application/json")
request_id = self.http_client.response_request_id(response)
if content_type.find("application/json") != -1:
data = response.json()
self.http_client.check_response_json(data)
self.__class__.__check_service_error(data)
self.__class__.__check_service_error(request_id, data)
return TTSResponse(
binary=response.content,
request_id=self.http_client.response_request_id(response),
request_id=request_id,
aue=request.aue
)

@staticmethod
def __check_service_error(data: dict):
def __check_service_error(request_id: str, data: dict):
r"""个性化服务response检查
参数:
Expand All @@ -175,6 +176,7 @@ def __check_service_error(data: dict):

if "err_no" in data or "err_msg" in data or 'sn' in data or 'idx' in data:
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("err_no", 0),
service_err_message="{} . {} . {}]".
format(data.get("err_msg", ""),
Expand Down
4 changes: 2 additions & 2 deletions appbuilder/tests/test_asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def test_check_service_error(self):
"""
data = {'err_msg': 'Error', 'err_no': 1}
with self.assertRaises(appbuilder.AppBuilderServerException):
self.asr._check_service_error(data)
self.asr._check_service_error("",data)
data = {'err_msg': 'No Error', 'err_no': 0}
self.assertIsNone(self.asr._check_service_error(data))
self.assertIsNone(self.asr._check_service_error("", data))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion appbuilder/tests/test_text_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_check_service_error(self):
"""
data = {"error_code": "ERROR", "error_msg": "Error message"}
with self.assertRaises(appbuilder.AppBuilderServerException):
self.text2Image.check_service_error(data)
self.text2Image.check_service_error("", data)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit a009a0b

Please sign in to comment.