This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 8618145e44 [feat] add support for ollama and update docs (#3441)
8618145e44 is described below

commit 8618145e442857be7d68e0cb26721102a456176d
Author: Yang Chen <[email protected]>
AuthorDate: Sat Jun 7 18:48:41 2025 +0800

    [feat] add support for ollama and update docs (#3441)
---
 .../hertzbeat/common/constants/AiConstants.java    | 25 ++++++
 .../hertzbeat/common/constants/AiTypeEnum.java     |  7 +-
 .../hertzbeat/manager/config/AiProperties.java     |  5 ++
 .../manager/service/ai/OllamaAiService.java        | 95 ++++++++++++++++++++++
 .../src/main/resources/application.yml             |  4 +-
 home/docs/help/ai_config.md                        | 10 +++
 .../current/help/ai_config.md                      | 10 +++
 7 files changed, 154 insertions(+), 2 deletions(-)

diff --git 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java
 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java
index 01bea86835..c9ac9f64d8 100644
--- 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java
+++ 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java
@@ -143,4 +143,29 @@ public interface AiConstants {
 
     }
 
+    /**
+     * Ollama constants
+     */
+    interface OllamaConstants {
+
+        /**
+         * request role param
+         */
+        String REQUEST_ROLE = "user";
+
+        /**
+         * The model outputs the maximum tokens, with a maximum output of 8192 
and a default value of 3072
+         */
+        Integer MAX_TOKENS = 3072;
+
+        /**
+         * 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.7f;
+
+    }
+
 }
diff --git 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java
 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java
index b8e965e3ad..dee566abdb 100644
--- 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java
+++ 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java
@@ -42,7 +42,12 @@ public enum AiTypeEnum {
     /**
      * Moonshot AI
      */
-    kimiAi;
+    kimiAi,
+
+    /**
+     * Ollama AI
+     */
+    ollama;
 
 
     /**
diff --git 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java
 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java
index 8725d3520a..f2528769d4 100644
--- 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java
+++ 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java
@@ -52,4 +52,9 @@ public class AiProperties {
      */
     private String apiSecret;
 
+    /**
+     * API URL for the Ollama AI service.
+     */
+    private String apiUrl;
+
 }
diff --git 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/ai/OllamaAiService.java
 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/ai/OllamaAiService.java
new file mode 100644
index 0000000000..8d3a7b3130
--- /dev/null
+++ 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/ai/OllamaAiService.java
@@ -0,0 +1,95 @@
+/*
+ * 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.service.ai;
+
+import io.jsonwebtoken.lang.Assert;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.common.constants.AiConstants;
+import org.apache.hertzbeat.common.constants.AiTypeEnum;
+import org.apache.hertzbeat.manager.config.AiProperties;
+import org.apache.hertzbeat.manager.pojo.dto.AiMessage;
+import org.apache.hertzbeat.manager.pojo.dto.OpenAiRequestParamDTO;
+import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.codec.ServerSentEvent;
+import org.springframework.stereotype.Service;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.ExchangeStrategies;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Flux;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+
+/**
+ * Ollama service
+ */
+@Service("OllamaServiceImpl")
+@ConditionalOnProperty(prefix = "ai", name = "type", havingValue = "ollama")
+@Slf4j
+public class OllamaAiService implements AiService{
+    @Autowired
+    private AiProperties aiProperties;
+
+    private WebClient webClient;
+
+    @PostConstruct
+    private void init() {
+        Assert.notNull(aiProperties.getApiUrl(), "Ollama API URL is null");
+        this.webClient = WebClient.builder()
+                .baseUrl(aiProperties.getApiUrl())
+                .defaultHeader(HttpHeaders.CONTENT_TYPE, 
MediaType.APPLICATION_JSON_VALUE)
+                .exchangeStrategies(ExchangeStrategies.builder()
+                        .codecs(item -> 
item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024))
+                        .build())
+                .build();
+    }
+
+    @Override
+    public AiTypeEnum getType() {
+        return AiTypeEnum.ollama;
+    }
+
+    @Override
+    public Flux<ServerSentEvent<String>> requestAi(String text) {
+        checkParam(text, aiProperties.getModel());
+        OpenAiRequestParamDTO ollamaParam = OpenAiRequestParamDTO.builder()
+                .model(aiProperties.getModel())
+                .stream(Boolean.TRUE)
+                .maxTokens(AiConstants.OllamaConstants.MAX_TOKENS)
+                .temperature(AiConstants.OllamaConstants.TEMPERATURE)
+                .messages(List.of(new 
AiMessage(AiConstants.OllamaConstants.REQUEST_ROLE, text)))
+                .build();
+
+        return webClient.post()
+                .body(BodyInserters.fromValue(ollamaParam))
+                .retrieve()
+                .bodyToFlux(String.class)
+                .filter(aiResponse -> !"[DONE]".equals(aiResponse))
+                .map(OpenAiResponse::convertToResponse)
+                .doOnError(error -> log.info("OllamaAiService.requestAi 
exception:{}", error.getMessage()));
+    }
+
+    private void checkParam(String param, String model) {
+        Assert.notNull(param, "text is null");
+        Assert.notNull(model, "model is null");
+    }
+}
diff --git a/hertzbeat-manager/src/main/resources/application.yml 
b/hertzbeat-manager/src/main/resources/application.yml
index 26db2e3d95..835b5bd26e 100644
--- a/hertzbeat-manager/src/main/resources/application.yml
+++ b/hertzbeat-manager/src/main/resources/application.yml
@@ -273,7 +273,7 @@ grafana:
 
 # See the documentation for details : 
https://hertzbeat.apache.org/zh-cn/docs/help/aiConfig
 ai:
-  # AI Type:zhiPu、alibabaAi、kimiAi、sparkDesk
+  # AI Type:zhiPu、alibabaAi、kimiAi、sparkDesk、ollama
   type:
   # Model name:glm-4、qwen-turboo、moonshot-v1-8k、generalv3.5
   model:
@@ -281,3 +281,5 @@ ai:
   api-key:
   #At present, only IFLYTEK large model needs to be filled in
   api-secret:
+  # The URL of the ollama AI service
+  api-url:
diff --git a/home/docs/help/ai_config.md b/home/docs/help/ai_config.md
index 81fd40e88f..facabdc43b 100644
--- a/home/docs/help/ai_config.md
+++ b/home/docs/help/ai_config.md
@@ -59,3 +59,13 @@ QuickStart: 
<https://www.xfyun.cn/doc/platform/quickguide.html>
 | Spark Pro         | generalv3   |
 | Spark V2.0        | generalv2   |
 | Spark Lite(free)  | general     |
+
+#### Ollama AI
+
+QuickStart: <https://github.com/ollama/ollama/tree/main/docs>
+
+| Name of the parameter | Example                                         | 
Link                          |
+|-----------------------|-------------------------------------------------|-------------------------------|
+| type                  | ollama (must be exactly the same as example)    |    
                           |
+| model                 | deepseek-r1:latest、qwen3:latest、llama4:16x17b   | 
<https://ollama.com/search>   |
+| api-url               | <http://127.0.0.1:11434/v1/chat/completions>    |    
                           |
diff --git 
a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md 
b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md
index 721ab8b299..b8d835ece7 100644
--- a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md
+++ b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md
@@ -59,3 +59,13 @@ keywords: [人工智能 AI]
 | Spark Pro       | generalv3                     |
 | Spark V2.0      | generalv2                     |
 | Spark Lite(免费版) | general                       |
+
+#### Ollama AI
+
+快速入门: <https://github.com/ollama/ollama/tree/main/docs>
+
+| 参数名称 |                     示例                      |                 链接      
            |
+|-----------------------|--------------------------------------------------|---------------------------------------|
+| type                  | ollama (必须和示例完全相同)  |                                
       |
+| model                 | deepseek-r1:latest、qwen3:latest、llama4:16x17b | 
<https://ollama.com/search> |
+| api-url               | <http://127.0.0.1:11434/v1/chat/completions> |       
                           |


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to