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-4222: Add LLM platform QianFan #65

Merged
merged 5 commits into from
Sep 12, 2024
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
4 changes: 4 additions & 0 deletions bigtop-manager-ai/bigtop-manager-ai-assistant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-dashscope</artifactId>
</dependency>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-qianfan</artifactId>
</dependency>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-dao</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.bigtop.manager.ai.core.provider.SystemPromptProvider;
import org.apache.bigtop.manager.ai.dashscope.DashScopeAssistant;
import org.apache.bigtop.manager.ai.openai.OpenAIAssistant;
import org.apache.bigtop.manager.ai.qianfan.QianFanAssistant;

import org.apache.commons.lang3.NotImplementedException;

Expand Down Expand Up @@ -66,8 +67,8 @@ public AIAssistant createWithPrompt(
switch (platformType) {
case OPENAI -> OpenAIAssistant.builder();
case DASH_SCOPE -> DashScopeAssistant.builder();
case QIANFAN -> QianFanAssistant.builder();
};

AIAssistant aiAssistant = builder.id(id)
.memoryStore((id == null) ? new InMemoryChatMemoryStore() : chatMemoryStore)
.withConfigProvider(assistantConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public boolean test() {
return ask("1+1=") != null;
}

@Override
public Object getId() {
return chatMemory.id();
}

public abstract static class Builder implements AIAssistant.Builder {
protected Object id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
@Getter
public enum PlatformType {
OPENAI("openai"),
DASH_SCOPE("dashscope");
DASH_SCOPE("dashscope"),
QIANFAN("qianfan");

private final String value;

Expand Down
1 change: 0 additions & 1 deletion bigtop-manager-ai/bigtop-manager-ai-dashscope/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.16.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@ public static Builder builder() {
return new Builder();
}

@Override
public Object getId() {
return dashScopeThreadParam.getThreadId();
}

@Override
public Flux<String> streamAsk(String userMessage) {
saveMessage(userMessage, MessageSender.USER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.bigtop.manager.ai.core.AbstractAIAssistant;
import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.exception.AssistantConfigNotSetException;
import org.apache.bigtop.manager.ai.core.factory.AIAssistant;

import dev.langchain4j.data.message.AiMessage;
Expand Down Expand Up @@ -56,9 +55,6 @@ public OpenAIAssistant(

@Override
public Flux<String> streamAsk(String chatMessage) {
if (chatMemory == null || streamingChatLanguageModel == null) {
throw new AssistantConfigNotSetException("threadId");
}
chatMemory.add(UserMessage.from(chatMessage));
return Flux.create(
emitter -> streamingChatLanguageModel.generate(chatMemory.messages(), new StreamingResponseHandler<>() {
Expand All @@ -83,9 +79,6 @@ public void onComplete(Response<AiMessage> response) {

@Override
public String ask(String chatMessage) {
if (chatMemory == null || chatLanguageModel == null) {
throw new AssistantConfigNotSetException("threadId");
}
chatMemory.add(UserMessage.from(chatMessage));
Response<AiMessage> generate = chatLanguageModel.generate(chatMemory.messages());
String aiMessage = generate.content().text();
Expand All @@ -95,28 +88,9 @@ public String ask(String chatMessage) {

@Override
public void setSystemPrompt(String systemPrompt) {
if (chatMemory == null) {
throw new AssistantConfigNotSetException("threadId");
}
chatMemory.add(SystemMessage.systemMessage(systemPrompt));
}

public void setSystemPrompt(SystemMessage systemPrompt) {
if (chatMemory == null) {
throw new AssistantConfigNotSetException("threadId");
}
chatMemory.add(systemPrompt);
}

@Override
public Object getId() {
return chatMemory.id();
}

public void resetMemory() {
chatMemory.clear();
}

@Override
public PlatformType getPlatform() {
return PlatformType.OPENAI;
Expand Down
44 changes: 44 additions & 0 deletions bigtop-manager-ai/bigtop-manager-ai-qianfan/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>
</parent>

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

<dependencies>
<dependency>
<groupId>org.apache.bigtop</groupId>
<artifactId>bigtop-manager-ai-core</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-qianfan</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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.qianfan;

import org.apache.bigtop.manager.ai.core.AbstractAIAssistant;
import org.apache.bigtop.manager.ai.core.enums.PlatformType;
import org.apache.bigtop.manager.ai.core.factory.AIAssistant;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.internal.ValidationUtils;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.qianfan.QianfanChatModel;
import dev.langchain4j.model.qianfan.QianfanStreamingChatModel;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

public class QianFanAssistant extends AbstractAIAssistant {

private final ChatLanguageModel chatLanguageModel;
private final StreamingChatLanguageModel streamingChatLanguageModel;
private SystemMessage systemMessage;

public QianFanAssistant(
ChatLanguageModel chatLanguageModel,
StreamingChatLanguageModel streamingChatLanguageModel,
ChatMemory chatMemory) {
super(chatMemory);
this.chatLanguageModel = chatLanguageModel;
this.streamingChatLanguageModel = streamingChatLanguageModel;
for (ChatMessage chatMessage : chatMemory.messages()) {
if (chatMessage instanceof SystemMessage) {
this.systemMessage = (SystemMessage) chatMessage;
}
}
}

@Override
public Flux<String> streamAsk(String chatMessage) {
chatMemory.add(UserMessage.from(chatMessage));
return Flux.create(
emitter -> streamingChatLanguageModel.generate(chatMemory.messages(), new StreamingResponseHandler<>() {
@Override
public void onNext(String token) {
emitter.next(token);
}

@Override
public void onError(Throwable error) {
emitter.error(error);
}

@Override
public void onComplete(Response<AiMessage> response) {
StreamingResponseHandler.super.onComplete(response);
chatMemory.add(response.content());
}
}),
FluxSink.OverflowStrategy.BUFFER);
}

@Override
public String ask(String chatMessage) {
chatMemory.add(UserMessage.from(chatMessage));
Response<AiMessage> generate = chatLanguageModel.generate(chatMemory.messages());
String aiMessage = generate.content().text();
chatMemory.add(AiMessage.from(aiMessage));
return aiMessage;
}

@Override
public void setSystemPrompt(String systemPrompt) {
// Multiple system messages are not supported
if (this.systemMessage == null) {
this.systemMessage = SystemMessage.systemMessage(systemPrompt);
chatMemory.add(this.systemMessage);
}
}

@Override
public PlatformType getPlatform() {
return PlatformType.QIANFAN;
}

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

public static class Builder extends AbstractAIAssistant.Builder {

public AIAssistant build() {
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()
.apiKey(apiKey)
.secretKey(secretKey)
.modelName(model)
.build();
StreamingChatLanguageModel qianfanStreamChatModel = 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.qianfan;

import org.apache.bigtop.manager.ai.core.factory.ToolBox;

import reactor.core.publisher.Flux;

import java.util.List;

public class QianFanToolBox implements ToolBox {
@Override
public List<String> getTools() {
return null;
}

@Override
public String invoke(String toolName) {
return null;
}

@Override
public Flux<String> streamInvoke(String toolName) {
return null;
}
}
1 change: 1 addition & 0 deletions bigtop-manager-ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<modules>
<module>bigtop-manager-ai-openai</module>
<module>bigtop-manager-ai-dashscope</module>
<module>bigtop-manager-ai-qianfan</module>
<module>bigtop-manager-ai-core</module>
<module>bigtop-manager-ai-assistant</module>
</modules>
Expand Down
11 changes: 11 additions & 0 deletions bigtop-manager-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<micrometer.version>1.12.4</micrometer.version>
<jdbc.dm.version>8.1.2.192</jdbc.dm.version>
<langchain4j.version>0.33.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>
</properties>
Expand Down Expand Up @@ -248,6 +249,16 @@
<artifactId>langchain4j-open-ai</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>${dashscope.version}</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-qianfan</artifactId>
<version>${langchain4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ private Boolean testAuthorization(PlatformAuthorizedDTO platformAuthorizedDTO) {
AIAssistant aiAssistant = getAiAssistantFactory()
.create(
getPlatformType(platformAuthorizedDTO.getPlatformName()),
getAIAssistantConfig(platformAuthorizedDTO, null),
false);
getAIAssistantConfig(platformAuthorizedDTO, null));
try {
return aiAssistant.test();
} catch (Exception e) {
Expand Down
Loading
Loading