Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIGTOP-4260: Add chatbot command and tools #101

Merged
merged 28 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

import org.apache.commons.lang3.NotImplementedException;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;

Expand All @@ -45,6 +49,10 @@ public GeneralAssistantFactory(ChatMemoryStore chatMemoryStore) {
this(new LocSystemPromptProvider(), chatMemoryStore);
}

public GeneralAssistantFactory() {
this(new LocSystemPromptProvider(), new InMemoryChatMemoryStore());
}

public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider, ChatMemoryStore chatMemoryStore) {
this.systemPromptProvider = systemPromptProvider;
this.chatMemoryStore = chatMemoryStore;
Expand Down Expand Up @@ -84,6 +92,27 @@ public AIAssistant create(PlatformType platformType, AIAssistantConfigProvider a
return createWithPrompt(platformType, assistantConfig, id, SystemPrompt.DEFAULT_PROMPT);
}

@Override
public AIAssistant createWithTools(
PlatformType platformType, AIAssistantConfigProvider assistantConfig, ToolProvider toolProvider) {
AIAssistant.Builder builder =
switch (platformType) {
case OPENAI -> OpenAIAssistant.builder();
case DASH_SCOPE -> DashScopeAssistant.builder();
case QIANFAN -> QianFanAssistant.builder();
};
/// TODO: Only a portion of the models of DashScope support the API of OpenAI
ChatLanguageModel chatLanguageModel =
builder.withConfigProvider(assistantConfig).getChatLanguageModel();
StreamingChatLanguageModel streamingChatLanguageModel = builder.getStreamingChatLanguageModel();

return AiServices.builder(AIAssistant.class)
.chatLanguageModel(chatLanguageModel)
.streamingChatLanguageModel(streamingChatLanguageModel)
.toolProvider(toolProvider)
.build();
}

@Override
public ToolBox createToolBox(PlatformType platformType) {
throw new NotImplementedException("ToolBox is not implemented for GeneralAssistantFactory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
*/
package org.apache.bigtop.manager.ai.core;

import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.factory.AIAssistant;
import org.apache.bigtop.manager.ai.core.factory.AIAssistantFactory;
import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;

public abstract class AbstractAIAssistantFactory implements AIAssistantFactory {}
import dev.langchain4j.service.tool.ToolProvider;

public abstract class AbstractAIAssistantFactory implements AIAssistantFactory {
public abstract AIAssistant createWithTools(
PlatformType platformType, AIAssistantConfigProvider assistantConfig, ToolProvider toolProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import reactor.core.publisher.Flux;

Expand Down Expand Up @@ -84,5 +86,9 @@ interface Builder {
Builder withConfigProvider(AIAssistantConfigProvider configProvider);

AIAssistant build();

ChatLanguageModel getChatLanguageModel();

StreamingChatLanguageModel getStreamingChatLanguageModel();
}
}
5 changes: 5 additions & 0 deletions bigtop-manager-ai/bigtop-manager-ai-dashscope/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-dashscope</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
import dev.langchain4j.internal.ValidationUtils;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.dashscope.QwenChatModel;
import dev.langchain4j.model.dashscope.QwenStreamingChatModel;
import io.reactivex.Flowable;
import reactor.core.publisher.Flux;

Expand Down Expand Up @@ -319,5 +323,24 @@ public AIAssistant build() {
MessageWindowChatMemory chatMemory = builder.build();
return new DashScopeAssistant(chatMemory, param);
}

@Override
public ChatLanguageModel getChatLanguageModel() {
String model = ValidationUtils.ensureNotNull(configProvider.getModel(), "model");
String apiKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("apiKey"), "apiKey");
return QwenChatModel.builder().apiKey(apiKey).modelName(model).build();
}

@Override
public StreamingChatLanguageModel getStreamingChatLanguageModel() {
String model = ValidationUtils.ensureNotNull(configProvider.getModel(), "model");
String apiKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("apiKey"), "apiKey");
return QwenStreamingChatModel.builder()
.apiKey(apiKey)
.modelName(model)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,39 @@ public static Builder builder() {

public static class Builder extends AbstractAIAssistant.Builder {

public AIAssistant build() {
@Override
public ChatLanguageModel getChatLanguageModel() {
String model = ValidationUtils.ensureNotNull(configProvider.getModel(), "model");
String apiKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("apiKey"), "apiKey");
ChatLanguageModel openAiChatModel = OpenAiChatModel.builder()
return OpenAiChatModel.builder()
.apiKey(apiKey)
.baseUrl(BASE_URL)
.modelName(model)
.build();
StreamingChatLanguageModel openaiStreamChatModel = OpenAiStreamingChatModel.builder()
}

@Override
public StreamingChatLanguageModel getStreamingChatLanguageModel() {
String model = ValidationUtils.ensureNotNull(configProvider.getModel(), "model");
String apiKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("apiKey"), "apiKey");
return OpenAiStreamingChatModel.builder()
.apiKey(apiKey)
.baseUrl(BASE_URL)
.modelName(model)
.build();
}

public AIAssistant build() {
MessageWindowChatMemory.Builder builder = MessageWindowChatMemory.builder()
.chatMemoryStore(chatMemoryStore)
.maxMessages(MEMORY_LEN);
if (id != null) {
builder.id(id);
}
MessageWindowChatMemory chatMemory = builder.build();
return new OpenAIAssistant(openAiChatModel, openaiStreamChatModel, chatMemory);
return new OpenAIAssistant(getChatLanguageModel(), getStreamingChatLanguageModel(), chatMemory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,42 @@ public static Builder builder() {
public static class Builder extends AbstractAIAssistant.Builder {

public AIAssistant build() {
MessageWindowChatMemory.Builder builder = MessageWindowChatMemory.builder()
.chatMemoryStore(chatMemoryStore)
.maxMessages(MEMORY_LEN);
if (id != null) {
builder.id(id);
}
MessageWindowChatMemory chatMemory = builder.build();
return new QianFanAssistant(getChatLanguageModel(), getStreamingChatLanguageModel(), chatMemory);
}

@Override
public ChatLanguageModel getChatLanguageModel() {
String model = ValidationUtils.ensureNotNull(configProvider.getModel(), "model");
String apiKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("apiKey"), "apiKey");
String secretKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("secretKey"), "secretKey");
ChatLanguageModel qianfanChatModel = QianfanChatModel.builder()
return QianfanChatModel.builder()
.apiKey(apiKey)
.secretKey(secretKey)
.modelName(model)
.build();
StreamingChatLanguageModel qianfanStreamChatModel = QianfanStreamingChatModel.builder()
}

@Override
public StreamingChatLanguageModel getStreamingChatLanguageModel() {
String model = ValidationUtils.ensureNotNull(configProvider.getModel(), "model");
String apiKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("apiKey"), "apiKey");
String secretKey = ValidationUtils.ensureNotNull(
configProvider.getCredentials().get("secretKey"), "secretKey");
return QianfanStreamingChatModel.builder()
.apiKey(apiKey)
.secretKey(secretKey)
.modelName(model)
.build();
MessageWindowChatMemory.Builder builder = MessageWindowChatMemory.builder()
.chatMemoryStore(chatMemoryStore)
.maxMessages(MEMORY_LEN);
if (id != null) {
builder.id(id);
}
MessageWindowChatMemory chatMemory = builder.build();
return new QianFanAssistant(qianfanChatModel, qianfanStreamChatModel, chatMemory);
}
}
}
7 changes: 6 additions & 1 deletion bigtop-manager-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<micrometer.version>1.12.4</micrometer.version>
<jdbc.dm.version>8.1.2.192</jdbc.dm.version>
<sshd.version>2.14.0</sshd.version>
<langchain4j.version>0.33.0</langchain4j.version>
<langchain4j.version>0.35.0</langchain4j.version>
<dashscope.version>2.16.3</dashscope.version>
<mybatis-spring-boot-starter.version>3.0.3</mybatis-spring-boot-starter.version>
<pagehelper-spring-boot-starter.version>2.1.0</pagehelper-spring-boot-starter.version>
Expand Down Expand Up @@ -267,6 +267,11 @@
<artifactId>langchain4j-qianfan</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-dashscope</artifactId>
<version>${langchain4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package org.apache.bigtop.manager.server.controller;

import org.apache.bigtop.manager.server.enums.ChatbotCommand;
import org.apache.bigtop.manager.server.model.converter.ChatThreadConverter;
import org.apache.bigtop.manager.server.model.dto.ChatThreadDTO;
import org.apache.bigtop.manager.server.model.req.ChatbotMessageReq;
import org.apache.bigtop.manager.server.model.req.ChatbotThreadReq;
import org.apache.bigtop.manager.server.model.vo.ChatMessageVO;
import org.apache.bigtop.manager.server.model.vo.ChatThreadVO;
import org.apache.bigtop.manager.server.service.ChatbotService;
import org.apache.bigtop.manager.server.service.LLMAgentService;
import org.apache.bigtop.manager.server.utils.ResponseEntity;

import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -51,6 +53,9 @@ public class ChatbotController {
@Resource
private ChatbotService chatbotService;

@Resource
private LLMAgentService llmAgentService;

@Operation(summary = "new thread", description = "Create a chat threads")
@PostMapping("/threads")
public ResponseEntity<ChatThreadVO> createChatThread(@RequestBody ChatbotThreadReq chatbotThreadReq) {
Expand Down Expand Up @@ -82,6 +87,19 @@ public ResponseEntity<List<ChatThreadVO>> getAllChatThreads() {
@Operation(summary = "talk", description = "Talk with Chatbot")
@PostMapping("/threads/{threadId}/talk")
public SseEmitter talk(@PathVariable Long threadId, @RequestBody ChatbotMessageReq messageReq) {
ChatbotCommand command;
if (messageReq.getCommand() == null) {
command = ChatbotCommand.getCommandFromMessage(messageReq.getMessage());
if (command != null) {
messageReq.setMessage(
messageReq.getMessage().substring(command.getCmd().length() + 2));
}
} else {
command = ChatbotCommand.getCommand(messageReq.getCommand());
}
if (command != null) {
return llmAgentService.talk(threadId, command, messageReq.getMessage());
}
return chatbotService.talk(threadId, messageReq.getMessage());
}

Expand All @@ -90,4 +108,10 @@ public SseEmitter talk(@PathVariable Long threadId, @RequestBody ChatbotMessageR
public ResponseEntity<List<ChatMessageVO>> history(@PathVariable Long threadId) {
return ResponseEntity.success(chatbotService.history(threadId));
}

@Operation(summary = "get commands", description = "Get all commands")
@GetMapping("/commands")
public ResponseEntity<List<String>> getCommands() {
return ResponseEntity.success(llmAgentService.getChatbotCommands());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.controller;

import org.apache.bigtop.manager.server.service.LLMAgentService;
import org.apache.bigtop.manager.server.utils.ResponseEntity;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;

@Tag(name = "LLM Agent Controller")
@RestController
@RequestMapping("/llm/agent/")
public class LLMAgentController {
@Resource
private LLMAgentService llmAgentService;

@Operation(summary = "activate intelligent alert ", description = "Activate Intelligent Alert ")
@PostMapping("/alert/activate")
public ResponseEntity<Boolean> activateIntelligentAlert() {
return ResponseEntity.success(llmAgentService.activateIntelligentAlert());
}
}
Loading
Loading