📦

solon-ai

by opensolon/solon-ai

0 views

Java AI(智能体) 全场景应用开发框架(LLM,Function Call,RAG,Embedding,Reranking,Flow,MCP Server,Mcp Client,Mcp Proxy)。同时兼容 java8 ~ java24。

javaChat & Messaging


Solon-AI

Java AI(智能体) 全场景应用开发框架(支持已知 AI 开发的各种能力)
【基于 Solon 应用开发框架构建】

https://solon.noear.org/article/learn-solon-ai

Maven Apache 2 jdk-8 jdk-11 jdk-17 jdk-21 jdk-23
gitee star github star gitcode star


简介

面向全场景的 Java AI 应用开发框架(支持已知 AI 开发的各种能力)。是 Solon 项目的一部分。也可嵌入到 SpringBoot2、jFinal、Vert.x 等框架中使用。

其中 solon-ai(& mcp) 的嵌入示例:

主要接口体验示例

  • ChatModel(通用接口,基于方言适配实现不同提供商与模型的扩展)
ChatModel chatModel = ChatModel.of("http://127.0.0.1:11434/api/chat")
                .provider("ollama") //需要指定供应商,用于识别接口风格(也称为方言)
                .model("qwen2.5:1.5b")
                .build();
//同步调用,并打印响应消息
System.out.println(chatModel.prompt("hello").call().getMessage());

//响应式调用
chatModel.prompt("hello").stream(); //Publisher<ChatResponse>
  • Function Calling(或者 Tool Calling)
//可以添加默认工具(即所有请求有产),或请求时工具
chatModel.prompt("今天杭州的天气情况?")
    .options(op->op.toolsAdd(new FunctionTools()))
    .call();
  • Vision(多媒体感知)
chatModel.prompt(ChatMessage.ofUser("这图里有方块吗?", Image.ofUrl(imageUrl)))
    .call();
  • RAG(EmbeddingModel,Repository,DocumentLoader,RerankingModel)
//构建知识库
EmbeddingModel embeddingModel = EmbeddingModel.of(apiUrl).apiKey(apiKey).provider(provider).model(model).batchSize(10).build();
RerankingModel rerankingModel = RerankingModel.of(apiUrl).apiKey(apiKey).provider(provider).model(model).build();
InMemoryRepository repository = new InMemoryRepository(TestUtils.getEmbeddingModel()); //3.初始化知识库

repository.insert(new PdfLoader(pdfUri).load());

//检索
List<Document> docs = repository.search(query);

//如果有需要,可以重排一下
docs = rerankingModel.rerank(query, docs);

//提示语增强是
ChatMessage message = ChatMessage.augment(query, docs);

//调用大模型
chatModel.prompt(message) 
    .call();
  • Ai Flow(模拟实现 Dify 的流程应用)
id: demo1
layout:
  - type: "start"
  - task: "@VarInput"
    meta:
      message: "Solon 是谁开发的?"
  - task: "@EmbeddingModel"
    meta:
      embeddingConfig: # "@type": "org.noear.solon.ai.embedding.EmbeddingConfig"
        provider: "ollama"
        model: "bge-m3"
        apiUrl: "http://127.0.0.1:11434/api/embed"
  - task: "@InMemoryRepository"
    meta:
      documentSources:
        - "https://solon.noear.org/article/about?format=md"
      splitPipeline:
        - "org.noear.solon.ai.rag.splitter.RegexTextSplitter"
        - "org.noear.solon.ai.rag.splitter.TokenSizeTextSplitter"
  - task: "@ChatModel"
    meta:
      systemPrompt: "你是个知识库"
      stream: false
      chatConfig: # "@type": "org.noear.solon.ai.chat.ChatConfig"
        provider: "ollama"
        model: "qwen2.5:1.5b"
        apiUrl: "http://127.0.0.1:11434/api/chat"
  - task: "@ConsoleOutput"

# FlowEngine flowEngine = FlowEngine.newInstance();
# ...
# flowEngine.eval("demo1");
  • MCP server(支持多端点)
//组件方式构建
@McpServerEndpoint(name="mcp-case1", sseEndpoint = "/case1/sse") 
public class McpServer {
    @ToolMapping(description = "查询天气预报")
    public String getWeather(@Param(description = "城市位置") String location) {
        return "晴,14度";
    }

    @ResourceMapping(uri = "config://app-version", description = "获取应用版本号", mimeType = "text/config")
    public String getAppVersion() {
        return "v3.2.0";
    }


    @PromptMapping(description = "生成关于某个主题的提问")
    public Collection<ChatMessage> askQuestion(@Param(description = "主题") String topic) {
        return Arrays.asList(
                ChatMessage.ofUser("请解释一下'" + topic + "'的概念?")
        );
    }
}

//原生 java 方式构建
McpServerEndpointProvider serverEndpoint = McpServerEndpointProvider.builder()
        .name("mcp-case2")
        .sseEndpoint("/case2/sse")
        .build();

serverEndpoint.addTool(new MethodToolProvider(new McpServerTools())); //添加工具
serverEndpoint.addResource(new MethodResourceProvider(new McpServerResources())); //添加资源
serverEndpoint.addPrompt(new MethodPromptProvider(new McpServerPrompts())); //添加提示语
serverEndpoint.postStart();
  • MCP client
McpClientToolProvider clientToolProvider = McpClientToolProvider.builder()
                .apiUrl("http://localhost:8080/case1/sse")
                .build();

String rst = clientToolProvider.callToolAsText("getWeather", Map.of("location", "杭州"))
                .getContent();
  • MCP Proxy (示例,把 gitee mcp stdio 转为 sse 服务)

配置参考自:https://gitee.com/oschina/mcp-gitee

@McpServerEndpoint(name = "mcp-case3", sseEndpoint="/case3/sse")
public class McpStdioToSseServerDemo implements ToolProvider {
    McpClientProvider stdioToolProvider = McpClientProvider.builder()
            .channel(McpChannel.STDIO) //表示使用 stdio
            .serverParameters(ServerParameters.builder("npx")
                    .args("-y", "@gitee/mcp-gitee@latest")
                    .addEnvVar("GITEE_API_BASE", "https://gitee.com/api/v5")
                    .addEnvVar("GITEE_ACCESS_TOKEN", "<your personal access token>")
                    .build())
            .build();

    @Override
    public Collection<FunctionTool> getTools() {
        return stdioToolProvider.getTools();
    }
}

Solon 项目相关代码仓库

代码仓库描述
/opensolon/solonSolon ,主代码仓库
/opensolon/solon-examplesSolon ,官网配套示例代码仓库
/opensolon/solon-expressionSolon Expression ,代码仓库
/opensolon/solon-flowSolon Flow ,代码仓库
/opensolon/solon-aiSolon Ai ,代码仓库
/opensolon/solon-cloudSolon Cloud ,代码仓库
/opensolon/solon-adminSolon Admin ,代码仓库
/opensolon/solon-jakartaSolon Jakarta ,代码仓库(base java21)
/opensolon/solon-integrationSolon Integration ,代码仓库
/opensolon/solon-gradle-pluginSolon Gradle ,插件代码仓库
/opensolon/solon-idea-pluginSolon Idea ,插件代码仓库
/opensolon/solon-vscode-pluginSolon VsCode ,插件代码仓库

Install

No configuration available
For more configuration details, refer to the content on the left

Related

Related projects feature coming soon

Will recommend related projects based on sub-categories