feat(AI工具): 添加本地AI执行工具
- 新增LocalWindow类,实现本地AI推理功能- 更新LM类,添加推理相关方法 - 在Main类中添加AI工具类别和本地AI执行工具项 - 优化FridaWindow类,添加作者注释
This commit is contained in:
24
.idea/inspectionProfiles/Project_Default.xml
generated
24
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -2,41 +2,65 @@
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliArrayNamingShouldHaveBracket" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliControlFlowStatementWithoutBraces" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliDeprecation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliEqualsAvoidNull" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliLongLiteralsEndingWithLowercaseL" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliMissingOverrideAnnotation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliWrapperTypeEquality" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAbstractClassShouldStartWithAbstractNaming" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidApacheBeanUtilsCopy" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidCallStaticSimpleDateFormat" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidCommentBehindStatement" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidComplexCondition" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidConcurrentCompetitionRandom" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidDoubleOrFloatEqualCompare" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidManuallyCreateThread" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidMissUseOfMathRandom" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidNegationOperator" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidNewDateGetTime" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidPatternCompileInMethod" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidReturnInFinally" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidStartWithDollarAndUnderLineNaming" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaAvoidUseTimer" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaBigDecimalAvoidDoubleConstructor" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaBooleanPropertyShouldNotStartWithIs" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaClassCastExceptionWithSubListToArrayList" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaClassCastExceptionWithToArray" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaClassMustHaveAuthor" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaClassNamingShouldBeCamel" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaCollectionInitShouldAssignCapacity" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaCommentsMustBeJavadocFormat" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaConcurrentExceptionWithModifyOriginSubList" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaConstantFieldShouldBeUpperCase" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaCountDownShouldInFinally" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaDontModifyInForeachCircle" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaEnumConstantsMustHaveComment" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaExceptionClassShouldEndWithException" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaIbatisMethodQueryForList" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaLockShouldWithTryFinally" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaLowerCamelCaseVariableNaming" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaMethodReturnWrapperType" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaMethodTooLong" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaPackageNaming" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaPojoMustOverrideToString" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaPojoMustUsePrimitiveField" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaPojoNoDefaultValue" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaRemoveCommentedCode" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaServiceOrDaoClassShouldEndWithImpl" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaSneakyThrowsWithoutExceptionType" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaStringConcat" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaSwitchExpression" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaSwitchStatement" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaTestClassShouldEndWithTestNaming" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaThreadLocalShouldRemove" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaThreadPoolCreation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaThreadShouldSetName" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaTransactionMustHaveRollback" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaUndefineMagicConstant" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaUnsupportedExceptionWithModifyAsList" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaUseQuietReferenceNotation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AlibabaUseRightCaseForDateFormat" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="MapOrSetKeyShouldOverrideHashCodeEquals" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.axis.innovators.box.events.GlobalEventBus;
|
||||
import com.axis.innovators.box.events.SettingsLoadEvents;
|
||||
import com.axis.innovators.box.events.SubscribeEvent;
|
||||
import com.axis.innovators.box.gui.FridaWindow;
|
||||
import com.axis.innovators.box.gui.LocalWindow;
|
||||
import com.axis.innovators.box.gui.MainWindow;
|
||||
import com.axis.innovators.box.tools.FolderCreator;
|
||||
import com.axis.innovators.box.tools.LibraryLoad;
|
||||
@@ -63,6 +64,24 @@ public class Main {
|
||||
// ....
|
||||
|
||||
ex.addToolCategory(debugCategory);
|
||||
|
||||
MainWindow.ToolCategory aICategory = new MainWindow.ToolCategory("AI工具",
|
||||
"ai/ai.png",
|
||||
"人工智能/大语言模型");
|
||||
|
||||
aICategory.addTool(new MainWindow.ToolItem("本地AI执行工具", "ai/local/local_main.png",
|
||||
"在本机对开源大语言模型进行推理" +
|
||||
"\n作者:tzdwindows 7", ++id, new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Window owner = SwingUtilities.windowForComponent((Component) e.getSource());
|
||||
LocalWindow dialog = new LocalWindow(owner);
|
||||
dialog.setVisible(true);
|
||||
}
|
||||
}));
|
||||
|
||||
ex.addToolCategory(aICategory);
|
||||
|
||||
ex.initUI();
|
||||
ex.setVisible(true);
|
||||
});
|
||||
|
||||
@@ -17,6 +17,10 @@ import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Frida注入工具窗口
|
||||
* @author tzdwindows 7
|
||||
*/
|
||||
public class FridaWindow extends JDialog {
|
||||
private JTextArea scriptArea;
|
||||
private JTextArea logArea;
|
||||
|
||||
263
src/main/java/com/axis/innovators/box/gui/LocalWindow.java
Normal file
263
src/main/java/com/axis/innovators/box/gui/LocalWindow.java
Normal file
@@ -0,0 +1,263 @@
|
||||
package com.axis.innovators.box.gui;
|
||||
|
||||
import org.tzd.lm.LM;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 本地AI执行工具
|
||||
* @author tzdwindows 7
|
||||
*/
|
||||
public class LocalWindow extends JDialog {
|
||||
private final Color bgColor = new Color(45, 45, 48);
|
||||
private final Color textColor = new Color(240, 240, 240);
|
||||
private final Font mainFont = new Font("微软雅黑", Font.PLAIN, 14);
|
||||
private final Color bgColor1 = new Color(235, 241, 250);
|
||||
private final Color bgColor2 = new Color(255, 255, 255);
|
||||
private final Color userTextColor = new Color(0, 100, 200);
|
||||
private final Color aiTextColor = new Color(0, 150, 0); // AI消息绿色
|
||||
|
||||
private JTextArea chatArea;
|
||||
private JTextField inputField;
|
||||
private JButton sendButton;
|
||||
private JProgressBar progressBar;
|
||||
private JComboBox<String> contextBox;
|
||||
private final LinkedList<Long> contextHandles = new LinkedList<>();
|
||||
private long currentModelHandle = -1;
|
||||
|
||||
private JSpinner tempSpinner;
|
||||
|
||||
public LocalWindow(Window owner) {
|
||||
super(owner, "本地AI执行工具", ModalityType.APPLICATION_MODAL);
|
||||
initializeUI();
|
||||
loadModelAsync();
|
||||
}
|
||||
|
||||
private void initializeUI() {
|
||||
// 主面板设置
|
||||
JPanel mainPanel = new JPanel(new BorderLayout()) {
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
GradientPaint gp = new GradientPaint(0, 0, bgColor1, getWidth(), getHeight(), bgColor2);
|
||||
g2d.setPaint(gp);
|
||||
g2d.fillRect(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
};
|
||||
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
||||
|
||||
// 聊天区域
|
||||
chatArea = new JTextArea();
|
||||
chatArea.setEditable(false);
|
||||
chatArea.setFont(mainFont);
|
||||
chatArea.setForeground(textColor);
|
||||
chatArea.setOpaque(false);
|
||||
chatArea.setLineWrap(true);
|
||||
chatArea.setWrapStyleWord(true);
|
||||
|
||||
chatArea.setBackground(Color.lightGray);
|
||||
chatArea.setForeground(Color.black);
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane(chatArea);
|
||||
scrollPane.setOpaque(false);
|
||||
scrollPane.getViewport().setOpaque(false);
|
||||
scrollPane.setBorder(BorderFactory.createEmptyBorder());
|
||||
|
||||
JToolBar toolBar = createToolBar();
|
||||
JPanel inputPanel = createInputPanel();
|
||||
|
||||
progressBar = new JProgressBar();
|
||||
progressBar.setIndeterminate(true);
|
||||
progressBar.setVisible(false);
|
||||
|
||||
mainPanel.add(toolBar, BorderLayout.NORTH);
|
||||
mainPanel.add(scrollPane, BorderLayout.CENTER);
|
||||
mainPanel.add(inputPanel, BorderLayout.SOUTH);
|
||||
|
||||
mainPanel.add(scrollPane, BorderLayout.CENTER);
|
||||
this.add(mainPanel);
|
||||
this.setSize(800, 600);
|
||||
this.setLocationRelativeTo(getOwner());
|
||||
}
|
||||
|
||||
private JToolBar createToolBar() {
|
||||
JToolBar toolBar = new JToolBar();
|
||||
toolBar.setFloatable(false);
|
||||
toolBar.setBackground(bgColor);
|
||||
|
||||
// 新对话按钮
|
||||
JButton newBtn = new JButton("新对话");
|
||||
styleButton(newBtn);
|
||||
newBtn.addActionListener(e -> createNewContext());
|
||||
|
||||
// 保存记录按钮
|
||||
JButton saveBtn = new JButton("保存记录");
|
||||
styleButton(saveBtn);
|
||||
saveBtn.addActionListener(e -> saveConversation());
|
||||
|
||||
// CUDA切换
|
||||
JCheckBox cudaCheck = new JCheckBox("启用CUDA", LM.CUDA);
|
||||
cudaCheck.setFont(mainFont);
|
||||
cudaCheck.setBackground(Color.lightGray);
|
||||
cudaCheck.setForeground(Color.black);
|
||||
cudaCheck.addActionListener(e -> LM.CUDA = cudaCheck.isSelected());
|
||||
|
||||
// 上下文选择
|
||||
contextBox = new JComboBox<>();
|
||||
contextBox.setBackground(bgColor);
|
||||
contextBox.setForeground(textColor);
|
||||
|
||||
toolBar.add(newBtn);
|
||||
toolBar.add(saveBtn);
|
||||
toolBar.addSeparator();
|
||||
toolBar.add(cudaCheck);
|
||||
toolBar.addSeparator();
|
||||
|
||||
toolBar.add(contextBox);
|
||||
|
||||
return toolBar;
|
||||
}
|
||||
|
||||
private JPanel createInputPanel() {
|
||||
JPanel panel = new JPanel(new BorderLayout());
|
||||
panel.setBackground(bgColor);
|
||||
|
||||
inputField = new JTextField();
|
||||
inputField.setFont(mainFont);
|
||||
inputField.setForeground(Color.black);
|
||||
inputField.setBackground(Color.white);
|
||||
|
||||
sendButton = new JButton("发送");
|
||||
styleButton(sendButton);
|
||||
sendButton.addActionListener(this::handleSendMessage);
|
||||
|
||||
// 温度调节
|
||||
tempSpinner = new JSpinner(new SpinnerNumberModel(0.8, 0.0, 1.0, 0.1));
|
||||
tempSpinner.setFont(mainFont);
|
||||
tempSpinner.setBackground(bgColor);
|
||||
tempSpinner.setForeground(textColor);
|
||||
tempSpinner.setPreferredSize(new Dimension(80, tempSpinner.getPreferredSize().height));
|
||||
|
||||
JPanel rightPanel = new JPanel(new BorderLayout());
|
||||
rightPanel.add(tempSpinner, BorderLayout.WEST);
|
||||
rightPanel.add(sendButton, BorderLayout.EAST);
|
||||
|
||||
panel.add(inputField, BorderLayout.CENTER);
|
||||
panel.add(rightPanel, BorderLayout.EAST);
|
||||
return panel;
|
||||
}
|
||||
|
||||
private void styleButton(JButton button) {
|
||||
button.setFont(mainFont);
|
||||
button.setBackground(Color.lightGray);
|
||||
button.setForeground(Color.black);
|
||||
button.setFocusPainted(false);
|
||||
button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
|
||||
}
|
||||
|
||||
private void loadModelAsync() {
|
||||
progressBar.setVisible(true);
|
||||
new SwingWorker<Long, Void>() {
|
||||
@Override
|
||||
protected Long doInBackground() {
|
||||
return LM.llamaLoadModelFromFile(LM.DEEP_SEEK);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
try {
|
||||
currentModelHandle = get();
|
||||
createNewContext();
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(LocalWindow.this, "模型加载失败");
|
||||
}
|
||||
progressBar.setVisible(false);
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
private void createNewContext() {
|
||||
if (currentModelHandle != -1) {
|
||||
long newCtx = LM.createContext(currentModelHandle);
|
||||
contextHandles.add(newCtx);
|
||||
contextBox.addItem("上下文 " + contextHandles.size());
|
||||
contextBox.setSelectedIndex(contextHandles.size()-1);
|
||||
contextBox.setBackground(Color.lightGray);
|
||||
contextBox.setForeground(Color.black);
|
||||
chatArea.setText(""); // 清除聊天区域内容
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSendMessage(ActionEvent e) {
|
||||
String prompt = inputField.getText();
|
||||
if (!prompt.isEmpty()) {
|
||||
appendMessage(prompt);
|
||||
inputField.setText("");
|
||||
new SwingWorker<String, String>() {
|
||||
@Override
|
||||
protected String doInBackground() {
|
||||
float temperature = ((Number) tempSpinner.getValue()).floatValue();
|
||||
return LM.inference(
|
||||
currentModelHandle,
|
||||
contextHandles.getLast(),
|
||||
temperature,
|
||||
prompt,
|
||||
this::publish
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(java.util.List<String> chunks) {
|
||||
chunks.forEach(msg -> appendAIMessage(msg));
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
|
||||
static boolean isTemporary = true;
|
||||
private final List<String> messages = new ArrayList<>();
|
||||
private void appendAIMessage(String message) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
if (isTemporary) {
|
||||
chatArea.append("【AI】\n");
|
||||
isTemporary = false;
|
||||
}
|
||||
// 处理Markdown语法
|
||||
String message2 = message.replaceAll("\\*\\*(.*?)\\*\\*", "<b>$1</b>")
|
||||
.replaceAll("##(.*?)\\n", "<h2>$1</h2>")
|
||||
.replaceAll("</think>", "</think></fold>");
|
||||
chatArea.append(message2);
|
||||
chatArea.setCaretPosition(chatArea.getDocument().getLength());
|
||||
messages.add(message2);
|
||||
});
|
||||
}
|
||||
|
||||
private void appendMessage(String message) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
chatArea.append("\n【用户】\n" + message + "\n\n");
|
||||
chatArea.setCaretPosition(chatArea.getDocument().getLength());
|
||||
});
|
||||
}
|
||||
|
||||
private void saveConversation() {
|
||||
String content = chatArea.getText();
|
||||
// TODO: 调用保存实现
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
contextHandles.forEach(LM::llamaFreeContext);
|
||||
if (currentModelHandle != -1) {
|
||||
LM.llamaFreeModel(currentModelHandle);
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import com.axis.innovators.box.tools.LibraryLoad;
|
||||
* @author tzdwindows 7
|
||||
*/
|
||||
public class LM {
|
||||
public static boolean CUDA = false;
|
||||
public final static String DEEP_SEEK = FolderCreator.getModelFolder() + "//DeepSeek-R1-Distill-Qwen-1.5B-Q8_0.gguf";
|
||||
public static boolean CUDA = true;
|
||||
public final static String DEEP_SEEK = "G:/deepseek/deepseek/DeepSeek-R1-Distill-Qwen-1.5B-Q8_0/DeepSeek-R1-Distill-Qwen-1.5B-Q8_0.gguf";
|
||||
|
||||
static {
|
||||
if (!CUDA) {
|
||||
@@ -41,12 +41,51 @@ public class LM {
|
||||
*/
|
||||
public static native void llamaFreeModel(long modelHandle);
|
||||
|
||||
public static long createContext(long modelHandle){
|
||||
return createContext(modelHandle,
|
||||
4096,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文创建
|
||||
* @param modelHandle 上下文句柄
|
||||
* @return 上下文句柄
|
||||
*
|
||||
* 创建一个新的上下文句柄,可以通过该句柄与模型进行交互。
|
||||
*
|
||||
* @param modelHandle 上下文句柄,表示已加载的模型实例的引用。
|
||||
* @param nCtx 上下文的大小,决定了处理的最大上下文量。
|
||||
* @param nBatch 批量大小,定义了每次处理的最大样本数。
|
||||
* @param nSeqMax 最大序列数,对于递归模型有用。
|
||||
* @param nThreads 用于生成的线程数。
|
||||
* @param nThreadsBatch 用于批处理的线程数。
|
||||
* @param logitsAll 是否计算所有 logits,而不仅仅是最后一个(弃用,改用 `llama_batch.logits`)。
|
||||
* @param embeddings 是否提取嵌入(同时提取 logits)。
|
||||
* @param offloadKqv 是否将 KQV 操作(包括 KV 缓存)卸载到 GPU 上。
|
||||
* @param flashAttn 是否启用闪存注意力(实验性功能)。
|
||||
* @param noPerf 是否禁用性能计时。
|
||||
* @return 返回创建的上下文句柄,如果创建失败,返回 0。
|
||||
*/
|
||||
public static native long createContext(long modelHandle);
|
||||
public static native long createContext(long modelHandle,
|
||||
int nCtx,
|
||||
int nBatch,
|
||||
int nSeqMax,
|
||||
int nThreads,
|
||||
int nThreadsBatch,
|
||||
boolean logitsAll,
|
||||
boolean embeddings,
|
||||
boolean offloadKqv,
|
||||
boolean flashAttn,
|
||||
boolean noPerf
|
||||
);
|
||||
|
||||
/**
|
||||
* 释放上下文资源
|
||||
@@ -54,18 +93,57 @@ public class LM {
|
||||
*/
|
||||
public static native void llamaFreeContext(long ctxHandle);
|
||||
|
||||
public static String inference(long modelHandle ,
|
||||
long ctxHandle,
|
||||
float temperature,
|
||||
String prompt,
|
||||
MessageCallback messageCallback){
|
||||
return inference(modelHandle,
|
||||
ctxHandle,
|
||||
temperature,
|
||||
0.05f,
|
||||
40,
|
||||
0.95f,
|
||||
0,
|
||||
5,
|
||||
2.0f,
|
||||
0,
|
||||
0,
|
||||
prompt,
|
||||
messageCallback
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 推理模型
|
||||
* @param modelHandle 模型句柄
|
||||
* @param ctxHandle 模型上下文句柄
|
||||
* @param temperature 温度
|
||||
* @param prompt 问题
|
||||
* @param messageCallback 回调接口
|
||||
* @return 最终内容
|
||||
* 推理方法,根据传入的参数进行推理并返回生成的结果。
|
||||
*
|
||||
* @param modelHandle 模型句柄,指向加载的推理模型实例。
|
||||
* @param ctxHandle 上下文句柄,指向推理上下文,用于维持推理状态。
|
||||
* @param temperature 控制生成的文本的多样性,较高的值(如0.8)生成更加多样化的文本,较低的值(如0.2)生成更加确定的文本。
|
||||
* @param minP 最小概率值,控制生成过程中所接受的最小概率的词汇,避免出现概率极低的词汇。
|
||||
* @param topK 控制每次采样时从候选词中选取的最大数量,较小的值会限制生成的多样性。
|
||||
* @param topP 控制每次采样时从累计概率最小的词汇池中选取的候选词比例,0到1之间的值。
|
||||
* @param dist 种子值,决定生成文本的随机性,通常设置为0或者使用某个固定值以获得确定性的结果。
|
||||
* @param penaltyLastN 控制对生成的最后n个词进行惩罚,0表示不惩罚,-1表示禁用惩罚。
|
||||
* @param penaltyRepeat 重复惩罚系数,避免生成重复的词汇,1.0表示禁用惩罚。
|
||||
* @param penaltyFreq 频率惩罚系数,避免过度重复高频词汇,0.0表示禁用。
|
||||
* @param penaltyPresent 当前词汇惩罚系数,避免过度使用相同词汇。
|
||||
* @param prompt 输入的提示文本,用于引导模型生成相关内容。
|
||||
* @param messageCallback 回调函数,当模型生成消息时调用该函数,传递生成的内容。
|
||||
*
|
||||
* @return 返回生成的文本结果,类型为字符串。若推理过程中出现错误,返回null。
|
||||
*/
|
||||
public static native String inference(long modelHandle ,
|
||||
long ctxHandle,
|
||||
float temperature,
|
||||
float minP,
|
||||
float topK,
|
||||
float topP,
|
||||
float dist,
|
||||
int penaltyLastN, // 最后n个要惩罚的标记(0 = disablepenalty-1 =上下文大小)
|
||||
float penaltyRepeat, // 1.0 =禁用
|
||||
float penaltyFreq, // 0.0 =禁用
|
||||
float penaltyPresent,
|
||||
String prompt,
|
||||
MessageCallback messageCallback);
|
||||
|
||||
@@ -85,7 +163,7 @@ public class LM {
|
||||
long modelHandle = llamaLoadModelFromFile(DEEP_SEEK);
|
||||
// 创建新的上下文
|
||||
long ctxHandle = createContext(modelHandle);
|
||||
inference(modelHandle, ctxHandle, 0.2f, "写一个ai", new MessageCallback() {
|
||||
inference(modelHandle, ctxHandle, 0.2f, "Tell me how gpt works in an easy way, in Chinese", new MessageCallback() {
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
// 回调输出
|
||||
@@ -93,13 +171,13 @@ public class LM {
|
||||
}
|
||||
});
|
||||
// 推理模型
|
||||
inference(modelHandle, ctxHandle, 0.2f, "谢谢你", new MessageCallback() {
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
// 回调输出
|
||||
System.out.print(message);
|
||||
}
|
||||
});
|
||||
//inference(modelHandle, ctxHandle, 0.2f, "谢谢你", new MessageCallback() {
|
||||
// @Override
|
||||
// public void onMessage(String message) {
|
||||
// // 回调输出
|
||||
// System.out.print(message);
|
||||
// }
|
||||
//});
|
||||
// 清理上下文
|
||||
llamaFreeContext(ctxHandle);
|
||||
}
|
||||
|
||||
BIN
src/main/resources/icons/ai/ai.png
Normal file
BIN
src/main/resources/icons/ai/ai.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
BIN
src/main/resources/icons/ai/local/local_main.png
Normal file
BIN
src/main/resources/icons/ai/local/local_main.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
Reference in New Issue
Block a user