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
@@ -0,0 +1,72 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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
*
* https://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 com.alibaba.cloud.ai.example.manus.dynamic.dialect;

import com.alibaba.cloud.ai.example.manus.recorder.entity.PlanExecutionRecordEntity;
import org.checkerframework.checker.initialization.qual.Initialized;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.UnknownKeyFor;
import org.hibernate.boot.Metadata;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.springframework.stereotype.Component;

/**
* auth: dahua time: 20250809
*/
@Component
public class DynamicColumnIntegrator implements Integrator {

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
Dialect dialect = jdbcServices.getDialect();
PersistentClass persistentClass = metadata.getEntityBinding(PlanExecutionRecordEntity.class.getName());
if (persistentClass == null) {
return;
}
Property contentProperty = persistentClass.getProperty("planExecutionRecord");
if (contentProperty == null) {
return;
}
contentProperty.getColumns().forEach(column -> {
if (dialect instanceof PostgreSQLDialect) { // pg
column.setSqlType("TEXT");
}
else if (dialect instanceof MySQLDialect) { // mysql
column.setSqlType("LONGTEXT");
}
else if (dialect instanceof H2Dialect) { // h2
column.setSqlType("LONGTEXT");
}
});
}

@Override
public void disintegrate(@UnknownKeyFor @NonNull @Initialized SessionFactoryImplementor sessionFactory,
@UnknownKeyFor @NonNull @Initialized SessionFactoryServiceRegistry serviceRegistry) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public class DynamicModelEntity {
@Column(nullable = false, columnDefinition = "boolean default false")
private boolean isDefault;

@Column(nullable = true, columnDefinition = "DOUBLE DEFAULT 0.7")
@Column
private Double temperature;

@Column(nullable = true, columnDefinition = "DOUBLE DEFAULT NULL")
@Column
private Double topP;

@Column(nullable = true)
@Column
private String completionsPath;

public DynamicModelEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,7 @@
*/
package com.alibaba.cloud.ai.example.manus.dynamic.model.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cloud.ai.example.manus.dynamic.agent.entity.DynamicAgentEntity;
import com.alibaba.cloud.ai.example.manus.dynamic.agent.repository.DynamicAgentRepository;
import com.alibaba.cloud.ai.example.manus.dynamic.model.entity.DynamicModelEntity;
Expand All @@ -46,8 +28,25 @@
import com.alibaba.cloud.ai.example.manus.dynamic.model.repository.DynamicModelRepository;
import com.alibaba.cloud.ai.example.manus.event.JmanusEventPublisher;
import com.alibaba.cloud.ai.example.manus.event.ModelChangeEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import cn.hutool.core.util.StrUtil;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ModelServiceImpl implements ModelService {
Expand Down Expand Up @@ -82,6 +81,8 @@ public ModelConfig getModelById(String id) {
@Override
public ModelConfig createModel(ModelConfig config) {
try {
// Set default values
setDefaultConfig(config);
// Check if an Model with the same name already exists
DynamicModelEntity existingModel = repository.findByModelName(config.getModelName());
if (existingModel != null) {
Expand Down Expand Up @@ -119,6 +120,7 @@ public ModelConfig createModel(ModelConfig config) {

@Override
public ModelConfig updateModel(ModelConfig config) {
setDefaultConfig(config);
DynamicModelEntity entity = repository.findById(config.getId())
.orElseThrow(() -> new IllegalArgumentException("Model not found: " + config.getId()));

Expand Down Expand Up @@ -348,6 +350,15 @@ private void updateEntityFromConfig(DynamicModelEntity entity, ModelConfig confi
entity.setCompletionsPath(config.getCompletionsPath());
}

private void setDefaultConfig(ModelConfig modelConfig) {
if (modelConfig.getTemperature() == null) {
modelConfig.setTemperature(0.7);
}
if (!StringUtils.hasText(modelConfig.getCompletionsPath())) {
modelConfig.setCompletionsPath("/v1/chat/completions");
}
}

@Override
@Transactional
public void setDefaultModel(Long modelId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -423,9 +422,6 @@ private OpenAiApi openAiApi(RestClient.Builder restClientBuilder, WebClient.Buil
.filter((request, next) -> next.exchange(request).timeout(Duration.ofMinutes(10)));

String completionsPath = dynamicModelEntity.getCompletionsPath();
if (!StringUtils.hasText(completionsPath)) {
completionsPath = "/v1/chat/completions";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个初始化 不要了? 我感觉这里改了好多次了。 会不会有兼容问题?

}

return new OpenAiApi(dynamicModelEntity.getBaseUrl(), new SimpleApiKey(dynamicModelEntity.getApiKey()),
multiValueMap, completionsPath, "/v1/embeddings", restClientBuilder, enhancedWebClientBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class PlanExecutionRecordEntity {
private Date gmtModified;

@Convert(converter = StringAttributeConverter.class)
@Column(columnDefinition = "longtext")
@Column
private PlanExecutionRecord planExecutionRecord;

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.alibaba.cloud.ai.example.manus.dynamic.dialect.DynamicColumnIntegrator
Loading