Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,31 @@
import io.agentscope.core.message.Msg;
import io.agentscope.core.session.Session;
import io.agentscope.core.tool.Toolkit;
import io.github.malonetalk.agent.models.ModelFactory;
import io.github.malonetalk.agent.models.ModelProperties;
import io.github.malonetalk.agent.tools.MarkAgentTool;
import io.github.malonetalk.convertor.EventConverter;
import io.github.malonetalk.dto.ChatStreamEvent;
import io.github.malonetalk.utils.MsgUtils;
import jakarta.annotation.PostConstruct;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

@Slf4j
@Service
@RequiredArgsConstructor
public class AgentService {

private final ModelFactory modelFactory;
private final List<MarkAgentTool> allToolBeans;
private final ModelProperties modelProperties;
private final SessionService sessionService;
private Toolkit toolkit;

private final List<MarkAgentTool> allToolBeans;

public AgentService(
ModelFactory modelFactory,
SessionService sessionService,
List<MarkAgentTool> allToolBeans) {
this.modelFactory = modelFactory;
this.sessionService = sessionService;
this.allToolBeans = allToolBeans;
}

@PostConstruct
public void init() {
this.toolkit = new Toolkit();
Expand Down Expand Up @@ -109,7 +104,7 @@ private ReActAgent createAgent() {
5. 根据查询结果回答用户问题
注意:仅支持 SELECT 查询,不支持修改操作。生成SQL时请务必先查看表结构,确保列名和类型正确。
""")
.model(modelFactory.createModel())
.model(modelFactory.getInstance(modelProperties))
.toolkit(toolkit)
.memory(new InMemoryMemory())
.maxIters(10)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.models;

import io.agentscope.core.model.AnthropicChatModel;
import io.agentscope.core.model.Model;
import org.springframework.stereotype.Component;

@Component
public class AnthropicModelProvider implements ModelProvider {
@Override
public String provider() {
return "anthropic";
}

@Override
public Model createModel(ModelConfig config) {
return AnthropicChatModel.builder()
.apiKey(config.getApiKey())
.modelName(config.getName())
.stream(true)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.models;

import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.model.Model;
import org.springframework.stereotype.Component;

@Component
public class DashscopeModelProvider implements ModelProvider {

@Override
public String provider() {
return "dashscope";
}

@Override
public Model createModel(ModelConfig config) {
return DashScopeChatModel.builder()
.apiKey(config.getApiKey())
.modelName(config.getName())
.stream(true)
.enableThinking(true)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.models;

import io.agentscope.core.model.Model;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class DefaultModelFactoryImpl implements ModelFactory {

private final Map<String, ModelProvider> providerMap;

@Autowired
public DefaultModelFactoryImpl(List<ModelProvider> providers) {
providerMap =
providers.stream()
.collect(
Collectors.toUnmodifiableMap(
ModelProvider::provider,
Function.identity(),
(v1, v2) -> {
throw new IllegalStateException(
"Provider "
+ v1.provider()
+ " should be only one instance! Now"
+ " there are at least two instance: "
+ v1.getClass().getName()
+ " and "
+ v2.getClass().getName());
}));
}

@Override
public Model getInstance(ModelConfig config) {
if (!StringUtils.hasText(config.getProvider())) {
throw new IllegalArgumentException("provider should not be empty!");
}
ModelProvider provider = providerMap.get(config.getProvider());
if (provider == null) {
throw new IllegalArgumentException(
"Illegal provider argument: " + config.getProvider());
}
return provider.createModel(config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.models;

import lombok.Data;

@Data
public class ModelConfig {

private String provider;

private String name;

private String baseUrl;

private String apiKey;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.models;

import io.agentscope.core.model.Model;

public interface ModelFactory {

Model getInstance(ModelConfig config);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2026 github.com/MaloneTalk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* limitations under the License.
*/
package io.github.malonetalk.agent.models;

import io.github.malonetalk.common.Constants;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

// TODO: 之后从yml硬编码配置改为可由用户通过http接口修改的配置
@Component
@ConfigurationProperties(prefix = Constants.PROPERTIES_PREFIX + ".model")
public class ModelProperties extends ModelConfig {}
Loading
Loading