gpt4 book ai didi

openai-api - 如何让ChatGPT返回用自己的数据训练时返回的信息来源

转载 作者:行者123 更新时间:2023-12-02 22:47:56 27 4
gpt4 key购买 nike

我正在尝试通过将数据存储在矢量数据库(Pinecone)中来使用包含我自己的数据来训练 ChatGPT。我正在使用 ChatGPT 检索插件对数据进行矢量化并将其存储在 Python 中。该插件可以在这里找到:https://github.com/openai/chatgpt-retrieval-plugin

遵循 https://betterprogramming.pub/enhancing-chatgpt-with-infinite-external-memory-using-vector-database-and-chatgpt-retrieval-plugin-b6f4ea16ab8 的指南,到目前为止一切都很好。但是,我在访问元数据(即信息来源)时遇到问题,以便可以是作者或网址等。

我相信这需要在下面的函数中完成,请注意,我自己添加了元数据部分:

def upsert_file(directory: str):
"""
Upload all files under a directory to the vector database.
"""
url = "http://0.0.0.0:8000/upsert-file"
headers = {"Authorization": "Bearer " + DATABASE_INTERFACE_BEARER_TOKEN}
files = []
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
file_path = os.path.join(directory, filename)
with open(file_path, "rb") as f:
file_content = f.read()
# files.append(("file", (filename, file_content, "text/plain")))
metadata = {
"source": filename, # Add your metadata values
"author": "Tim Cook",
"url": "Some fake url"
# Add more metadata fields as needed
}
print(metadata)
files = {
"file": (filename, file_content, "text/plain"),
"metadata": (None, json.dumps(metadata), "application/json"),
}
# response = requests.post(url, headers=headers, files=files, timeout=600)

response = requests.post(url,
headers=headers,
files=files,
# data={"metadata": json.dumps(metadata)},
timeout=600)
if response.status_code == 200:
print(filename + " uploaded successfully.")
else:
print(
f"Error: {response.status_code} {response.content} for uploading "
+ filename)

我的问题是文件仍然被矢量化/存储在松果中,但元数据仍然返回为None,如下所示:

metadata': {'source': 'file', 'source_id': None, 'url': None, 'created_at': None, 'author': None, 'document_id': 'Some_Doc_Id_here_that_is_not_None'}

我的问题是如何获取元数据?为什么这么多字段都返回 None ?我还应该提到这一行:

"metadata": (None, json.dumps(metadata), "application/json")

如果我要将 None 更改为其他内容,例如 testing,当我尝试更新插入文件时,最终会出现以下错误:

Error: 422 b'{"detail":[{"loc":["body","metadata"],"msg":"str type expected","type":"type_error.str"}]}'

最佳答案

对于遇到同样问题的人来说,元数据显示None的原因是因为元数据必须包含正确的属性。深入研究 GPT 插件的代码,正确的属性是:

class DocumentMetadata(BaseModel):
source: Optional[Source] = None
source_id: Optional[str] = None
url: Optional[str] = None
created_at: Optional[str] = None
author: Optional[str] = None

class Source(str, Enum):
email = "email"
file = "file"
chat = "chat"

我为源提供了一个文件名,这不是 Source 对象的有效属性。

我最终将元数据更新为如下所示,现在我可以检索元数据:

metadata = {"source": "file", "source_id": filename, "url": "https://example.com", "created_at":  str(date.today()), "author": "Tim Cook"}

关于openai-api - 如何让ChatGPT返回用自己的数据训练时返回的信息来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76648707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com