labring / FastGPT

FastGPT is a knowledge-based platform built on the LLMs, offers a comprehensive suite of out-of-the-box capabilities such as data processing, RAG retrieval, and visual AI workflow orchestration, letting you easily develop and deploy complex question-answering systems without the need for extensive setup or configuration.
https://fastgpt.in
Other
15.58k stars 4.09k forks source link

请提供一个支持百度千帆平台上bce_reranker_base的接口,在线rerank模型的支持有限。 #1968

Closed martin0359 closed 1 week ago

martin0359 commented 1 week ago

例行检查

你的版本

问题描述, 日志截图

复现步骤

预期结果

相关截图

xy3xy3 commented 1 week ago

已实现:https://blog.skyw.cc/archives/312.html

martin0359 commented 1 week ago

已实现:https://blog.skyw.cc/archives/312.html

感谢,正在测试,但docker中的容器报错,日志:

2024/07/06 23:02:13 | stdout | INFO:     172.17.0.1:46382 - "POST /v1/rerank HTTP/1.1" 200 OK -- | -- | -- 2024/07/06 23:02:13 | stdout | api return error, req_id:  code: 14, msg: IAM Certification failed 可能的原因: IAM 鉴权失败,请检查 Access Key 与 Secret Key 是否正确,当前使用的 Access Key 为 `zjQTUb***` 2024/07/06 23:02:13 | stdout | 报错: 2024/07/06 23:02:13 | stderr | [ERROR][2024-07-06 15:02:13.846] openapi_requestor.py:258 [t:140702030883712]: api request req_id:  failed with error code: 14, err msg: IAM Certification failed 可能的原因: IAM 鉴权失败,请检查 Access Key 与 Secret Key 是否正确,当前使用的 Access Key 为 `zjQTUb***`, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/06 23:02:13 | stderr | response body: b'' 2024/07/06 23:02:13 | stderr | response headers: {'Bfe-Trace-Id': '4f17822716bbe57632a72fe9e63c80d6', 'Content-Length': '0', 'Date': 'Sat, 06 Jul 2024 15:02:13 GMT', 'X-Bce-Error-Code': 'IamSignatureInvalid', 'X-Bce-Error-Message': 'IamSignatureInvalid, cause: Could not find credential.', 'X-Bce-Exception-Point': 'Gateway', 'X-Bce-Gateway-Region': 'BJ', 'X-Bce-Request-Id': 'd104c0a4-630f-42f3-94ed-07de0a2ca773', 'Content-Type': 'text/plain; charset=utf-8'} 2024/07/06 23:02:13 | stderr | request body: '{}' 2024/07/06 23:02:13 | stderr | request headers: {'User-Agent': 'python-requests/2.32.3', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Host': 'qianfan.baidubce.com', 'request-source': 'qianfan_py_sdk_v0.4.0.1', 'x-bce-date': '2024-07-06T15:02:13Z', 'Authorization': 'bce-auth-v1/zjQTUb5qFi74oMKl6aVzNJpS/2024-07-06T15:02:13Z/300/content-type;host;request-source;x-bce-date/fd9b1ef396961a73b31887bc487e7d6c2e1fb0653698c6853d8cee4f890e780d', 'Content-Length': '2'} 2024/07/06 23:02:13 | stderr | error message from baidu: IamSignatureInvalid, cause: Could not find credential. 2024/07/06 23:02:13 | stderr | error code from baidu: IamSignatureInvalid 2024/07/06 23:02:13 | stderr | [WARNING][2024-07-06 15:02:13.691] base.py:1018 [t:140702030883712]: fetch_supported_models failed: http request url https://qianfan.baidubce.com/wenxinworkshop/service/list failed with http status code 403 2024/07/06 23:02:13 | stderr | response body: b'' 2024/07/06 23:02:13 | stderr | response headers: {'Bfe-Trace-Id': '4f17822716bbe57632a72fe9e63c80d6', 'Content-Length': '0', 'Date': 'Sat, 06 Jul 2024 15:02:13 GMT', 'X-Bce-Error-Code': 'IamSignatureInvalid', 'X-Bce-Error-Message': 'IamSignatureInvalid, cause: Could not find credential.', 'X-Bce-Exception-Point': 'Gateway', 'X-Bce-Gateway-Region': 'BJ', 'X-Bce-Request-Id': 'd104c0a4-630f-42f3-94ed-07de0a2ca773', 'Content-Type': 'text/plain; charset=utf-8'} 2024/07/06 23:02:13 | stderr | request body: '{}' 2024/07/06 23:02:13 | stderr | request headers: {'User-Agent': 'python-requests/2.32.3', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Host': 'qianfan.baidubce.com', 'request-source': 'qianfan_py_sdk_v0.4.0.1', 'x-bce-date': '2024-07-06T15:02:13Z', 'Authorization': 'bce-auth-v1/zjQTUb5qFi74oMKl6aVzNJpS/2024-07-06T15:02:13Z/300/content-type;host;request-source;x-bce-date/fd9b1ef396961a73b31887bc487e7d6c2e1fb0653698c6853d8cee4f890e780d', 'Content-Length': '2'} 2024/07/06 23:02:13 | stderr | error message from baidu: IamSignatureInvalid, cause: Could not find credential. 2024/07/06 23:02:13 | stderr | error code from baidu: IamSignatureInvalid 2024/07/06 23:02:13 | stderr | [ERROR][2024-07-06 15:02:13.691] base.py:133 [t:140702030883712]: http request url https://qianfan.baidubce.com/wenxinworkshop/service/list failed with http status code 403 2024/07/06 23:01:55 | stdout | INFO:     172.17.0.1:46358 - "GET / HTTP/1.1" 404 Not Found 2024/07/06 22:58:26 | stderr | INFO:     Uvicorn running on http://0.0.0.0:6008 (Press CTRL+C to quit) 2024/07/06 22:58:26 | stderr | INFO:     Application startup complete. 2024/07/06 22:58:26 | stderr | INFO:     Waiting for application startup. 2024/07/06 22:58:26 | stderr | INFO:     Started server process [1]

martin0359 commented 1 week ago

鉴权问题已解决,错误使用了应用的AK/SK,应使用安全中心中的AK/SK.现在出现新的报错

date | stream | content -- | -- | -- 2024/07/07 11:47:41 | stdout | INFO:     172.17.0.1:38408 - "POST /v1/rerank HTTP/1.1" 200 OK 2024/07/07 11:47:41 | stderr | [ERROR][2024-07-07 03:47:41.928] openapi_requestor.py:258 [t:140683129891712]: api request req_id: as-iy3zuhyrdn failed with error code: 336221, err msg: max number of documents is 64, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:47:41 | stdout | api return error, req_id: as-iy3zuhyrdn code: 336221, msg: max number of documents is 64 2024/07/07 11:47:41 | stdout | 报错: 2024/07/07 11:46:35 | stdout | INFO:     172.17.0.1:38356 - "POST /v1/rerank HTTP/1.1" 200 OK 2024/07/07 11:46:35 | stdout | api return error, req_id: as-48ug1xzh5h code: 336221, msg: max number of documents is 64 2024/07/07 11:46:35 | stdout | 报错: 2024/07/07 11:46:35 | stderr | [ERROR][2024-07-07 03:46:35.462] openapi_requestor.py:258 [t:140683129891712]: api request req_id: as-48ug1xzh5h failed with error code: 336221, err msg: max number of documents is 64, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:46:29 | stdout | INFO:     172.17.0.1:38342 - "GET / HTTP/1.1" 404 Not Found 2024/07/07 11:46:28 | stderr | INFO:     Uvicorn running on http://0.0.0.0:6008 (Press CTRL+C to quit) 2024/07/07 11:46:28 | stderr | INFO:     Application startup complete. 2024/07/07 11:46:28 | stderr | INFO:     Waiting for application startup. 2024/07/07 11:46:28 | stderr | INFO:     Started server process [1]

xy3xy3 commented 1 week ago

鉴权问题已解决,错误使用了应用的AK/SK,应使用安全中心中的AK/SK.现在出现新的报错

date | stream | content -- | -- | -- 2024/07/07 11:47:41 | stdout | INFO:     172.17.0.1:38408 - "POST /v1/rerank HTTP/1.1" 200 OK 2024/07/07 11:47:41 | stderr | [ERROR][2024-07-07 03:47:41.928] openapi_requestor.py:258 [t:140683129891712]: api request req_id: as-iy3zuhyrdn failed with error code: 336221, err msg: max number of documents is 64, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:47:41 | stdout | api return error, req_id: as-iy3zuhyrdn code: 336221, msg: max number of documents is 64 2024/07/07 11:47:41 | stdout | 报错: 2024/07/07 11:46:35 | stdout | INFO:     172.17.0.1:38356 - "POST /v1/rerank HTTP/1.1" 200 OK 2024/07/07 11:46:35 | stdout | api return error, req_id: as-48ug1xzh5h code: 336221, msg: max number of documents is 64 2024/07/07 11:46:35 | stdout | 报错: 2024/07/07 11:46:35 | stderr | [ERROR][2024-07-07 03:46:35.462] openapi_requestor.py:258 [t:140683129891712]: api request req_id: as-48ug1xzh5h failed with error code: 336221, err msg: max number of documents is 64, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:46:29 | stdout | INFO:     172.17.0.1:38342 - "GET / HTTP/1.1" 404 Not Found 2024/07/07 11:46:28 | stderr | INFO:     Uvicorn running on http://0.0.0.0:6008 (Press CTRL+C to quit) 2024/07/07 11:46:28 | stderr | INFO:     Application startup complete. 2024/07/07 11:46:28 | stderr | INFO:     Waiting for application startup. 2024/07/07 11:46:28 | stderr | INFO:     Started server process [1]

max number of documents is 64 文档数量太多

martin0359 commented 1 week ago

鉴权问题已解决,错误使用了应用的AK/SK,应使用安全中心中的AK/SK.现在出现新的报错 date | stream | content -- | -- | -- 2024/07/07 11:47:41 | stdout | INFO:     172.17.0.1:38408 - "POST /v1/rerank HTTP/1.1" 200 OK 2024/07/07 11:47:41 | stderr | [ERROR][2024-07-07 03:47:41.928] openapi_requestor.py:258 [t:140683129891712]: api request req_id: as-iy3zuhyrdn failed with error code: 336221, err msg: max number of documents is 64, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:47:41 | stdout | api return error, req_id: as-iy3zuhyrdn code: 336221, msg: max number of documents is 64 2024/07/07 11:47:41 | stdout | 报错: 2024/07/07 11:46:35 | stdout | INFO:     172.17.0.1:38356 - "POST /v1/rerank HTTP/1.1" 200 OK 2024/07/07 11:46:35 | stdout | api return error, req_id: as-48ug1xzh5h code: 336221, msg: max number of documents is 64 2024/07/07 11:46:35 | stdout | 报错: 2024/07/07 11:46:35 | stderr | [ERROR][2024-07-07 03:46:35.462] openapi_requestor.py:258 [t:140683129891712]: api request req_id: as-48ug1xzh5h failed with error code: 336221, err msg: max number of documents is 64, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:46:29 | stdout | INFO:     172.17.0.1:38342 - "GET / HTTP/1.1" 404 Not Found 2024/07/07 11:46:28 | stderr | INFO:     Uvicorn running on http://0.0.0.0:6008 (Press CTRL+C to quit) 2024/07/07 11:46:28 | stderr | INFO:     Application startup complete. 2024/07/07 11:46:28 | stderr | INFO:     Waiting for application startup. 2024/07/07 11:46:28 | stderr | INFO:     Started server process [1]

max number of documents is 64 文档数量太多

任何知识库的文档都是远超这个限制的。

xy3xy3 commented 1 week ago

鉴权问题已解决,错误使用了应用的AK/SK,应使用安全中心中的AK/SK.现在出现新的报错 date |流 |内容 -- |-- |-- 2024/07/07 11:47:41 |标准 |信息: 172.17.0.1:38408 - “POST /v1/rerank HTTP/1.1” 200 OK 2024/07/07 11:47:41 |斯特德尔 |[错误][2024-07-07 03:47:41.928] openapi_requestor.py:258 [t:140683129891712]:API 请求req_id:as-iy3zuhyrdn 失败,错误代码:336221,错误消息:最大文档数为 64,请检查 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:47:41 |标准 |API 返回错误,req_id:AS-IY3ZUHYRDN 代码:336221,消息:最大文档数为 64 2024/07/07 11:47:41 |标准 |报错: 2024/07/07 11:46:35 |标准 |信息: 172.17.0.1:38356 - “发布 /v1/rerank HTTP/1.1” 200 确定 2024/07/07 11:46:35 |标准 |API 返回错误,req_id:AS-48UG1XZH5H 代码:336221,消息:最大文档数为 64 2024/07/07 11:46:35 |标准 |报错: 2024/07/07 11:46:35 |斯特德尔 |[错误][2024-07-07 03:46:35.462] openapi_requestor.py:258 [t:140683129891712]:API 请求req_id:as-48ug1xzh5h 失败,错误代码:336221,错误消息:最大文档数为 64,请检查 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:46:29 |标准 |信息: 172.17.0.1:38342 - “GET / HTTP/1.1” 404 未找到 2024/07/07 11:46:28 |斯特德尔 |信息:Uvicorn 在 http://0.0.0.0:6008 上运行(按 CTRL+C 退出) 2024/07/07 11:46:28 |斯特德尔 |信息:应用程序启动完成。2024/07/07 11:46:28 |斯特德尔 |信息:正在等待应用程序启动。2024/07/07 11:46:28 |斯特德尔 |信息:已启动服务器进程 [1]

最大文档数量为 64 文档数量太多

任何知识库的文档都是远超这个限制的。

修改小一点召回token。是你最后召回的内容太多了

martin0359 commented 1 week ago

鉴权问题已解决,错误使用了应用的AK/SK,应使用安全中心中的AK/SK.现在出现新的报错 date |流 |内容 -- |-- |-- 2024/07/07 11:47:41 |标准 |信息: 172.17.0.1:38408 - “POST /v1/rerank HTTP/1.1” 200 OK 2024/07/07 11:47:41 |斯特德尔 |[错误][2024-07-07 03:47:41.928] openapi_requestor.py:258 [t:140683129891712]:API 请求req_id:as-iy3zuhyrdn 失败,错误代码:336221,错误消息:最大文档数为 64,请检查 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:47:41 |标准 |API 返回错误,req_id:AS-IY3ZUHYRDN 代码:336221,消息:最大文档数为 64 2024/07/07 11:47:41 |标准 |报错: 2024/07/07 11:46:35 |标准 |信息: 172.17.0.1:38356 - “发布 /v1/rerank HTTP/1.1” 200 确定 2024/07/07 11:46:35 |标准 |API 返回错误,req_id:AS-48UG1XZH5H 代码:336221,消息:最大文档数为 64 2024/07/07 11:46:35 |标准 |报错: 2024/07/07 11:46:35 |斯特德尔 |[错误][2024-07-07 03:46:35.462] openapi_requestor.py:258 [t:140683129891712]:API 请求req_id:as-48ug1xzh5h 失败,错误代码:336221,错误消息:最大文档数为 64,请检查 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh 2024/07/07 11:46:29 |标准 |信息: 172.17.0.1:38342 - “GET / HTTP/1.1” 404 未找到 2024/07/07 11:46:28 |斯特德尔 |信息:Uvicorn 在 http://0.0.0.0:6008 上运行(按 CTRL+C 退出) 2024/07/07 11:46:28 |斯特德尔 |信息:应用程序启动完成。2024/07/07 11:46:28 |斯特德尔 |信息:正在等待应用程序启动。2024/07/07 11:46:28 |斯特德尔 |信息:已启动服务器进程 [1]

最大文档数量为 64 文档数量太多

任何知识库的文档都是远超这个限制的。

修改小一点召回token。是你最后召回的内容太多了

你好,能不能留一个telegram,这个召回token在哪里修改?

martin0359 commented 1 week ago

好了,解决了,修改了你的部分代码,按照模型要求切分了,你检查后重新发布一下,我跑了下rerank的权重分比之前的本地模型低了一些,一般0.9左右的变成0.6了,但是速度比nas部署本地模型快了很多。 import os import uvicorn from fastapi import FastAPI, Security, HTTPException from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from pydantic import BaseModel from typing import Optional, List

导入相关库和模块

app = FastAPI() security = HTTPBearer() env_bearer_token = 'your_token' # 替换为您的token

class QADocs(BaseModel): query: Optional[str] documents: Optional[List[str]]

定义请求体数据模型

class Singleton(type): def call(cls, *args, *kwargs): if not hasattr(cls, '_instance'): cls._instance = super().call(args, **kwargs) return cls._instance

单例模式,确保只有一个ReRanker实例

class ReRanker(metaclass=Singleton): def init(self): os.environ["QIANFAN_ACCESS_KEY"] = "your_access_key" # 替换为您的access key os.environ["QIANFAN_SECRET_KEY"] = "your_secret_key" # 替换为您的secret key self.reranker = Reranker(model="bce-reranker-base_v1") # 初始化reranker模型

def compute_score(self, query: str, documents: List[str]):
    if len(documents) > 0:
        result = self.reranker.do(query, documents)
        return result.body['results']
    else:
        return None

ReRanker类,用于计算文档与查询的相关性分数

class Chat(object): def init(self): self.reranker = ReRanker() self.max_docs = 64 # 设置最大文档数量限制

def fit_query_answer_rerank(self, query_docs: QADocs) -> List:
    if query_docs is None or len(query_docs.documents) == 0:
        return []

    # 分割documents,确保每批不超过max_docs
    batched_docs = [query_docs.documents[i:i+self.max_docs] for i in range(0, len(query_docs.documents), self.max_docs)]

    all_results = []
    for docs in batched_docs:
        scores = self.reranker.compute_score(query_docs.query, docs)

        new_docs = []
        for index, score in enumerate(scores):
            new_docs.append({"index": score['index'], "text": docs[score['index']], "score": score['relevance_score']})
        results = [{"index": documents["index"], "relevance_score": documents["score"]} for documents in list(sorted(new_docs, key=lambda x: x["score"], reverse=True))]
        all_results.extend(results)

    return all_results

Chat类,用于处理查询和文档,并返回重排后的结果

@app.post('/v1/rerank') async def handle_post_request(docs: QADocs, credentials: HTTPAuthorizationCredentials = Security(security)): token = credentials.credentials if env_bearer_token is not None and token != env_bearer_token: raise HTTPException(status_code=401, detail="Invalid token") chat = Chat() try: results = chat.fit_query_answer_rerank(docs) return {"results": results} except Exception as e: print(f"报错:\n{e}") return {"error": "重排出错"}

API端点,接收查询和文档,返回重排后的结果

if name == "main": token = os.getenv("ACCESS_TOKEN") if token is not None: env_bearer_token = token try: uvicorn.run(app, host='0.0.0.0', port=6008) except Exception as e: print(f"API启动失败!\n报错:\n{e}")

c121914yu commented 1 week ago

👍 可以再调整下 md 格式,便于阅读