001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.gnu.org/licenses/lgpl-3.0.txt
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package dev.tinyflow.core.llm;
017
018import dev.tinyflow.core.chain.Chain;
019import dev.tinyflow.core.node.LlmNode;
020
021import java.io.Serializable;
022import java.util.List;
023
024/**
025 * Llm接口定义了大语言模型的基本功能规范
026 * <p>
027 * 该接口提供了与大型语言模型交互的标准方法,
028 * 包括文本生成、对话处理等核心功能
029 */
030public interface Llm {
031
032
033    /**
034     * 执行聊天对话操作
035     *
036     * @param messageInfo 消息信息对象,包含用户输入的原始消息内容及相关元数据
037     * @param options     聊天配置选项,用于控制对话行为和模型参数
038     * @param llmNode     大语言模型节点,指定使用的具体语言模型实例
039     * @param chain       对话链对象,管理对话历史和上下文状态
040     * @return 返回模型生成的回复字符串
041     */
042    String chat(MessageInfo messageInfo, ChatOptions options, LlmNode llmNode, Chain chain);
043
044
045    /**
046     * 消息信息类,用于封装消息相关的信息
047     */
048    class MessageInfo implements Serializable {
049        private static final long serialVersionUID = 1L;
050        private String message;
051        private String systemMessage;
052        private List<String> images;
053
054        public String getMessage() {
055            return message;
056        }
057
058        public void setMessage(String message) {
059            this.message = message;
060        }
061
062        public String getSystemMessage() {
063            return systemMessage;
064        }
065
066        public void setSystemMessage(String systemMessage) {
067            this.systemMessage = systemMessage;
068        }
069
070        public List<String> getImages() {
071            return images;
072        }
073
074        public void setImages(List<String> images) {
075            this.images = images;
076        }
077    }
078
079    /**
080     * ChatOptions 类用于存储聊天相关的配置选项
081     * 该类实现了Serializable接口,支持序列化操作
082     */
083    /**
084     * ChatOptions类用于配置聊天模型的参数选项
085     * 实现了Serializable接口,支持序列化
086     */
087    class ChatOptions implements Serializable {
088
089        private String seed;
090        private Float temperature = 0.8f;
091        private Float topP;
092        private Integer topK;
093        private Integer maxTokens;
094        private List<String> stop;
095
096        /**
097         * 获取随机种子值
098         *
099         * @return 返回随机种子字符串,用于控制生成结果的随机性
100         */
101        public String getSeed() {
102            return seed;
103        }
104
105        /**
106         * 设置随机种子值
107         *
108         * @param seed 随机种子字符串,用于控制生成结果的随机性
109         */
110        public void setSeed(String seed) {
111            this.seed = seed;
112        }
113
114        /**
115         * 获取温度参数
116         *
117         * @return 返回温度值,控制生成文本的随机性,值越高越随机
118         */
119        public Float getTemperature() {
120            return temperature;
121        }
122
123        /**
124         * 设置温度参数
125         *
126         * @param temperature 温度值,控制生成文本的随机性,值越高越随机
127         */
128        public void setTemperature(Float temperature) {
129            this.temperature = temperature;
130        }
131
132        /**
133         * 获取Top-P参数
134         *
135         * @return 返回Top-P值,用于 nucleus sampling,控制生成词汇的概率阈值
136         */
137        public Float getTopP() {
138            return topP;
139        }
140
141        /**
142         * 设置Top-P参数
143         *
144         * @param topP Top-P值,用于 nucleus sampling,控制生成词汇的概率阈值
145         */
146        public void setTopP(Float topP) {
147            this.topP = topP;
148        }
149
150        /**
151         * 获取Top-K参数
152         *
153         * @return 返回Top-K值,限制每步选择词汇的范围
154         */
155        public Integer getTopK() {
156            return topK;
157        }
158
159        /**
160         * 设置Top-K参数
161         *
162         * @param topK Top-K值,限制每步选择词汇的范围
163         */
164        public void setTopK(Integer topK) {
165            this.topK = topK;
166        }
167
168        /**
169         * 获取最大令牌数
170         *
171         * @return 返回最大令牌数,限制生成文本的最大长度
172         */
173        public Integer getMaxTokens() {
174            return maxTokens;
175        }
176
177        /**
178         * 设置最大令牌数
179         *
180         * @param maxTokens 最大令牌数,限制生成文本的最大长度
181         */
182        public void setMaxTokens(Integer maxTokens) {
183            this.maxTokens = maxTokens;
184        }
185
186        /**
187         * 获取停止词列表
188         *
189         * @return 返回停止词字符串列表,当生成文本遇到这些词时会停止生成
190         */
191        public List<String> getStop() {
192            return stop;
193        }
194
195        /**
196         * 设置停止词列表
197         *
198         * @param stop 停止词字符串列表,当生成文本遇到这些词时会停止生成
199         */
200        public void setStop(List<String> stop) {
201            this.stop = stop;
202        }
203    }
204
205
206}