Java文档阅读笔记-AI LangChain4j - Intent Classifier with Entity Extraction
·
官方
使用Ollama模型如:phi3:mini-128k,并且请求一个严格的JSON响应,其中包含检测到的意图和实体。然后Java程序打印出原始模型输出。
代码如下:
package com.logicbig.example;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.ollama.OllamaChatModel;
public class IntentClassifierExample {
public static void main(String[] args) {
ChatModel model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.modelName("phi3:mini-128k")
.numCtx(4096)
.temperature(0.5)
.build();
SystemMessage systemInstruction = SystemMessage.from(
"Identify the intent and extract entities from the user's request.\n"
+ "Intents: [CHECK_ORDER, CANCEL_ORDER, REFUND_REQUEST, UNKNOWN]\n"
+ "Entities to find: [order_id, item_name, reason]\n"
+ "Return the result ONLY as a JSON object.");
UserMessage userMessage = UserMessage.from(
"I need a refund for the fries in my order #9921 because they are soggy");
ChatResponse response = model.chat(systemInstruction, userMessage);
AiMessage aiMessage = response.aiMessage();
System.out.println(aiMessage.text());
}
}
输出:
```json
{
"Intent": "REFUND_REQUEST",
"Entities": {
"order_id": "9921",
"item_name": "fries",
"reason": "soggy"
}
}
```
该示例产生的输出证实,该模型能够正确推断用户的意图(例如退款请求),并提取相关实体,如订单编号、商品名称和原因。这验证了一个单一的、范围明确的提示足以用于意图分类和实体提取。

测试
IntentClassifierExample.java
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.ollama.OllamaChatModel;
public class IntentClassifierExample {
public static void main(String[] args) {
ChatModel model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.modelName("phi3:mini-128k")
.numCtx(4096)
.temperature(0.5)
.build();
SystemMessage systemInstruction = SystemMessage.from(
"Identify the intent and extract entities from the user's request.\n"
+ "Intents: [CHECK_ORDER, CANCEL_ORDER, REFUND_REQUEST, UNKNOWN]\n"
+ "Entities to find: [order_id, item_name, reason]\n"
+ "Return the result ONLY as a JSON object.");
UserMessage userMessage = UserMessage.from(
"I need a refund for the fries in my order #9921 because they are soggy");
ChatResponse response = model.chat(systemInstruction, userMessage);
AiMessage aiMessage = response.aiMessage();
System.out.println(aiMessage.text());
}
}
程序运行截图如下:

修改下输入:
UserMessage userMessage = UserMessage.from(
"不想要了,取消订单");
输出如下:
```json
{
"intent": "CANCEL_ORDER",
"entities": {
"order_id": null,
"item_name": null,
"reason": null
}
}
```
指示的質量大幅度上升,要求在這次產生的JSON中包括更多元素和更褒辭的語言。此外,我需要您根據提供的信息產生出一併具有賜義意義的結果。
Intents: [CREATE_ORDER, MODIFY_ORDER, DELIVER_FEEDBACK]
Entities to find: [customer_name, product_id, quantity, delivery_address, preferred_delivery_date, feedback_reason]
Return the result ONLY as a JSON object. Additionally, include an emotional tone and anticipate customer satisfaction in your response designation.
Process finished with exit code 0
更多推荐

所有评论(0)