How to select different chains based on the question #2652
Replies: 2 comments
-
🤖 Hi, To conditionally select different chains based on the output of an original LLM chain in LangChainJS, you can use an if-else statement or a switch-case statement to check the output of the original chain and then select the appropriate chain accordingly. Here's an example of how you can do this: // Assume we have another chain, chainB
const chainB = new DocStuffChain({ /* configuration for DocStuffChain */ });
const chainC = new GraphDBChain({ /* configuration for GraphDBChain */ });
// Run the original chain
const resA = await chainA.call({ product: "colorful socks" });
// Check the output and select the appropriate chain
let selectedChain;
if (resA.text.includes("Socktastic")) {
selectedChain = chainB;
} else {
selectedChain = chainC;
}
// Now you can use the selected chain
const resB = await selectedChain.call({ /* appropriate input for the selected chain */ }); In this example, if the output of the original chain ( In the context shared, you can see how to create a custom chain ( You can find more examples and details in the following sources: I hope this helps! If you have any other questions, feel free to ask. Best, Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
I tried the following the getting some decent results but I am curious to see what others have tried: import { z } from "zod";
import { LLMChain } from "langchain/chains";
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { StructuredOutputParser } from "langchain/output_parsers";
const deciderPrompt = `
You need to decided if the question asked is an "aggregate" question or "not-aggregate" question.
An "aggregate" question are questions that enquire about count, sum, total, how many and a "not-aggregate"
question is one that focuses on the how or what. For example, How do I query entity A from system X.
==========================================
It is possible that a question has multiple parts, where a part is an "aggregate" question but other parts
are "not-aggregate" questions. In this case, you should separate the questions and label them accordingly.
==========================================
Your answer should not be description but percises and provided as a JavaScript Array.
==========================================
Examples:
Example 1
Question: How many charts have broken pages report in them?
Answer: question: "How many charts have broken pages report in them?", type: "aggregate"
Example 2
Question: How do I query the reports of my latest crawl?
Answer: question: "How do I query the reports of my latest crawl?", type: "not-aggregate"
Example 3
Question: What does the paginated pages report tell me and how many graphs contain the paginated report in them?
Answer:
question: "What does the paginated pages report tell me?", type: "not-aggregate"
question: "How many graphs contain the paginated report in them?", type: "aggregate"
==========================================
Question: {question}
==========================================
{format_instructions}
==========================================
Answer:
`;
export class MultiChainDecider {
private deciderOutputParser = StructuredOutputParser.fromZodSchema(
z.array(
z.object({
question: z.string().describe("the question itself"),
type: z.string().describe("Whether its aggregate or not-aggregate"),
}),
),
);
private decider = new LLMChain({
llm: new OpenAI({ modelName: "gpt-3.5-turbo-16k" }),
prompt: new PromptTemplate({
template: deciderPrompt,
inputVariables: ["question"],
partialVariables: { format_instructions: this.deciderOutputParser.getFormatInstructions() },
}),
});
public async query(question: string) {
this.deciderOutputParser.parseResult;
const res = await this.decider.call({ question });
const parsed = await this.deciderOutputParser.parse(res.text);
parsed.forEach(item => {
item.type === "aggregate"
? console.log(item.question, "use aggregator chain like neo4j")
: console.log(item.question, "use non-aggregator chain like QA");
});
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Everyone,
The sequential chain goes through all the provided chains as I can see conditionally you can have conditional prompt selection. However I am not clear on how to have conditional chains based on an original LLM chain output. e.g. based on the type of question asked, use a DocStuff or a graph db chain. Any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions