初始化
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package com.easyagents.llm.openai;
|
||||
|
||||
import com.easyagents.core.model.chat.ChatModel;
|
||||
import com.easyagents.core.model.chat.ChatOptions;
|
||||
import com.easyagents.core.model.chat.StreamResponseListener;
|
||||
import com.easyagents.core.model.chat.response.AiMessageResponse;
|
||||
import com.easyagents.core.model.client.StreamContext;
|
||||
import com.easyagents.core.prompt.Prompt;
|
||||
import com.easyagents.core.prompt.SimplePrompt;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ChatModelTestUtils {
|
||||
|
||||
public static void waitForStream(
|
||||
ChatModel model,
|
||||
String prompt,
|
||||
StreamResponseListener listener) {
|
||||
waitForStream(model, new SimplePrompt(prompt), listener, Integer.MAX_VALUE, null);
|
||||
}
|
||||
|
||||
public static void waitForStream(
|
||||
ChatModel model,
|
||||
String prompt,
|
||||
StreamResponseListener listener,
|
||||
ChatOptions options) {
|
||||
waitForStream(model, new SimplePrompt(prompt), listener, Integer.MAX_VALUE, options);
|
||||
}
|
||||
|
||||
public static void waitForStream(
|
||||
ChatModel model,
|
||||
Prompt prompt,
|
||||
StreamResponseListener listener) {
|
||||
waitForStream(model, prompt, listener, Integer.MAX_VALUE, null);
|
||||
}
|
||||
|
||||
public static void waitForStream(
|
||||
ChatModel model,
|
||||
Prompt prompt,
|
||||
StreamResponseListener listener,
|
||||
ChatOptions options) {
|
||||
waitForStream(model, prompt, listener, Integer.MAX_VALUE, options);
|
||||
}
|
||||
|
||||
public static void waitForStream(
|
||||
ChatModel model,
|
||||
Prompt prompt,
|
||||
StreamResponseListener listener,
|
||||
long timeoutSeconds, ChatOptions options) {
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
StreamResponseListener wrapped = new StreamResponseListener() {
|
||||
@Override
|
||||
public void onStart(StreamContext context) {
|
||||
listener.onStart(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(StreamContext ctx, AiMessageResponse resp) {
|
||||
listener.onMessage(ctx, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(StreamContext ctx) {
|
||||
listener.onStop(ctx);
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(StreamContext context, Throwable throwable) {
|
||||
listener.onFailure(context, throwable);
|
||||
}
|
||||
};
|
||||
|
||||
model.chatStream(prompt, wrapped, options);
|
||||
try {
|
||||
if (!latch.await(timeoutSeconds, TimeUnit.SECONDS)) {
|
||||
throw new RuntimeException("Stream did not complete within " + timeoutSeconds + "s");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.easyagents.llm.openai;
|
||||
|
||||
import com.easyagents.core.model.chat.ChatModel;
|
||||
import com.easyagents.core.model.chat.response.AiMessageResponse;
|
||||
import com.easyagents.core.prompt.SimplePrompt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GiteeAiImageTest {
|
||||
|
||||
|
||||
@NotNull
|
||||
private static OpenAIChatConfig getOpenAIChatConfig() {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setApiKey("PXW1GXE******L7D12");
|
||||
// config.setModel("InternVL3-78B");
|
||||
config.setModel("Qwen3-32B");
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setLogEnabled(true);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImage() {
|
||||
OpenAIChatConfig config = getOpenAIChatConfig();
|
||||
ChatModel chatModel = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("请识别并输入 markdown,请用中文输出");
|
||||
prompt.addImageUrl("http://www.codeformat.cn/static/images/logo.png");
|
||||
|
||||
AiMessageResponse response = chatModel.chat(prompt);
|
||||
if (!response.isError()) {
|
||||
System.out.println(response.getMessage().getContent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChat() {
|
||||
OpenAIChatConfig config = getOpenAIChatConfig();
|
||||
config.setSupportImage(false);
|
||||
ChatModel chatModel = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("你叫什么名字");
|
||||
prompt.addImageUrl("http://www.codeformat.cn/static/images/logo.png");
|
||||
|
||||
AiMessageResponse response = chatModel.chat(prompt);
|
||||
if (!response.isError()) {
|
||||
System.out.println(response.getMessage().getContent());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
package com.easyagents.llm.openai;
|
||||
|
||||
import com.easyagents.core.agent.react.ReActAgent;
|
||||
import com.easyagents.core.agent.react.ReActAgentListener;
|
||||
import com.easyagents.core.agent.react.ReActAgentState;
|
||||
import com.easyagents.core.agent.react.ReActStep;
|
||||
import com.easyagents.core.memory.ChatMemory;
|
||||
import com.easyagents.core.message.ToolCall;
|
||||
import com.easyagents.core.message.ToolMessage;
|
||||
import com.easyagents.core.message.UserMessage;
|
||||
import com.easyagents.core.model.chat.ChatModel;
|
||||
import com.easyagents.core.model.chat.ChatOptions;
|
||||
import com.easyagents.core.model.chat.StreamResponseListener;
|
||||
import com.easyagents.core.model.chat.response.AiMessageResponse;
|
||||
import com.easyagents.core.model.chat.tool.Tool;
|
||||
import com.easyagents.core.model.chat.tool.ToolScanner;
|
||||
import com.easyagents.core.model.client.StreamContext;
|
||||
import com.easyagents.core.model.exception.ModelException;
|
||||
import com.easyagents.core.prompt.SimplePrompt;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class OpenAIChatModelTest {
|
||||
|
||||
@Test(expected = ModelException.class)
|
||||
public void testChat() {
|
||||
|
||||
String output = OpenAIChatConfig.builder()
|
||||
.endpoint("https://ai.gitee.com")
|
||||
.provider("GiteeAI")
|
||||
.model("Qwen3-32B")
|
||||
.apiKey("PXW1****D12")
|
||||
.buildModel()
|
||||
.chat("你叫什么名字");
|
||||
|
||||
System.out.println(output);
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testChatStream() {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setApiKey("PXW1GXE***");
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setModel("Qwen3-32B");
|
||||
config.setLogEnabled(true);
|
||||
|
||||
ChatOptions options = ChatOptions.builder().thinkingEnabled(false).build();
|
||||
|
||||
ChatModel chatModel = new OpenAIChatModel(config);
|
||||
|
||||
ChatModelTestUtils.waitForStream(chatModel, "你叫什么名字", new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
System.out.println(response.getMessage().getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(StreamContext context, Throwable throwable) {
|
||||
System.out.println("onFailure>>>>" + throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(StreamContext context) {
|
||||
System.out.println("stop!!!!");
|
||||
}
|
||||
}, options);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testChatStreamBailian() {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setApiKey("sk-32ab******57502");
|
||||
config.setEndpoint("https://dashscope.aliyuncs.com");
|
||||
config.setRequestPath("/compatible-mode/v1/chat/completions");
|
||||
config.setModel("qwen3-max");
|
||||
ChatModel chatModel = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("北京的天气如何?");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
|
||||
ChatModelTestUtils.waitForStream(chatModel, prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onFailure(StreamContext context, Throwable throwable) {
|
||||
System.out.println("onFailure>>>>" + throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
if (response.getMessage().getContent() == null) {
|
||||
System.out.println(response.getMessage());
|
||||
}
|
||||
|
||||
if (response.getMessage().isFinalDelta()) {
|
||||
List<ToolCall> toolCalls = response.getMessage().getToolCalls();
|
||||
System.out.println(toolCalls);
|
||||
}
|
||||
|
||||
System.out.println("onMessage >>>>>" + response.getMessage().getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(StreamContext context) {
|
||||
System.out.println("stop!!!!");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChatOllama() {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setEndpoint("http://localhost:11434");
|
||||
config.setModel("llama3");
|
||||
config.setLogEnabled(true);
|
||||
|
||||
ChatModel chatModel = new OpenAIChatModel(config);
|
||||
chatModel.chatStream("who are you", new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
System.out.println(response.getMessage().getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(StreamContext context) {
|
||||
System.out.println("stop!!!!");
|
||||
}
|
||||
});
|
||||
|
||||
// try {
|
||||
// Thread.sleep(2000);
|
||||
// } catch (InterruptedException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testChatWithImage() {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setApiKey("sk-5gqOcl*****");
|
||||
config.setModel("gpt-4-turbo");
|
||||
|
||||
|
||||
ChatModel chatModel = new OpenAIChatModel(config);
|
||||
SimplePrompt prompt = new SimplePrompt("What's in this image?");
|
||||
prompt.addImageUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg");
|
||||
|
||||
|
||||
AiMessageResponse response = chatModel.chat(prompt);
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling1() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setApiKey("sk-rts5NF6n*******");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("今天北京的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
AiMessageResponse response = llm.chat(prompt);
|
||||
|
||||
System.out.println(response.executeToolCallsAndGetResults());
|
||||
// 阴转多云
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling2() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setApiKey("sk-rts5NF6n*******");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("今天北京的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
AiMessageResponse response = llm.chat(prompt);
|
||||
|
||||
if (response.hasToolCalls()) {
|
||||
prompt.setToolMessages(response.executeToolCallsAndGetToolMessages());
|
||||
AiMessageResponse response1 = llm.chat(prompt);
|
||||
System.out.println(response1.getMessage().getContent());
|
||||
} else {
|
||||
System.out.println(response);
|
||||
}
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling3() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ark.cn-beijing.volces.com");
|
||||
config.setRequestPath("/api/v3/chat/completions");
|
||||
config.setModel("deepseek-v3-250324");
|
||||
config.setApiKey("2d57a");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("今天北京的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
AiMessageResponse response = llm.chat(prompt);
|
||||
|
||||
if (response.hasToolCalls()) {
|
||||
prompt.setToolMessages(response.executeToolCallsAndGetToolMessages());
|
||||
AiMessageResponse response1 = llm.chat(prompt);
|
||||
System.out.println(response1.getMessage().getContent());
|
||||
} else {
|
||||
System.out.println(response);
|
||||
}
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling4() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ark.cn-beijing.volces.com");
|
||||
config.setRequestPath("/api/v3/chat/completions");
|
||||
config.setModel("deepseek-v3-250324");
|
||||
config.setApiKey("2d57aa75");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("今天北京的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
System.out.println(" onMessage >>>>>" + response.hasToolCalls());
|
||||
}
|
||||
});
|
||||
|
||||
TimeUnit.SECONDS.sleep(5);
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling44() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ark.cn-beijing.volces.com");
|
||||
config.setRequestPath("/api/v3/chat/completions");
|
||||
config.setModel("deepseek-v3-250324");
|
||||
config.setApiKey("2d5");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("今天北京的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
System.out.println(" onMessage >>>>>" + response.hasToolCalls());
|
||||
}
|
||||
});
|
||||
|
||||
TimeUnit.SECONDS.sleep(5);
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling444() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
// config.setRequestPath("/api/v3/chat/completions");
|
||||
// config.setModel("Qwen3-32B");
|
||||
config.setModel("DeepSeek-V3.2");
|
||||
config.setApiKey("PXW1G***L7D12");
|
||||
// config.setLogEnabled(false);
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("北京和上海的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
|
||||
// System.out.println("onMessage11 >>>>>" + response);
|
||||
if (response.getMessage().isFinalDelta() && response.hasToolCalls()) {
|
||||
System.out.println(":::::::: start....");
|
||||
List<ToolMessage> toolMessages = response.executeToolCallsAndGetToolMessages();
|
||||
prompt.setAiMessage(response.getMessage());
|
||||
prompt.setToolMessages(toolMessages);
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
String msg = response.getMessage().getContent() != null ? response.getMessage().getContent() : response.getMessage().getReasoningContent();
|
||||
System.out.println(":::22" + msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(StreamContext context) {
|
||||
System.out.println("onStop >>>>>");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
String msg = response.getMessage().getContent() != null ? response.getMessage().getContent() : response.getMessage().getReasoningContent();
|
||||
System.out.println(">>>" + msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
TimeUnit.SECONDS.sleep(25);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling5() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setModel("Qwen3-32B");
|
||||
config.setApiKey("PXW1G*********D12");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("北京和上海的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
// System.out.println("onMessage >>>>>" + response);
|
||||
}
|
||||
});
|
||||
|
||||
TimeUnit.SECONDS.sleep(25);
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling55() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setModel("Qwen3-32B");
|
||||
// config.setModel("DeepSeek-V3");
|
||||
// config.setSupportToolMessage(false);
|
||||
config.setApiKey("PXW1");
|
||||
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("/no_think 北京和上海的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
|
||||
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
if (response.getMessage().isFinalDelta() && response.hasToolCalls()) {
|
||||
System.out.println(":::::::: start....");
|
||||
prompt.setAiMessage(response.getMessage());
|
||||
prompt.setToolMessages(response.executeToolCallsAndGetToolMessages());
|
||||
llm.chatStream(prompt, new StreamResponseListener() {
|
||||
@Override
|
||||
public void onMessage(StreamContext context, AiMessageResponse response) {
|
||||
String msg = response.getMessage().getContent() != null ? response.getMessage().getContent() : response.getMessage().getReasoningContent();
|
||||
System.out.println(":::" + msg);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
String msg = response.getMessage().getContent() != null ? response.getMessage().getContent() : response.getMessage().getReasoningContent();
|
||||
System.out.println(">>>" + msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
TimeUnit.SECONDS.sleep(25);
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void testFunctionCalling6() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
config.setLogEnabled(true);
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setModel("Qwen3-32B");
|
||||
config.setApiKey("PXW1");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
SimplePrompt prompt = new SimplePrompt("/no_think 北京和上海的天气怎么样");
|
||||
prompt.addToolsFromClass(WeatherFunctions.class);
|
||||
AiMessageResponse response = llm.chat(prompt);
|
||||
|
||||
prompt.setToolMessages(response.executeToolCallsAndGetToolMessages());
|
||||
|
||||
System.out.println(llm.chat(prompt));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testReAct1() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
// config.setDebug(true);
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setModel("Qwen3-32B");
|
||||
config.setApiKey("****");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
List<Tool> tools = ToolScanner.scan(WeatherFunctions.class);
|
||||
// ReActAgent reActAgent = new ReActAgent(llm, functions, "北京和上海的天气怎么样?");
|
||||
ReActAgent reActAgent = new ReActAgent(llm, tools, "介绍一下北京");
|
||||
reActAgent.addListener(new ReActAgentListener() {
|
||||
|
||||
@Override
|
||||
public void onActionStart(ReActStep step) {
|
||||
System.out.println(">>>>>>" + step.getThought());
|
||||
System.out.println("正在调用工具 >>>>> " + step.getAction() + ":" + step.getActionInput());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActionEnd(ReActStep step, Object result) {
|
||||
System.out.println("工具调用结束 >>>>> " + step.getAction() + ":" + step.getActionInput() + ">>>>结果:" + result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinalAnswer(String finalAnswer) {
|
||||
System.out.println("onFinalAnswer >>>>>" + finalAnswer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNonActionResponse(AiMessageResponse response) {
|
||||
System.out.println("onNonActionResponse >>>>>" + response.getMessage().getContent());
|
||||
}
|
||||
});
|
||||
|
||||
reActAgent.execute();
|
||||
}
|
||||
|
||||
|
||||
@Test()
|
||||
public void testReAct2() throws InterruptedException {
|
||||
OpenAIChatConfig config = new OpenAIChatConfig();
|
||||
// config.setDebug(true);
|
||||
config.setEndpoint("https://ai.gitee.com");
|
||||
config.setModel("Qwen2-72B-Instruct");
|
||||
config.setApiKey("*****");
|
||||
|
||||
OpenAIChatModel llm = new OpenAIChatModel(config);
|
||||
|
||||
List<Tool> tools = ToolScanner.scan(WeatherFunctions.class);
|
||||
ReActAgent reActAgent = new ReActAgent(llm, tools, "今天的天气怎么样?");
|
||||
// reActAgent.setStreamable(true);
|
||||
reActAgent.addListener(new ReActAgentListener() {
|
||||
|
||||
@Override
|
||||
public void onChatResponseStream(StreamContext context, AiMessageResponse response) {
|
||||
// System.out.print(response.getMessage().getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestUserInput(String question) {
|
||||
System.out.println("onRequestUserInput>>>" + question);
|
||||
|
||||
ReActAgentState state = reActAgent.getState();
|
||||
state.addMessage(new UserMessage("我在北京市"));
|
||||
ReActAgent newAgent = new ReActAgent(llm, tools, state);
|
||||
newAgent.addListener(this);
|
||||
newAgent.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActionStart(ReActStep step) {
|
||||
System.out.println(">>>>>>" + step.getThought());
|
||||
System.out.println("正在调用工具 >>>>> " + step.getAction() + ":" + step.getActionInput());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActionEnd(ReActStep step, Object result) {
|
||||
System.out.println("工具调用结束 >>>>> " + step.getAction() + ":" + step.getActionInput() + ">>>>结果:" + result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinalAnswer(String finalAnswer) {
|
||||
System.out.println("onFinalAnswer >>>>>" + finalAnswer);
|
||||
ChatMemory memory = reActAgent.getMemoryPrompt().getMemory();
|
||||
System.out.println(memory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNonActionResponseStream(StreamContext context) {
|
||||
System.out.println("onNonActionResponseStream >>>>>" + context);
|
||||
}
|
||||
});
|
||||
|
||||
reActAgent.execute();
|
||||
|
||||
TimeUnit.SECONDS.sleep(20);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.easyagents.llm.openai;
|
||||
|
||||
import com.easyagents.core.model.chat.tool.annotation.ToolDef;
|
||||
import com.easyagents.core.model.chat.tool.annotation.ToolParam;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class WeatherFunctions {
|
||||
|
||||
private static final String[] weathers = {
|
||||
"晴", "多云", "阴", "小雨", "中雨", "大雨", "暴雨", "雷阵雨",
|
||||
"小雪", "中雪", "大雪", "暴雪", "雨夹雪", "雾", "霾", "沙尘暴",
|
||||
"冰雹", "阵雨", "冻雨", "晴间多云", "局部多云", "强对流"
|
||||
};
|
||||
|
||||
@ToolDef(name = "get_the_weather_info", description = "get the weather info")
|
||||
public static String getWeatherInfo(@ToolParam(name = "city", description = "the city name") String name) {
|
||||
String weather = weathers[ThreadLocalRandom.current().nextInt(weathers.length)];
|
||||
System.out.println(">>>>>>>>>>>>>>!!!!!!" + name + ":" + weather);
|
||||
return weather;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user