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

[feature] :support alibaba Ai #2153

Merged
merged 34 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f3b9bdf
[feature] : ai
Yanshuming1 Jun 19, 2024
c28c373
[feature] : ai
Yanshuming1 Jun 19, 2024
fa3c5d6
[feature] : ai
Yanshuming1 Jun 19, 2024
0a562f0
[feature] : ai
Yanshuming1 Jun 19, 2024
9842953
[feature] : ai
Yanshuming1 Jun 19, 2024
92838b0
[feature] : ai
Yanshuming1 Jun 22, 2024
28ff307
[feature] : ai
Yanshuming1 Jun 22, 2024
ff8f894
[feature] : ai
Yanshuming1 Jun 22, 2024
597ad83
[feature] : ai
Yanshuming1 Jun 22, 2024
9ba43a6
[feature] : fix AIConstants
Yanshuming1 Jun 22, 2024
341e1ad
[feature] : fix AIConstants
Yanshuming1 Jun 22, 2024
03dd8b6
[feature] : fix AIConstants
Yanshuming1 Jun 22, 2024
6954921
[feature] : fix AIConstants
Yanshuming1 Jun 22, 2024
af19c06
[feature] : fix AIConstants
Yanshuming1 Jun 23, 2024
0c8a5bd
[feature] : fix AIConstants
Yanshuming1 Jun 23, 2024
918e0b8
Merge branch 'master' into AI
zqr10159 Jun 23, 2024
60ec4c6
[check] : check ci
Yanshuming1 Jun 23, 2024
afda40d
Merge remote-tracking branch 'origin/AI' into AI
Yanshuming1 Jun 23, 2024
d53cb17
[check] : check ci
Yanshuming1 Jun 23, 2024
2b00e5b
Merge branch 'master' into AI
tomsun28 Jun 24, 2024
f839a3e
Merge branch 'master' into AI
tomsun28 Jun 25, 2024
4756005
Merge branch 'master' into AI
tomsun28 Jun 25, 2024
28c18f1
Merge remote-tracking branch 'refs/remotes/upstream/master' into supp…
Yanshuming1 Jun 25, 2024
7d466ba
[improve] : fix AiService getType method data type,complete informati…
Yanshuming1 Jun 25, 2024
3be60b8
[feature] :support alibaba Ai
Yanshuming1 Jun 26, 2024
487bed5
[feature] :support alibaba Ai
Yanshuming1 Jun 27, 2024
a1112a4
[feature] :support alibaba Ai
Yanshuming1 Jun 27, 2024
8076e36
Merge remote-tracking branch 'refs/remotes/upstream/master' into alib…
Yanshuming1 Jun 28, 2024
cc8c96c
[feature] :support alibaba Ai
Yanshuming1 Jun 28, 2024
acaa96e
Merge branch 'master' into alibabaAi
zqr10159 Jun 28, 2024
67786cf
Merge branch 'master' into alibabaAi
tomsun28 Jul 1, 2024
de769b7
[feature] : check ci
Yanshuming1 Jul 1, 2024
4f9a93b
Merge remote-tracking branch 'origin/alibabaAi' into alibabaAi
Yanshuming1 Jul 1, 2024
49d1eae
Merge branch 'master' into alibabaAi
zqr10159 Jul 1, 2024
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 @@ -51,4 +51,35 @@ interface ZhiPuConstants {
double TEMPERATURE = 0.95;

}

/**
* alibaba Ai constants
*/
interface AliAiConstants {

/**
* alibabaAi request url
*/
String URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

/**
* request role param
*/
String REQUEST_ROLE = "user";

/**
* The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024
*/
Integer MAX_TOKENS = 1024;

/**
* The sampling temperature, which controls the randomness of the output, must be positive
* The value ranges from 0.0 to 1.0, and cannot be equal to 0. The default value is 0.95. The larger the value,
* the more random and creative the output will be. The smaller the value, the more stable or certain the output will be
* You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time
*/
float TEMPERATURE = 0.9f;


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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
*
* http://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.hertzbeat.common.constants;

/**
* Ai type Enum
*/
public enum AiTypeEnum {

/**
* 智普
*/
zhiPu,

/**
* alibabaAi
*/
alibabaAi;


/**
* get type
*/
public static AiTypeEnum getTypeByName(String type) {
for (AiTypeEnum aiTypeEnum : values()) {
if (aiTypeEnum.name().equals(type)) {
return aiTypeEnum;
}

}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.hertzbeat.manager.service.impl.AiServiceFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -55,7 +54,7 @@ public class AiController {
* @return AI response
*/
@GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE})
public Flux<ServerSentEvent<String>> requestAi(@RequestParam("text") String text,
public Flux<String> requestAi(@RequestParam("text") String text,
@RequestParam(value = "type", required = false) String currentlyDisabledType) {
AiService aiServiceImplBean = aiServiceFactory.getAiServiceImplBean(type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public ResponseEntity<Message<Void>> deleteMonitors(
@Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List<Long> ids
) {
if (ids != null && !ids.isEmpty()) {
monitorService.deleteMonitors(new HashSet<>(ids));
monitorService.cancelManageMonitors(new HashSet<>(ids));
Yanshuming1 marked this conversation as resolved.
Show resolved Hide resolved
}
Message<Void> message = Message.success();
return ResponseEntity.ok(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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
*
* http://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.hertzbeat.manager.pojo.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
* Alibaba Ai Request param
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AliAiRequestParamDTO {

/**
* ai version
*/
private String model;

/**
* Enter information about the model
*/
private Input input;

/**
* Parameters used to control model generation
*/
private Parameters parameters;

/**
* Input
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class Input {

/**
* request message
*/
private List<AiMessage> messages;

}

/**
* Parameters
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class Parameters {

/**
* The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024
*/
@JsonProperty("max_tokens")
private Integer maxTokens;

/**
* Used to control the degree of randomness and variety. Specifically, the temperature value controls the
* degree to which the probability distribution for each candidate word is smoothed when text is generated.
* A higher temperature will reduce the peak value of the probability distribution, so that more low-probability
* words will be selected and the results will be more diversified. A lower temperature will increase the peak of the probability distribution,
* making it easier for high-probability words to be selected and producing more certain results.
*/
private float temperature;

/**
* The Internet search service is built into the model, and this parameter controls whether the model refers
* to the Internet search results when generating text. The value can be:
* true: If Internet search is enabled, the model uses the search results as reference information in the text
* generation process, but the model "decides" whether to use the Internet search results based on its internal logic.
* false: Turn off Internet search.
*/
@JsonProperty("enable_search")
private boolean enableSearch;

/**
* Set return format,default message
*/
@JsonProperty("result_format")
private String resultFormat;

/**
* Control whether incremental output is enabled in stream output mode, that is, whether the subsequent output content contains
* the output content. If the value is set to True, the incremental output mode will be enabled, and the subsequent output will
* not contain the output content, you need to splice the overall output by yourself. Set to False to contain the output.
*/
@JsonProperty("incremental_output")
private boolean incrementalOutput;

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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
*
* http://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.hertzbeat.manager.pojo.dto;


import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* AliAiResponse
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AliAiResponse {

/**
* response
*/
private AliAiOutput output;

/**
* Returns the number of tokens invoked by the model at the end.
*/
private Tokens usage;

/**
* AliAiOutput
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AliAiOutput {

/**
* response message
*/
private List<Choice> choices;

}

/**
* Choice
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Choice {

/**
* Stop cause:
* null: being generated
* stop: stop token causes the end
* length: indicates that the generation length ends
*/
@JsonProperty("finish_reason")
private String finishReason;

/**
* response message
*/
private AiMessage message;
}

/**
* Tokens
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Tokens {

/**
* The number of tokens of the model output content
*/
@JsonProperty("output_tokens")
private Integer outputTokens;

/**
* The number of tokens entered this request.
* When enable_search is set to true, the number of tokens entered is greater than the number of
* tokens you entered the request because you need to add search related content.
*/
@JsonProperty("input_tokens")
private Integer inputTokens;

/**
* usage.output_tokens and usage.input_tokens sum
*/
@JsonProperty("total_tokens")
private Integer totalTokens;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.hertzbeat.manager.service;


import org.springframework.http.codec.ServerSentEvent;
import org.apache.hertzbeat.common.constants.AiTypeEnum;
import reactor.core.publisher.Flux;


Expand All @@ -31,13 +31,13 @@ public interface AiService {
* get AI type
* @return type
*/
String getType();
AiTypeEnum getType();

/**
* AI response
* @param text text
* @return AI response
*/
Flux<ServerSentEvent<String>> requestAi(String text);
Flux<String> requestAi(String text);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.apache.hertzbeat.common.constants.AiTypeEnum;
import org.apache.hertzbeat.manager.service.AiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -37,7 +38,7 @@ public class AiServiceFactoryImpl {
@Autowired
private List<AiService> aiService;

private Map<String, AiService> aiServiceFactoryMap = new HashMap<>();
private Map<AiTypeEnum, AiService> aiServiceFactoryMap = new HashMap<>();

@PostConstruct
public void init() {
Expand All @@ -47,7 +48,9 @@ public void init() {

public AiService getAiServiceImplBean(String type) {
Assert.notNull(type, "type is null");
AiService aiServiceImpl = aiServiceFactoryMap.get(type);
AiTypeEnum typeByName = AiTypeEnum.getTypeByName(type);
Assert.notNull(typeByName, "The current type is not supported");
AiService aiServiceImpl = aiServiceFactoryMap.get(typeByName);
Assert.notNull(aiServiceImpl, "No bean for current type found");
return aiServiceImpl;
}
Expand Down
Loading