我有一个带有令牌限制的LLM聊天模型。 我正在尝试将示例用户消息和预期的 AI 消息响应传递给 LLM,以训练它如何根据从文档中提取的文本提供响应。 我正在使用 System Loader 加载文档
Document document = loadDocument(toPath("file:///filepath\\filename.pdf"));
我正在使用正则表达式拆分器来帮助法学硕士理解模式
DocumentByRegexSplitter splitter=new DocumentByRegexSplitter(regex,joiner,maxCharLimit,maxOverlap,subSplitter);
嵌入文档(内存中嵌入存储并获取相关向量)后,我将其加入到一个信息字符串中,我可以将其输入到提示模板中以生成用户消息
PromptTemplate promptTemplate = PromptTemplate.from(
"Answer the following question to the best of your ability"
+ "Question:\n"
+ "{{question}}\n"
+ "\n"
+ "Base your answer on the following information:\n"
+ "{{information}}");
String information = relevantEmbeddings.stream()
.map(match -> match.embedded().text())
.collect(joining("\n\n"));
Map<String, Object> variables = new HashMap<>();
variables.put("question", trainingQuestion);
variables.put("information", information);
Prompt prompt = promptTemplate.apply(variables);
List<ChatMessage> chatMessages=new ArrayList<>();
chatMessages.add(prompt .toUserMessage());
chatMessages.add(new AiMessage("Expected Response"));
variables.put("question", actualQuestion);
variables.put("information", information);
prompt = promptTemplate.apply(variables);
chatMessages.add(prompt .toUserMessage());
我将按照 Java Langchain 框架的要求将训练消息添加到列表中
AiMessage response=chatModel.generate(chatMessages);
长话短说,由于为所有 Few Shot 消息嵌入相同的文档信息,我面临着令牌约束。 有没有办法让LLM使用相同的文档作为Few-Shot训练和实际查询的参考,这样我就可以避免多次消耗文档的令牌?
我收到同事的建议,将文档添加到 SystemMessage,这样就不会在培训和实际用户消息中多次传递。 将尝试这个并更新