Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ header:
- '**/target/**'
- '**/*.ftl'
- '**/*.log'
- '**/*.st'
- '.git/'
- '.github/**'
- '**/.gitignore'
Expand Down
44 changes: 44 additions & 0 deletions bigtop-manager-ai/bigtop-manager-ai-assistant/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>bigtop-manager-ai-assistant</artifactId>
<name>${project.artifactId}</name>
<description>Bigtop Manager AI Assistant</description>

<dependencies>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-openai</artifactId>
</dependency>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.ai.assistant;

import org.apache.bigtop.manager.ai.assistant.provider.LocSystemPromptProvider;
import org.apache.bigtop.manager.ai.core.AbstractAIAssistantFactory;
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.ToolBox;
import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;
import org.apache.bigtop.manager.ai.core.provider.SystemPromptProvider;
import org.apache.bigtop.manager.ai.openai.OpenAIAssistant;

import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;

import java.util.Objects;

public class GeneralAssistantFactory extends AbstractAIAssistantFactory {

private SystemPromptProvider systemPromptProvider = new LocSystemPromptProvider();
private ChatMemoryStore chatMemoryStore = new InMemoryChatMemoryStore();

public GeneralAssistantFactory() {}

public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider) {
this.systemPromptProvider = systemPromptProvider;
}

public GeneralAssistantFactory(SystemPromptProvider systemPromptProvider, ChatMemoryStore chatMemoryStore) {
this.systemPromptProvider = systemPromptProvider;
this.chatMemoryStore = chatMemoryStore;
}

@Override
public AIAssistant createWithPrompt(
PlatformType platformType, AIAssistantConfigProvider assistantConfig, Object id, Object promptId) {
AIAssistant aiAssistant = create(platformType, assistantConfig, id);
SystemMessage systemPrompt = systemPromptProvider.getSystemPrompt(promptId);
aiAssistant.setSystemPrompt(systemPrompt);
return aiAssistant;
}

@Override
public AIAssistant create(PlatformType platformType, AIAssistantConfigProvider assistantConfig, Object id) {
if (Objects.requireNonNull(platformType) == PlatformType.OPENAI) {
AIAssistant aiAssistant = OpenAIAssistant.builder()
.id(id)
.memoryStore(chatMemoryStore)
.withConfigProvider(assistantConfig)
.build();
aiAssistant.setSystemPrompt(systemPromptProvider.getSystemPrompt());
return aiAssistant;
}
return null;
}

@Override
public ToolBox createToolBox(PlatformType platformType) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.ai.assistant.provider;

import org.apache.bigtop.manager.ai.core.provider.AIAssistantConfigProvider;

import java.util.HashMap;
import java.util.Map;

public class AIAssistantConfig implements AIAssistantConfigProvider {
private final Map<String, String> configMap;

private AIAssistantConfig(Map<String, String> configMap) {
this.configMap = configMap;
}

public static Builder builder() {
return new Builder();
}

public static Builder withDefault(String baseUrl, String apiKey) {
Builder builder = new Builder();
return builder.set("baseUrl", baseUrl).set("apiKey", apiKey);
}

@Override
public Map<String, String> configs() {

return configMap;
}

public static class Builder {
private final Map<String, String> configs;

public Builder() {
configs = new HashMap<>();
configs.put("memoryLen", "30");
}

public Builder set(String key, String value) {
configs.put(key, value);
return this;
}

public AIAssistantConfig build() {
return new AIAssistantConfig(configs);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.ai.assistant.provider;

import org.apache.bigtop.manager.ai.core.provider.SystemPromptProvider;

import org.springframework.util.ResourceUtils;

import dev.langchain4j.data.message.SystemMessage;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Objects;

@Slf4j
public class LocSystemPromptProvider implements SystemPromptProvider {

public static final String DEFAULT = "default";
private static final String SYSTEM_PROMPT_PATH = "src/main/resources/";
private static final String DEFAULT_NAME = "big-data-professor.st";

@Override
public SystemMessage getSystemPrompt(Object id) {
if (Objects.equals(id.toString(), DEFAULT)) {
return getSystemPrompt();
} else {
return loadPromptFromFile(id.toString());
}
}

@Override
public SystemMessage getSystemPrompt() {
return loadPromptFromFile(DEFAULT_NAME);
}

private SystemMessage loadPromptFromFile(String fileName) {
final String filePath = SYSTEM_PROMPT_PATH + fileName;
try {
File file = ResourceUtils.getFile(filePath);
String text = Files.readString(file.toPath(), StandardCharsets.UTF_8);
return SystemMessage.from(text);
} catch (IOException e) {
//
log.error(
"Exception occurred while loading SystemPrompt from local. Here is some information:{}",
e.getMessage());
return SystemMessage.from("");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You are a qualified big data expert who knows the world's leading open source big data component engineering projects. Now please focus on the content in the direction of big data. I will ask you some questions and hope to get your answers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.ai.assistant;

import org.apache.bigtop.manager.ai.assistant.provider.AIAssistantConfig;
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;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import dev.langchain4j.model.openai.OpenAiChatModelName;
import reactor.core.publisher.Flux;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when;

public class AIAssistantServiceTest {

private AIAssistantConfigProvider configProvider = AIAssistantConfig.builder()
.set("apiKey", "sk-")
// The `baseUrl` has a default value that is automatically generated based on the `PlatformType`.
.set("baseUrl", "https://api.openai.com/v1")
// default 30
.set("memoryLen", "10")
.set("modelName", OpenAiChatModelName.GPT_3_5_TURBO.toString())
.build();

@Mock
private AIAssistant aiAssistant;

@Mock
private AIAssistantFactory aiAssistantFactory;

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
when(aiAssistant.ask("1?")).thenReturn("1");
when(aiAssistant.streamAsk("stream 1?")).thenReturn(Flux.create(emmit -> {
final String text = "stream text";
for (int i = 0; i < text.length(); i++) {
emmit.next(text.charAt(i) + "");
}
}));
when(aiAssistantFactory.create(PlatformType.OPENAI, configProvider)).thenReturn(this.aiAssistant);
when(aiAssistant.getPlatform()).thenReturn(PlatformType.OPENAI.getValue());
}

@Test
public void createNew2SimpleChat() {
AIAssistant aiAssistant = aiAssistantFactory.create(PlatformType.OPENAI, configProvider);
String ask = aiAssistant.ask("1?");
assertFalse(ask.isEmpty());
System.out.println(ask);
}

@Test
public void createNew2StreamChat() throws InterruptedException {
AIAssistant aiAssistant = aiAssistantFactory.create(PlatformType.OPENAI, configProvider);
Flux<String> stringFlux = aiAssistant.streamAsk("stream 1?");
stringFlux.subscribe(
System.out::println,
error -> System.out.println("error:" + error),
() -> System.out.println("Completed"));
Thread.sleep(1000);
}
}
Loading