gpt4 book ai didi

openai-api - 如何将LangChain Vector Store加上整个文档传递给OpenAI?

转载 作者:行者123 更新时间:2023-12-02 05:48:59 25 4
gpt4 key购买 nike

我有一些代码几乎是从 langchain docs 逐字复制的。 ,

该代码根据 .txt 文档列表创建向量存储。我的假设是下面的代码从存储中找到与问题相关的所需内容,并使用选定的数据进行 api 调用,而不是将数百行 .txt 传递到有效负载中。

这非常有用,因为我有很多文档需要 AI 引用,但有一个文档特别指定了我希望 AI 执行的一些特定任务。我需要将这个完整的文档以及从商店返回的任何内容传递给 openAI 的请求。有谁知道langchain是否支持此类任务?我对此很陌生,所以我的知识非常有限。寻求一些指导

    const docs = await textSplitter.createDocuments(txtFiles)

// Create a vector store from the documents.
const vectorStore = await HNSWLib.fromDocuments(docs, embeddings)

// Create a chain that uses the OpenAI LLM and HNSWLib vector store.
const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever())

// Call the chain with the prompt.
const chatGptRes = await chain.call({
query: prompt,
})

最佳答案

这就是文档的建议,希望这会有所帮助。

langchain不和谐也很不错,值得一看

一个示例可能是使用目录加载器来获取所有 .json 文件,例如 ./docs 中的文件

    llm = //add yourr openai instance here

//get the query from the request
const userPrompt = "how big is the universe";

// your directory holding your docs ./docs
const directoryLoader = new DirectoryLoader("docs", {
".json": (path) => new JSONLoader(path),
});

// load the documents into the docs variable
const docs = await directoryLoader.load();


// Load the vector store from the directory
const directory = "./vectorstore";
const loadedVectorStore = await HNSWLib.load(
directory,
new OpenAIEmbeddings()
);

const chain = RetrievalQAChain.fromLLM(
llm,
loadedVectorStore.asRetriever()
);

const res = await chain.call({
input_documents: docs,
query: userPrompt,
});



关于openai-api - 如何将LangChain Vector Store加上整个文档传递给OpenAI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76552646/

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