软件工程日报

几天制作了一个调用百度千帆大模型api的程序,能够根据输入的关键字自动生成故事文本以及插图,并且将插图保存在本地;参考的是百度千帆的官方文档,通过shell进行调用
这是application.properties

点击查看代码
spring.application.name=children


spring.datasource.url=jdbc:mysql://localhost:3306/story_generator?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.children.entity


bai.api.url=https://qianfan.baidubce.com/v2/chat/completions
bai.api.appid=app-UrBDmV3b
bai.api.authorization=Bearer bce-v3/ALTAK-CehVCpeNnYeFKKGViLHjO/d3ece4819ea77f76c35ef30d2244d1bf95bd7df4
bai.api.model=ernie-4.5-turbo-128k
下面是story.controller层
点击查看代码
package com.example.children.controller;

import com.example.children.entity.Story;
import com.example.children.service.StoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/stories")
public class StoryController {
    
    @Autowired
    private StoryService storyService;
    
    // 根据关键字生成故事
    @PostMapping("/generate")
    public ResponseEntity<?> generateStory(@RequestBody Map<String, String> request) {
        String keywords = request.get("keywords");
        String additionalInfo = request.get("additionalInfo");
        
        if (keywords == null || keywords.trim().isEmpty()) {
            Map<String, String> errorResponse = new HashMap<>();
            errorResponse.put("message", "请至少输入一个关键词");
            return ResponseEntity.badRequest().body(errorResponse);
        }
        
        try {
            // 调用service生成故事,传入关键词和额外要求
            Story story = storyService.generateStory(keywords + (additionalInfo != null && !additionalInfo.trim().isEmpty() ? " " + additionalInfo : ""));
            return ResponseEntity.ok(story);
        } catch (Exception e) {
            Map<String, String> errorResponse = new HashMap<>();
            errorResponse.put("message", "生成故事失败: " + e.getMessage());
            return ResponseEntity.status(500).body(errorResponse);
        }
    }
    
    // 获取所有故事
    @GetMapping
    public ResponseEntity<List<Story>> getAllStories() {
        List<Story> stories = storyService.getAllStories();
        return ResponseEntity.ok(stories);
    }
    
    // 根据ID获取故事
    @GetMapping("/{id}")
    public ResponseEntity<Story> getStoryById(@PathVariable Long id) {
        Story story = storyService.getStoryById(id);
        if (story == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(story);
    }
    
    // 根据关键字搜索故事
    @GetMapping("/search")
    public ResponseEntity<?> searchStories(@RequestParam String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return ResponseEntity.ok(storyService.getAllStories());
        }
        try {
            List<Story> stories = storyService.searchStoriesByKeywords(keyword);
            return ResponseEntity.ok(stories);
        } catch (Exception e) {
            Map<String, String> errorResponse = new HashMap<>();
            errorResponse.put("message", "搜索故事失败: " + e.getMessage());
            return ResponseEntity.status(500).body(errorResponse);
        }
    }
}
posted @ 2025-11-11 16:22  一只虎鲸  阅读(8)  评论(0)    收藏  举报