
LANGCHAIN — Can If Statements be Enhanced with Prompt Classification Using Ollama and Langchain?
The art of programming is the art of organizing complexity, of mastering multitude and avoiding its bastard chaos as effectively as possible. — Edsger W. Dijkstra
If statements are a fundamental part of programming, allowing developers to create conditional logic in their code. However, by integrating prompt classification using Ollama and Langchain, if statements can be enhanced to make more sophisticated decisions based on language understanding. In this tutorial, we’ll explore how to supercharge if-statements with prompt classification using Ollama and Langchain.
Reintroducing Lumos! 🪄
In our previous work, we have introduced Lumos, an LLM co-pilot for browsing the web, powered by local LLMs. Lumos is a Chrome extension that processes parsed results in an online, in-memory RAG workflow, all in a single request context. It’s built on LangChain and powered by Ollama.
Prompt Classification with Ollama 🦙
We can enhance if statements by using Ollama to determine if a prompt requires a specific tool or action. By asking the LLM if the prompt was a math equation with numbers and operators and checking if the response contained a “yes” or “no,” we can make more informed decisions in our code.
const isArithmeticExpression = async (
baseURL: string,
model: string,
prompt: string,
): Promise<boolean> => {
if (prompt.trim().toLowerCase().startsWith("calculate:")) {
return new Promise((resolve) => resolve(true));
}
const ollama = new Ollama({ baseUrl: baseURL, model: model, temperature: 0, stop: [".", ","]});
const question = `Is the following prompt a math equation with numbers and operators? Answer with 'yes' or 'no'.\n\nPrompt: ${prompt}`;
return ollama.invoke(question).then((response) => {
const answer = response.trim().split(" ")[0].toLowerCase();
return answer.includes("yes");
});
};By leveraging local LLMs, this operation is effectively free, and the utility of Ollama truly shines for this use case.
Scaling Classification For Complex Conditionals 🌲
We can replicate the classification technique to make complex decisions based on different prompts. By creating a configurable function, we can reuse the classification process throughout the application code.
const classifyPrompt = async (
baseURL: string,
model: string,
type: string,
originalPrompt: string,
classifcationPrompt: string,
prefixTrigger?: string,
): Promise<boolean> => {
if (prefixTrigger) {
if (originalPrompt.trim().toLowerCase().startsWith(prefixTrigger)) {
return new Promise((resolve) => resolve(true));
}
}
const ollama = new Ollama({
baseUrl: baseURL,
model: model,
temperature: 0,
stop: [".", ","],
});
const finalPrompt = `${classifcationPrompt} Answer with 'yes' or 'no'.\n\nPrompt: ${originalPrompt}`;
return ollama.invoke(finalPrompt).then((response) => {
const answer = response.trim().split(" ")[0].toLowerCase();
return answer.includes("yes");
});
};By combining the classification result with other application states, we can make effective and testable decisions in our code.
Conclusion
By leveraging Ollama and Langchain, we can enhance if statements with prompt classification, enabling our code to make more sophisticated decisions based on language understanding. This approach can be particularly useful for making decisions based on LLM responses, such as executing specific tools or actions. With these techniques, developers can have full control over the execution of an application and make complex decisions based on a combination of classification results and application states.





