多模型LLM集成
夜月AI应用平台 通过统一的抽象层接入15+主流大语言模型,支持文本对话、嵌入生成、图像生成、网络搜索等多种模型类型,让用户无需关心底层实现细节即可使用各种LLM能力。
核心特性
- 15+ LLM提供商:覆盖国内外主流大模型
- 4种模型类型:CHAT、EMBEDDING、TEXT_IMAGE、WEB_SEARCH
- 策略模式抽象:统一接口,动态切换
- 动态模型配置:通过数据库配置,无需重启
支持的LLM提供商
国际提供商
| 提供商 | 模型示例 | 特点 |
|---|---|---|
| OpenAI | GPT-4, GPT-3.5 | 全球领先的LLM |
| Azure OpenAI | GPT-4 | 企业级部署 |
| Claude | Claude 3 | 长文本理解 |
| Gemini | Gemini Pro | 多模态能力 |
| Ollama | Llama, Mistral | 本地部署 |
国内提供商
| 提供商 | 模型示例 | 特点 |
|---|---|---|
| Qwen (阿里) | 通义千问 | 中文优化 |
| Qianfan (百度) | 文心一言 | 中文优化 |
| Zhipu AI | 智谱清言 | 中文优化 |
| DeepSeek | DeepSeek-V2 | 高性价比 |
| Spark (讯飞) | 星火大模型 | 中文优化 |
| Yi (零一万物) | Yi-Large | 长文本 |
| Douyin (字节) | 豆包 | 中文优化 |
| Gitee AI | 多种模型 | 开源模型 |
| Silicon | 多种模型 | 开源模型 |
| MiMo | MiMo | 中文优化 |
模型类型
CHAT - 文本对话
用于多轮对话、问答、内容生成等场景。
java
ChatModel model = modelStoreFactory.createChatModel(provider, modelName);
String response = model.generate("你好,请介绍一下自己");EMBEDDING - 嵌入生成
用于文本向量化,支持RAG知识库、语义搜索等场景。
java
EmbeddingModel model = modelStoreFactory.createEmbeddingModel(provider, modelName);
float[] embedding = model.embed("这是一段测试文本");TEXT_IMAGE - 图像生成
用于文本到图像生成场景。
java
ImageModel model = modelStoreFactory.createImageModel(provider, modelName);
byte[] image = model.generate("一只可爱的猫咪");WEB_SEARCH - 网络搜索
用于实时网络搜索场景。
java
WebSearchModel model = modelStoreFactory.createWebSearchModel(provider, modelName);
List<SearchResult> results = model.search("今天的天气");技术实现
策略模式
每个LLM提供商都有对应的 ModelBuildHandler 实现:
java
public interface ModelBuildHandler {
ChatModel buildChatModel(LLMModelConfig config);
EmbeddingModel buildEmbeddingModel(LLMModelConfig config);
// ...
}工厂模式
通过 ModelStoreFactory 统一创建模型实例:
java
@Component
public class ModelStoreFactory {
public ChatModel createChatModel(String provider, String modelName) {
ModelBuildHandler handler = getHandler(provider);
LLMModelConfig config = loadConfig(modelName);
return handler.buildChatModel(config);
}
}动态配置
模型配置存储在数据库中,支持动态更新:
sql
CREATE TABLE llm_model (
id BIGINT PRIMARY KEY,
provider VARCHAR(50),
model_name VARCHAR(100),
api_key VARCHAR(500),
api_endpoint VARCHAR(500),
config JSON
);使用场景
智能对话
java
// 创建对话模型
ChatModel model = modelStoreFactory.createChatModel("openai", "gpt-4");
// 多轮对话
ChatMemory memory = new MessageWindowChatMemory(10);
ChatAssistant assistant = AiServices.builder(ChatAssistant.class)
.chatModel(model)
.chatMemory(memory)
.build();
String response = assistant.chat("你好");RAG知识库
java
// 创建嵌入模型
EmbeddingModel embeddingModel = modelStoreFactory.createEmbeddingModel("openai", "text-embedding-3-small");
// 文本向量化
float[] embedding = embeddingModel.embed("这是一段测试文本");内容生成
java
// 创建图像模型
ImageModel imageModel = modelStoreFactory.createImageModel("openai", "dall-e-3");
// 生成图像
byte[] image = imageModel.generate("一只可爱的猫咪坐在月亮上");配置示例
OpenAI配置
yaml
llm:
providers:
openai:
api-key: ${OPENAI_API_KEY}
base-url: https://api.openai.com/v1
models:
- name: gpt-4
type: CHAT
- name: text-embedding-3-small
type: EMBEDDINGOllama本地配置
yaml
llm:
providers:
ollama:
base-url: http://localhost:11434
models:
- name: llama3
type: CHAT
- name: nomic-embed-text
type: EMBEDDING