View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.llm;
17  
18  /**
19   * Represents the result of intent detection from user input.
20   * Contains the detected intent type, Fess query, and other metadata.
21   */
22  public class IntentDetectionResult {
23  
24      private final ChatIntent intent;
25      private final String query;
26      private final String documentUrl;
27      private final String reasoning;
28  
29      private IntentDetectionResult(final ChatIntent intent, final String query, final String documentUrl, final String reasoning) {
30          this.intent = intent;
31          this.query = query;
32          this.documentUrl = documentUrl;
33          this.reasoning = reasoning;
34      }
35  
36      /**
37       * Returns the detected intent type.
38       *
39       * @return the intent type
40       */
41      public ChatIntent getIntent() {
42          return intent;
43      }
44  
45      /**
46       * Returns the Fess query string for search.
47       *
48       * @return the Fess query string, or null if not available
49       */
50      public String getQuery() {
51          return query;
52      }
53  
54      /**
55       * Returns the document URL for summary intent.
56       *
57       * @return the document URL
58       */
59      public String getDocumentUrl() {
60          return documentUrl;
61      }
62  
63      /**
64       * Returns the reasoning for the detected intent.
65       *
66       * @return the reasoning
67       */
68      public String getReasoning() {
69          return reasoning;
70      }
71  
72      /**
73       * Creates a search intent result with a Fess query.
74       *
75       * @param query the Fess query string
76       * @param reasoning the detection reasoning
77       * @return the search intent result
78       */
79      public static IntentDetectionResult search(final String query, final String reasoning) {
80          return new IntentDetectionResult(ChatIntent.SEARCH, query, null, reasoning);
81      }
82  
83      /**
84       * Creates a summary intent result for a specific document.
85       *
86       * @param documentUrl the document URL to summarize
87       * @param reasoning the detection reasoning
88       * @return the summary intent result
89       */
90      public static IntentDetectionResult summary(final String documentUrl, final String reasoning) {
91          return new IntentDetectionResult(ChatIntent.SUMMARY, null, documentUrl, reasoning);
92      }
93  
94      /**
95       * Creates a FAQ intent result with a Fess query.
96       *
97       * @param query the Fess query string
98       * @param reasoning the detection reasoning
99       * @return the FAQ intent result
100      */
101     public static IntentDetectionResult faq(final String query, final String reasoning) {
102         return new IntentDetectionResult(ChatIntent.FAQ, query, null, reasoning);
103     }
104 
105     /**
106      * Creates an unclear intent result when intent cannot be determined.
107      *
108      * @param reasoning the detection reasoning
109      * @return the unclear intent result
110      */
111     public static IntentDetectionResult unclear(final String reasoning) {
112         return new IntentDetectionResult(ChatIntent.UNCLEAR, null, null, reasoning);
113     }
114 
115     /**
116      * Creates a fallback search intent when intent detection fails.
117      *
118      * @param originalMessage the original user message
119      * @return the fallback search intent result
120      */
121     public static IntentDetectionResult fallbackSearch(final String originalMessage) {
122         return new IntentDetectionResult(ChatIntent.SEARCH, originalMessage, null, "Fallback: using original message as query");
123     }
124 
125     @Override
126     public String toString() {
127         return "IntentDetectionResult{intent=" + intent + ", query=" + query + ", documentUrl=" + documentUrl + ", reasoning=" + reasoning
128                 + "}";
129     }
130 }