如何处理未生成查询的情况
先决条件
本指南假设您熟悉以下内容
有时,查询分析技术可能允许生成任意数量的查询 - 包括不生成查询!在这种情况下,我们的整体链需要检查查询分析的结果,然后再决定是否调用检索器。
我们将在此示例中使用模拟数据。
设置
安装依赖项
提示
有关安装集成包的一般说明,请参阅此部分。
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/openai @langchain/core zod chromadb
yarn add @langchain/community @langchain/openai @langchain/core zod chromadb
pnpm 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
LANGSMITH_TRACING=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."),
});
选择您的聊天模型
- Groq
- OpenAI
- Anthropic
- FireworksAI
- MistralAI
- VertexAI
安装依赖项
提示
请参阅 此部分,了解有关安装集成包的一般说明.
- 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: "llama-3.3-70b-versatile",
temperature: 0
});
安装依赖项
提示
请参阅 此部分,了解有关安装集成包的一般说明.
- 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/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 }
}
下一步
您现在已经学习了一些在查询分析系统中处理不相关问题的技术。
接下来,查看本节中的其他查询分析指南,例如如何使用少量示例。