Skip to content

Commit 10767ac

Browse files
authored
Merge pull request #3 from twj666/master
[dev] sfchian后端代码
2 parents e2d879b + 7791db2 commit 10767ac

33 files changed

+4693
-0
lines changed

prompto-lab-app/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@
131131
<groupId>org.springframework.boot</groupId>
132132
<artifactId>spring-boot-maven-plugin</artifactId>
133133
</plugin>
134+
<plugin>
135+
<groupId>org.apache.maven.plugins</groupId>
136+
<artifactId>maven-compiler-plugin</artifactId>
137+
<configuration>
138+
<source>16</source>
139+
<target>16</target>
140+
</configuration>
141+
</plugin>
134142
</plugins>
135143
</build>
136144
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.github.timemachinelab.sfchain.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* 描述: AI操作注解
10+
* 用于标识AI操作类,并指定操作类型和默认模型
11+
* @author suifeng
12+
* 日期: 2025/8/11
13+
*/
14+
@Target(ElementType.TYPE)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
public @interface AIOp {
17+
18+
/**
19+
* 操作类型标识
20+
* 例如: "POSITION_BASIC_INFO_PARSE_OP"
21+
*/
22+
String value();
23+
24+
/**
25+
* 默认使用的模型名称
26+
* 如果不指定,将使用操作映射配置中的模型
27+
*/
28+
String defaultModel() default "";
29+
30+
/**
31+
* 操作描述
32+
*/
33+
String description() default "";
34+
35+
/**
36+
* 是否启用该操作
37+
*/
38+
boolean enabled() default true;
39+
40+
/**
41+
* 支持的模型列表(可选)
42+
* 如果指定,将限制该操作只能使用这些模型
43+
*/
44+
String[] supportedModels() default {};
45+
46+
/**
47+
* 是否需要JSON输出
48+
*/
49+
boolean requireJsonOutput() default true;
50+
51+
/**
52+
* 是否自动修复JSON格式错误
53+
* 当requireJsonOutput为true且AI返回的JSON格式有误时,自动调用JSON修复操作
54+
*/
55+
boolean autoRepairJson() default true;
56+
57+
/**
58+
* 是否支持思考模式
59+
*/
60+
boolean supportThinking() default false;
61+
62+
/**
63+
* 默认最大token数
64+
*/
65+
int defaultMaxTokens() default 4096;
66+
67+
/**
68+
* 默认温度参数
69+
*/
70+
double defaultTemperature() default 0.7;
71+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.github.timemachinelab.sfchain.config;
2+
3+
import io.github.timemachinelab.sfchain.core.AIModel;
4+
import io.github.timemachinelab.sfchain.core.openai.OpenAIModelConfig;
5+
import io.github.timemachinelab.sfchain.core.openai.OpenAIModelFactory;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.context.annotation.Primary;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
/**
19+
* 描述: OpenAI兼容模型自动配
20+
* @author suifeng
21+
* 日期: 2025/8/11
22+
*/
23+
@Slf4j
24+
@Configuration
25+
@EnableConfigurationProperties(OpenAIModelsConfig.class)
26+
@RequiredArgsConstructor
27+
@ConditionalOnProperty(prefix = "ai.openai-models", name = "enabled", havingValue = "true", matchIfMissing = true)
28+
public class OpenAIAutoConfiguration {
29+
30+
private final OpenAIModelsConfig openAIModelsConfig;
31+
32+
/**
33+
* 创建OpenAI模型工厂
34+
*/
35+
@Bean
36+
@Primary
37+
public OpenAIModelFactory openAIModelFactory() {
38+
OpenAIModelFactory factory = new OpenAIModelFactory();
39+
40+
// 注册配置文件中的模型
41+
Map<String, OpenAIModelConfig> modelConfigs = openAIModelsConfig.getValidModelConfigs();
42+
modelConfigs.forEach((name, config) -> {
43+
try {
44+
factory.registerModel(config);
45+
log.info("成功注册模型: {} ({})", config.getModelName(), config.getProvider());
46+
} catch (Exception e) {
47+
log.error("注册模型失败: {} - {}", config.getModelName(), e.getMessage());
48+
}
49+
});
50+
51+
return factory;
52+
}
53+
54+
/**
55+
* 创建AI模型列表,供ModelRegistry使用
56+
*/
57+
@Bean
58+
public List<AIModel> aiModels(OpenAIModelFactory factory) {
59+
List<AIModel> models = new ArrayList<>();
60+
61+
// 为每个注册的模型创建实例
62+
factory.getRegisteredModelNames().forEach(modelName -> {
63+
try {
64+
AIModel model = factory.createModel(modelName);
65+
models.add(model);
66+
log.info("创建AI模型实例: {}", modelName);
67+
} catch (Exception e) {
68+
log.error("创建模型实例失败: {} - {}", modelName, e.getMessage());
69+
}
70+
});
71+
72+
return models;
73+
}
74+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package io.github.timemachinelab.sfchain.config;
2+
3+
import io.github.timemachinelab.sfchain.core.openai.OpenAIModelConfig;
4+
import lombok.Data;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* 描述: OpenAI兼容模型配置
13+
* @author suifeng
14+
* 日期: 2025/8/11
15+
*/
16+
@Data
17+
@Component
18+
@ConfigurationProperties(prefix = "ai.openai-models")
19+
public class OpenAIModelsConfig {
20+
21+
/**
22+
* 模型配置映射
23+
* key: 模型名称
24+
* value: 模型配置
25+
*/
26+
private Map<String, ModelConfigProperties> models = new HashMap<>();
27+
28+
/**
29+
* 模型配置属性
30+
*/
31+
@Data
32+
public static class ModelConfigProperties {
33+
/**
34+
* 模型名称
35+
*/
36+
private String modelName;
37+
38+
/**
39+
* API基础URL
40+
*/
41+
private String baseUrl;
42+
43+
/**
44+
* API密钥
45+
*/
46+
private String apiKey;
47+
48+
/**
49+
* 默认最大token数
50+
*/
51+
private Integer defaultMaxTokens = 4096;
52+
53+
/**
54+
* 默认温度参数
55+
*/
56+
private Double defaultTemperature = 0.7;
57+
58+
/**
59+
* 是否支持流式输出
60+
*/
61+
private Boolean supportStream = false;
62+
63+
/**
64+
* 是否支持JSON格式输出
65+
*/
66+
private Boolean supportJsonOutput = false;
67+
68+
/**
69+
* 是否支持思考模式
70+
*/
71+
private Boolean supportThinking = false;
72+
73+
/**
74+
* 额外的HTTP请求头
75+
*/
76+
private Map<String, String> additionalHeaders = new HashMap<>();
77+
78+
/**
79+
* 模型描述
80+
*/
81+
private String description;
82+
83+
/**
84+
* 模型提供商
85+
*/
86+
private String provider;
87+
88+
/**
89+
* 是否启用
90+
*/
91+
private Boolean enabled = true;
92+
93+
/**
94+
* 转换为OpenAIModelConfig
95+
*/
96+
public OpenAIModelConfig toOpenAIModelConfig() {
97+
return OpenAIModelConfig.builder()
98+
.modelName(modelName)
99+
.baseUrl(baseUrl)
100+
.apiKey(apiKey)
101+
.defaultMaxTokens(defaultMaxTokens)
102+
.defaultTemperature(defaultTemperature)
103+
.supportStream(supportStream)
104+
.supportJsonOutput(supportJsonOutput)
105+
.supportThinking(supportThinking)
106+
.additionalHeaders(additionalHeaders)
107+
.description(description)
108+
.provider(provider)
109+
.enabled(enabled)
110+
.build();
111+
}
112+
}
113+
114+
/**
115+
* 获取所有有效的模型配置
116+
*/
117+
public Map<String, OpenAIModelConfig> getValidModelConfigs() {
118+
Map<String, OpenAIModelConfig> validConfigs = new HashMap<>();
119+
120+
models.forEach((key, properties) -> {
121+
if (properties.getModelName() == null) {
122+
properties.setModelName(key);
123+
}
124+
125+
OpenAIModelConfig config = properties.toOpenAIModelConfig();
126+
if (config.isValid() && Boolean.TRUE.equals(config.getEnabled())) {
127+
validConfigs.put(key, config);
128+
}
129+
});
130+
131+
return validConfigs;
132+
}
133+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.timemachinelab.sfchain.constants;
2+
3+
/**
4+
* 描述: AI操作常量定义
5+
* 定义所有AI操作的标识符
6+
*
7+
* @author suifeng
8+
* 日期: 2025/8/11
9+
*/
10+
public class AIOperationConstant {
11+
12+
/**
13+
* JSON修复操作
14+
*/
15+
public static final String JSON_REPAIR_OP = "JSON_REPAIR_OP";
16+
17+
/**
18+
* 文本分类操作
19+
*/
20+
public static final String TEXT_CLASSIFICATION_OP = "TEXT_CLASSIFICATION";
21+
22+
/**
23+
* 模型验证操作
24+
*/
25+
public static final String MODEL_VALIDATION_OP = "MODEL_VALIDATION_OP";
26+
27+
/**
28+
* 私有构造函数,防止实例化
29+
*/
30+
private AIOperationConstant() {
31+
throw new UnsupportedOperationException("常量类不允许实例化");
32+
}
33+
}

0 commit comments

Comments
 (0)