文本处理
大约 6 分钟
第六章:文本处理与生成
6.1 文本摘要
文本摘要是AI的重要应用之一,Spring AI提供了强大的文本摘要功能。
6.1.1 基础文本摘要
@RestController
public class SummarizationController {
private final ChatClient chatClient;
public SummarizationController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/summarize")
public String summarizeText(@RequestBody String text) {
String prompt = "Please summarize the following text in 3 sentences:\n\n" + text;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}
}6.1.2 不同长度的摘要
@GetMapping("/summarize/length")
public String summarizeWithLength(
@RequestParam String text,
@RequestParam(defaultValue = "short") String length) {
String prompt;
switch (length.toLowerCase()) {
case "short":
prompt = "Summarize the following text in one sentence:\n\n" + text;
break;
case "medium":
prompt = "Summarize the following text in 3 sentences:\n\n" + text;
break;
case "long":
prompt = "Summarize the following text in 5 sentences:\n\n" + text;
break;
default:
prompt = "Summarize the following text:\n\n" + text;
}
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.1.3 关键点提取
@GetMapping("/summarize/key-points")
public List<String> extractKeyPoints(@RequestBody String text) {
String prompt = "Extract the key points from the following text:\n\n" + text +
"\nReturn the key points as a bulleted list.";
String response = chatClient.prompt()
.user(prompt)
.call()
.content();
// 解析响应为列表
return parseKeyPoints(response);
}
private List<String> parseKeyPoints(String response) {
// 实现解析逻辑
return Arrays.asList(response.split("\n- "));
}6.2 内容翻译
6.2.1 基础翻译
@RestController
public class TranslationController {
private final ChatClient chatClient;
public TranslationController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/translate")
public String translate(
@RequestParam String text,
@RequestParam String targetLanguage) {
String prompt = "Translate the following text to " + targetLanguage + ":\n\n" + text;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}
}6.2.2 多语言翻译
@GetMapping("/translate/multi")
public Map<String, String> multiLanguageTranslate(
@RequestParam String text,
@RequestParam List<String> targetLanguages) {
Map<String, String> translations = new HashMap<>();
for (String language : targetLanguages) {
String prompt = "Translate the following text to " + language + ":\n\n" + text;
String translation = chatClient.prompt()
.user(prompt)
.call()
.content();
translations.put(language, translation);
}
return translations;
}6.2.3 上下文翻译
@PostMapping("/translate/context")
public String contextTranslation(
@RequestParam String text,
@RequestParam String targetLanguage,
@RequestParam String context) {
String prompt = "Context: " + context + "\n\n" +
"Translate the following text to " + targetLanguage + " while maintaining the context:\n\n" + text;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.3 代码生成
6.3.1 基础代码生成
@RestController
public class CodeGenerationController {
private final ChatClient chatClient;
public CodeGenerationController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/generate-code")
public String generateCode(
@RequestParam String language,
@RequestParam String requirement) {
String prompt = "Generate " + language + " code for: " + requirement;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}
}6.3.2 函数生成
@GetMapping("/generate/function")
public String generateFunction(
@RequestParam String language,
@RequestParam String functionName,
@RequestParam String description) {
String prompt = "Generate a " + language + " function named '" + functionName +
"' that " + description;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.3.3 类生成
@PostMapping("/generate/class")
public String generateClass(
@RequestParam String language,
@RequestParam String className,
@RequestParam String description) {
String prompt = "Generate a " + language + " class named '" + className +
"' with the following functionality: " + description;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.3.4 代码解释
@PostMapping("/explain-code")
public String explainCode(
@RequestParam String language,
@RequestParam String code) {
String prompt = "Explain the following " + language + " code:\n\n" + code;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.4 文档分析
6.4.1 文档摘要
@RestController
public class DocumentAnalysisController {
private final ChatClient chatClient;
public DocumentAnalysisController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/analyze-document")
public DocumentAnalysis analyzeDocument(@RequestBody String document) {
String prompt = "Analyze the following document and provide:\n" +
"1. Summary\n" +
"2. Key points\n" +
"3. Main themes\n" +
"4. Action items\n\n" + document;
return chatClient.prompt()
.user(prompt)
.call()
.entity(DocumentAnalysis.class);
}
}
public class DocumentAnalysis {
private String summary;
private List<String> keyPoints;
private List<String> mainThemes;
private List<String> actionItems;
// getters and setters
}6.4.2 代码文档生成
@PostMapping("/generate-docs")
public String generateDocumentation(
@RequestParam String language,
@RequestParam String code) {
String prompt = "Generate comprehensive documentation for the following " + language + " code:\n\n" + code;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.4.3 API文档生成
@PostMapping("/generate-api-docs")
public String generateApiDocs(@RequestBody String apiDefinition) {
String prompt = "Generate API documentation for the following API definition:\n\n" + apiDefinition;
return chatClient.prompt()
.user(prompt)
.call()
.content();
}6.5 高级文本处理技术
6.5.1 情感分析
@GetMapping("/analyze/sentiment")
public SentimentAnalysis analyzeSentiment(@RequestBody String text) {
String prompt = "Analyze the sentiment of the following text:\n\n" + text +
"\nReturn sentiment (positive/negative/neutral) and confidence score.";
return chatClient.prompt()
.user(prompt)
.call()
.entity(SentimentAnalysis.class);
}
public class SentimentAnalysis {
private String sentiment;
private double confidence;
private String explanation;
// getters and setters
}6.5.2 文本分类
@GetMapping("/classify/text")
public TextClassification classifyText(
@RequestParam String text,
@RequestParam List<String> categories) {
String prompt = "Classify the following text into one of these categories: " +
String.join(", ", categories) + "\n\n" + text;
return chatClient.prompt()
.user(prompt)
.call()
.entity(TextClassification.class);
}
public class TextClassification {
private String category;
private double confidence;
private String reasoning;
// getters and setters
}6.5.3 问答系统
@PostMapping("/qa/system")
public QaResponse answerQuestion(
@RequestParam String question,
@RequestParam String context) {
String prompt = "Based on the following context, answer the question:\n\n" +
"Context: " + context + "\n\n" +
"Question: " + question;
return chatClient.prompt()
.user(prompt)
.call()
.entity(QaResponse.class);
}
public class QaResponse {
private String answer;
private String source;
private double confidence;
// getters and setters
}6.6 实际应用示例
6.6.1 内容生成器
@Service
public class ContentGenerator {
private final ChatClient chatClient;
public ContentGenerator(ChatClient chatClient) {
this.chatClient = chatClient;
}
public Content generateContent(
String topic,
String format,
int length,
String style) {
String prompt = "Generate " + format + " content about " + topic +
" with approximately " + length + " words in " + style + " style";
String content = chatClient.prompt()
.user(prompt)
.call()
.content();
return new Content(topic, format, content);
}
}
public class Content {
private String topic;
private String format;
private String content;
// getters and setters
}6.6.2 代码审查工具
@Service
public class CodeReviewer {
private final ChatClient chatClient;
public CodeReviewer(ChatClient chatClient) {
this.chatClient = chatClient;
}
public CodeReview reviewCode(String code, String language) {
String prompt = "Review the following " + language + " code and provide:\n" +
"1. Code quality assessment\n" +
"2. Potential issues\n" +
"3. Improvement suggestions\n\n" + code;
return chatClient.prompt()
.user(prompt)
.call()
.entity(CodeReview.class);
}
}
public class CodeReview {
private String qualityAssessment;
private List<String> issues;
private List<String> suggestions;
// getters and setters
}6.7 性能优化
6.7.1 批量处理
@RestController
public class BatchProcessingController {
private final ChatClient chatClient;
public BatchProcessingController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/process/batch")
public List<String> batchProcess(@RequestBody List<String> texts) {
return texts.stream()
.map(text -> chatClient.prompt()
.user("Process: " + text)
.call()
.content())
.collect(Collectors.toList());
}
}6.7.2 缓存策略
@Configuration
@EnableCaching
public class TextProcessingCacheConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(
new ConcurrentMapCache("text-summaries"),
new ConcurrentMapCache("translations"),
new ConcurrentMapCache("code-generations")
));
return cacheManager;
}
}6.8 错误处理
6.8.1 文本处理异常
@RestControllerAdvice
public class TextProcessingExceptionHandler {
@ExceptionHandler(TextProcessingException.class)
public ResponseEntity<ErrorResponse> handleTextProcessingError(TextProcessingException ex) {
ErrorResponse error = new ErrorResponse(
"TEXT_PROCESSING_ERROR",
"文本处理失败: " + ex.getMessage()
);
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
}6.8.2 超长文本处理
@Service
public class LargeTextProcessor {
private final ChatClient chatClient;
public LargeTextProcessor(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String processLargeText(String text) {
if (text.length() > 4000) {
// 分块处理
return processInChunks(text);
}
return chatClient.prompt()
.user("Process: " + text)
.call()
.content();
}
private String processInChunks(String text) {
// 实现分块处理逻辑
return "Processed large text";
}
}6.9 监控与日志
6.9.1 文本处理指标
@RestController
public class TextProcessingMetricsController {
private final MeterRegistry meterRegistry;
public TextProcessingMetricsController(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@GetMapping("/text-processing/metrics")
public Map<String, Object> getMetrics() {
return Map.of(
"totalProcessing", meterRegistry.counter("text.processing.total").count(),
"averageProcessingTime", meterRegistry.timer("text.processing.time").mean(),
"errorRate", meterRegistry.counter("text.processing.errors").count() /
(meterRegistry.counter("text.processing.total").count() + 1.0)
);
}
}6.9.2 文本处理日志
@Configuration
public class TextProcessingLogging {
@Bean
public ChatClient loggingChatClient(ChatClient chatClient) {
return new ChatClient() {
@Override
public ChatResponse call(Prompt prompt) {
long startTime = System.currentTimeMillis();
try {
ChatResponse response = chatClient.call(prompt);
long duration = System.currentTimeMillis() - startTime;
log.info("文本处理 - 耗时: {}ms, Prompt: {}", duration, prompt);
return response;
} catch (Exception ex) {
log.error("文本处理失败", ex);
throw ex;
}
}
// 实现其他方法...
};
}
}6.10 小结
本章详细介绍了文本处理与生成的高级技术:
- 文本摘要:掌握不同长度和风格的摘要生成
- 内容翻译:学习基础翻译和多语言翻译
- 代码生成:了解函数、类和代码解释的生成
- 文档分析:掌握文档摘要和API文档生成
- 高级技术:学习情感分析、文本分类等高级功能
- 实际应用:了解内容生成器和代码审查工具
- 性能优化:掌握批量处理和缓存策略
- 错误处理:了解文本处理异常和超长文本处理
- 监控日志:掌握文本处理指标和日志记录
在下一章中,我们将学习智能对话系统的开发。 点击这里👇🏻获取:100万QPS短链系统、复杂的商城微服务系统、智能翻译助手AI Agent、SaaS点餐系统、刷题吧小程序、商城系统、秒杀系统、AI项目、代码生成神器、苏三demo项目、智能天气播报AI Agent、智能代码审查AI Agent等 10 个项目的:项目源代码、开发教程和技术答疑
