Intent classification (check out our tutorial) is the process of using AI to categorize user input into predefined labels, allowing systems to understand the goal behind a message. This technique is fundamental for building intelligent routers that direct user queries to the appropriate business logic or service.
Example
In this example, we will use a local LLM model running via Ollama Platform. We chose to run phi3:mini-128k model because it's fast to run on local machines and uses less RAM. Our example will show how to classify a user's inquiry. The LLM model is prompted to return only the specific enum name, which is then handled by a switch statement to simulate business routing.
package com.logicbig.example;
public enum Intent {
GREETING,
CHECK_ORDER,
CANCEL_ORDER,
REFUND_REQUEST,
UNKNOWN
}
package com.logicbig.example;
import dev.langchain4j.model.ollama.OllamaChatModel;
public class IntentRecognitionExample {
public static void main(String[] args) {
OllamaChatModel model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.modelName("phi3:mini-128k")
.numCtx(4096)
.temperature(0.0)
.build();
String userMessage = "Where is my order?";
String prompt = """
You are an intent classification system.
Classify the user message into ONE of the following intents:
- GREETING
- CHECK_ORDER
- CANCEL_ORDER
- REFUND_REQUEST
- UNKNOWN
Return ONLY the intent name. No explanation.
User message:
"%s"
""".formatted(userMessage);
String response = model.chat(prompt).trim();
System.out.println("Detected intent: " + response);
try {
Intent intent = Intent.valueOf(response.toUpperCase());
switch (intent) {
case CHECK_ORDER:
System.out.println("start process: looking up order status...");
break;
case GREETING:
System.out.println("start process: responding to greeting...");
break;
case CANCEL_ORDER:
System.out.println("start process: responding to Cancel order...");
break;
case REFUND_REQUEST:
System.out.println("start process: responding to refund request...");
break;
case UNKNOWN:
default:
System.out.println("start process: generic help flow...");
}
} catch (IllegalArgumentException e) {
System.out.println("Process: handle unknown intent");
}
}
}
Output$ mvn exec:java -Dexec.mainClass="com.logicbig.example.IntentRecognitionExample" [INFO] Scanning for projects... [INFO] [INFO] ----< com.logicbig.example:intent-classifier-lang-chain-4j-example >---- [INFO] Building intent-classifier-lang-chain-4j-example 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- exec:3.6.3:java (default-cli) @ intent-classifier-lang-chain-4j-example --- Detected intent: CHECK_ORDER start process: looking up order status... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.431 s [INFO] Finished at: 2026-01-14T05:28:01+08:00 [INFO] ------------------------------------------------------------------------
Conclusion
The output confirms that the LLM correctly identified the user's message 'Where is my order?' as the CHECK_ORDER intent. By constraining the model's output to specific enum values, we successfully transformed unstructured natural language into a structured programmatic flow, as evidenced by the successful execution of the corresponding switch case.
Example ProjectDependencies and Technologies Used: - langchain4j 1.10.0 (Build LLM-powered applications in Java: chatbots, agents, RAG, and much more)
- langchain4j-ollama 1.10.0 (LangChain4j :: Integration :: Ollama)
- JDK 17
- Maven 3.9.11
|