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  import java.io.Serializable;
19  
20  /**
21   * Represents a message in a chat conversation.
22   *
23   * @author FessProject
24   */
25  public class LlmMessage implements Serializable {
26  
27      private static final long serialVersionUID = 1L;
28  
29      /** The role identifier for system messages. */
30      public static final String ROLE_SYSTEM = "system";
31  
32      /** The role identifier for user messages. */
33      public static final String ROLE_USER = "user";
34  
35      /** The role identifier for assistant messages. */
36      public static final String ROLE_ASSISTANT = "assistant";
37  
38      /** The role of the message sender. */
39      private String role;
40  
41      /** The content of the message. */
42      private String content;
43  
44      /**
45       * Default constructor.
46       */
47      public LlmMessage() {
48      }
49  
50      /**
51       * Creates a new message with the specified role and content.
52       *
53       * @param role the message role
54       * @param content the message content
55       */
56      public LlmMessage(final String role, final String content) {
57          this.role = role;
58          this.content = content;
59      }
60  
61      /**
62       * Creates a system message with the specified content.
63       *
64       * @param content the message content
65       * @return a new system message
66       */
67      public static LlmMessage system(final String content) {
68          return new LlmMessage(ROLE_SYSTEM, content);
69      }
70  
71      /**
72       * Creates a user message with the specified content.
73       *
74       * @param content the message content
75       * @return a new user message
76       */
77      public static LlmMessage user(final String content) {
78          return new LlmMessage(ROLE_USER, content);
79      }
80  
81      /**
82       * Creates an assistant message with the specified content.
83       *
84       * @param content the message content
85       * @return a new assistant message
86       */
87      public static LlmMessage assistant(final String content) {
88          return new LlmMessage(ROLE_ASSISTANT, content);
89      }
90  
91      /**
92       * Gets the message role.
93       *
94       * @return the role
95       */
96      public String getRole() {
97          return role;
98      }
99  
100     /**
101      * Sets the message role.
102      *
103      * @param role the role
104      */
105     public void setRole(final String role) {
106         this.role = role;
107     }
108 
109     /**
110      * Gets the message content.
111      *
112      * @return the content
113      */
114     public String getContent() {
115         return content;
116     }
117 
118     /**
119      * Sets the message content.
120      *
121      * @param content the content
122      */
123     public void setContent(final String content) {
124         this.content = content;
125     }
126 
127     @Override
128     public String toString() {
129         return "LlmMessage{role='" + role + "', content='"
130                 + (content != null && content.length() > 50 ? content.substring(0, 50) + "..." : content) + "'}";
131     }
132 }