跳至主要内容

如何处理没有生成查询的情况

先决条件

本指南假定您熟悉以下内容

有时,查询分析技术可能允许生成任意数量的查询,包括没有查询!在这种情况下,我们的整体链需要在决定是否调用检索器之前检查查询分析的结果。

在本例中,我们将使用模拟数据。

设置

安装依赖项

yarn add @langchain/community @langchain/openai @langchain/core zod chromadb

设置环境变量

OPENAI_API_KEY=your-api-key

# Optional, use LangSmith for best-in-class observability
LANGSMITH_API_KEY=your-api-key
LANGCHAIN_TRACING_V2=true

# Reduce tracing latency if you are not in a serverless environment
# LANGCHAIN_CALLBACKS_BACKGROUND=true

创建索引

我们将基于虚假信息创建一个向量存储。

import { Chroma } from "@langchain/community/vectorstores/chroma";
import { OpenAIEmbeddings } from "@langchain/openai";
import "chromadb";

const texts = ["Harrison worked at Kensho"];
const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" });
const vectorstore = await Chroma.fromTexts(texts, {}, embeddings, {
collectionName: "harrison",
});
const retriever = vectorstore.asRetriever(1);

查询分析

我们将使用函数调用来构建输出。但是,我们将配置 LLM,使其不必调用代表搜索查询的函数(如果它决定不调用)。然后,我们还将使用提示进行查询分析,该提示明确说明它何时应该进行搜索,以及何时不应该进行搜索。

import { z } from "zod";

const searchSchema = z.object({
query: z.string().describe("Similarity search query applied to job record."),
});

选择你的聊天模型

安装依赖项

yarn add @langchain/openai 

添加环境变量

OPENAI_API_KEY=your-api-key

实例化模型

import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0
});
import { zodToJsonSchema } from "zod-to-json-schema";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
RunnableSequence,
RunnablePassthrough,
} from "@langchain/core/runnables";

const system = `You have the ability to issue search queries to get information to help answer user information.

You do not NEED to look things up. If you don't need to, then just respond normally.`;
const prompt = ChatPromptTemplate.fromMessages([
["system", system],
["human", "{question}"],
]);
const llmWithTools = llm.bind({
tools: [
{
type: "function" as const,
function: {
name: "search",
description: "Search over a database of job records.",
parameters: zodToJsonSchema(searchSchema),
},
},
],
});
const queryAnalyzer = RunnableSequence.from([
{
question: new RunnablePassthrough(),
},
prompt,
llmWithTools,
]);

我们可以看到,通过调用它,我们得到了一条消息,该消息有时(但并非总是)会返回工具调用。

await queryAnalyzer.invoke("where did Harrison work");
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "",
additional_kwargs: {
function_call: undefined,
tool_calls: [
{
id: "call_uqHm5OMbXBkmqDr7Xzj8EMmd",
type: "function",
function: [Object]
}
]
}
},
lc_namespace: [ "langchain_core", "messages" ],
content: "",
name: undefined,
additional_kwargs: {
function_call: undefined,
tool_calls: [
{
id: "call_uqHm5OMbXBkmqDr7Xzj8EMmd",
type: "function",
function: { name: "search", arguments: '{"query":"Harrison"}' }
}
]
}
}
await queryAnalyzer.invoke("hi!");
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "Hello! How can I assist you today?",
additional_kwargs: { function_call: undefined, tool_calls: undefined }
},
lc_namespace: [ "langchain_core", "messages" ],
content: "Hello! How can I assist you today?",
name: undefined,
additional_kwargs: { function_call: undefined, tool_calls: undefined }
}

使用查询分析进行检索

那么我们如何将它包含在链中?让我们看看下面的示例。

import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";

const outputParser = new JsonOutputKeyToolsParser({
keyName: "search",
});
import { RunnableConfig, RunnableLambda } from "@langchain/core/runnables";

const chain = async (question: string, config?: RunnableConfig) => {
const response = await queryAnalyzer.invoke(question, config);
if (
"tool_calls" in response.additional_kwargs &&
response.additional_kwargs.tool_calls !== undefined
) {
const query = await outputParser.invoke(response, config);
return retriever.invoke(query[0].query, config);
} else {
return response;
}
};

const customChain = new RunnableLambda({ func: chain });
await customChain.invoke("where did Harrison Work");
[ Document { pageContent: "Harrison worked at Kensho", metadata: {} } ]
await customChain.invoke("hi!");
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "Hello! How can I assist you today?",
additional_kwargs: { function_call: undefined, tool_calls: undefined }
},
lc_namespace: [ "langchain_core", "messages" ],
content: "Hello! How can I assist you today?",
name: undefined,
additional_kwargs: { function_call: undefined, tool_calls: undefined }
}

后续步骤

现在,您已经了解了一些在查询分析系统中处理不相关问题的方法。

接下来,查看本节中其他一些查询分析指南,例如如何使用少样本示例


此页面是否有帮助?


您也可以在 GitHub 上留下详细的反馈 在 GitHub 上.