如何处理没有生成查询的情况
先决条件
本指南假定您熟悉以下内容
有时,查询分析技术允许生成任意数量的查询 - 包括没有查询!在这种情况下,我们的整体链需要在决定是否调用检索器之前检查查询分析的结果。
我们将在此示例中使用模拟数据。
设置
安装依赖项
提示
有关安装集成包的一般说明,请参见此部分.
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/openai zod chromadb
yarn add @langchain/community @langchain/openai zod chromadb
pnpm add @langchain/community @langchain/openai 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."),
});
选择您的聊天模型
- OpenAI
- Anthropic
- FireworksAI
- MistralAI
- Groq
- VertexAI
安装依赖项
提示
- npm
- yarn
- pnpm
npm i @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
添加环境变量
OPENAI_API_KEY=your-api-key
实例化模型
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0
});
安装依赖项
提示
- npm
- yarn
- pnpm
npm i @langchain/anthropic
yarn add @langchain/anthropic
pnpm add @langchain/anthropic
添加环境变量
ANTHROPIC_API_KEY=your-api-key
实例化模型
import { ChatAnthropic } from "@langchain/anthropic";
const llm = new ChatAnthropic({
model: "claude-3-5-sonnet-20240620",
temperature: 0
});
安装依赖项
提示
- npm
- yarn
- pnpm
npm i @langchain/community
yarn add @langchain/community
pnpm add @langchain/community
添加环境变量
FIREWORKS_API_KEY=your-api-key
实例化模型
import { ChatFireworks } from "@langchain/community/chat_models/fireworks";
const llm = new ChatFireworks({
model: "accounts/fireworks/models/llama-v3p1-70b-instruct",
temperature: 0
});
安装依赖项
提示
- npm
- yarn
- pnpm
npm i @langchain/mistralai
yarn add @langchain/mistralai
pnpm add @langchain/mistralai
添加环境变量
MISTRAL_API_KEY=your-api-key
实例化模型
import { ChatMistralAI } from "@langchain/mistralai";
const llm = new ChatMistralAI({
model: "mistral-large-latest",
temperature: 0
});
安装依赖项
提示
- npm
- yarn
- pnpm
npm i @langchain/groq
yarn add @langchain/groq
pnpm add @langchain/groq
添加环境变量
GROQ_API_KEY=your-api-key
实例化模型
import { ChatGroq } from "@langchain/groq";
const llm = new ChatGroq({
model: "mixtral-8x7b-32768",
temperature: 0
});
安装依赖项
提示
- npm
- yarn
- pnpm
npm i @langchain/google-vertexai
yarn add @langchain/google-vertexai
pnpm add @langchain/google-vertexai
添加环境变量
GOOGLE_APPLICATION_CREDENTIALS=credentials.json
实例化模型
import { ChatVertexAI } from "@langchain/google-vertexai";
const llm = new ChatVertexAI({
model: "gemini-1.5-flash",
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 }
}
后续步骤
您现在已经了解了一些在查询分析系统中处理无关问题的方法。
接下来,查看本节中其他一些查询分析指南,例如如何使用少样本示例。