我正在按照 Pixegami 制作的这个教程来创建适用于瑞典建筑师的施工规则。而且我是初学者,所以问题可能很简单,与构建 RAG 的具体任务无关。
这是“我的”代码:
import argparse
from dataclasses import dataclass
from langchain.vectorstores.chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_community.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
CHROMA_PATH = "chroma"
PROMPT_TEMPLATE = """
Answer the question based only on the following context:
{context}
---
Answer the question based on the above context: {question}
"""
def main():
# Create CLI.
parser = argparse.ArgumentParser()
parser.add_argument("query_text", type=str, help="is the word regler used in this text?")
args = parser.parse_args()
query_text = args.query_text
# Prepare the DB.
embedding_function = OpenAIEmbeddings()
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
# Search the DB.
results = db.similarity_search_with_relevance_scores(query_text, k=3)
if len(results) == 0 or results[0][1] < 0.7:
print(f"Unable to find matching results.")
return
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
prompt = prompt_template.format(context=context_text, question=query_text)
print(prompt)
model = ChatOpenAI()
response_text = model.predict(prompt)
sources = [doc.metadata.get("source", None) for doc, _score in results]
formatted_response = f"Response: {response_text}\nSources: {sources}"
print(formatted_response)
if __name__ == "__main__":
main()
当我运行代码时,我收到以下错误消息:
usage: query_data.py [-h] query_text
query_data.py: error: the following arguments are required: query_text
起初我问 ChatGPT 可能出了什么问题,得到的答案是可能不允许使用非英文字符,所以现在我尝试仅使用英文字符更改查询,但仍然无法正常工作。我想我可能没有
argsparse
所以我通过pip install argparse
安装了它。
我可以看到 Pixegami 编辑器中的代码颜色与我的不同。
"query_text = args"
在我的编辑器中是浅蓝色的,其余部分是白色的,但在他的编辑器中整行都是白色的。
这就是我陷入困境的地方,如果需要,我很乐意提供更多信息。
用法:query_data.py [-h] query_text
这意味着您应该在执行命令的同时发布您的query_text 例子 python yourcode.py“query_text”