TestController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.producthunt.server;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.ai.chat.client.ChatClient;
  5. import org.springframework.ai.chat.messages.Message;
  6. import org.springframework.ai.chat.prompt.Prompt;
  7. import org.springframework.ai.chat.prompt.PromptTemplate;
  8. import org.springframework.ai.chat.prompt.SystemPromptTemplate;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.graphql.client.GraphQlClient;
  13. import org.springframework.util.CollectionUtils;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import com.producthunt.server.core.config.ProductHuntConfig;
  18. import com.producthunt.server.feign.ProductHuntFeign;
  19. import com.producthunt.server.feign.dto.OauthRequest;
  20. import com.producthunt.server.feign.dto.OauthResponse;
  21. /**
  22. * @author tyuio
  23. * @version 1.0.0
  24. * @description TODO
  25. * @date 2025/2/17 19:17
  26. */
  27. @RestController
  28. @RequestMapping("/test")
  29. public class TestController {
  30. @Autowired
  31. private ProductHuntFeign productHuntFeign;
  32. @Autowired
  33. private ProductHuntConfig productHuntConfig;
  34. @GetMapping("/t1")
  35. public OauthResponse t1() {
  36. OauthRequest oauthRequest = new OauthRequest();
  37. oauthRequest.setClientId(productHuntConfig.getClientId());
  38. oauthRequest.setClientSecret(productHuntConfig.getClientSecret());
  39. return productHuntFeign.getOauthToken(oauthRequest);
  40. }
  41. @Autowired
  42. private GraphQlClient graphQlClient;
  43. /**
  44. * Product Hunt 查询模板(按投票数排序,限定时间范围)
  45. */
  46. private final String queryTemplate = """
  47. {
  48. posts(order: VOTES, postedAfter: "%sT00:00:00Z", postedBefore: "%sT23:59:59Z", after: "%s") {
  49. nodes {
  50. id
  51. name
  52. tagline
  53. description
  54. votesCount
  55. createdAt
  56. featuredAt
  57. website
  58. url
  59. }
  60. pageInfo {
  61. hasNextPage
  62. endCursor
  63. }
  64. }
  65. }
  66. """;
  67. @GetMapping("/t2")
  68. public List<PostNode> t2() {
  69. List<PostNode> posts = new ArrayList<>();
  70. boolean hasNextPage = true;
  71. String endCursor = "";
  72. String dateStr = "2025-02-22";
  73. while (hasNextPage) {
  74. // 设置查询模板并发起查询,好像无法直接一次性查询指定数量,只能分页查询并且要自己控制所需数量
  75. String queryDocument = String.format(queryTemplate, dateStr, dateStr, endCursor);
  76. PostResponse postResponse =
  77. graphQlClient.document(queryDocument).retrieveSync("posts").toEntity(PostResponse.class);
  78. // 把获取到的帖子信息放入容器
  79. if (!CollectionUtils.isEmpty(postResponse.getNodes())) {
  80. posts.addAll(postResponse.getNodes());
  81. }
  82. // 帖子数量大于30则退出
  83. if (posts.size() > 30) {
  84. break;
  85. }
  86. // 没有下一页了则跳出循环
  87. if (!postResponse.getPageInfo().getHasNextPage()) {
  88. break;
  89. }
  90. hasNextPage = postResponse.getPageInfo().getHasNextPage();
  91. }
  92. return posts.subList(0, 30);
  93. }
  94. @Autowired
  95. private ChatClient.Builder chatClientBuilder;
  96. // 通过@Value注解注入资源文件
  97. @Value("classpath:/prompts/system-message.st")
  98. private Resource promptSystemMessage;
  99. @GetMapping("/t3")
  100. public String t3() {
  101. // // 1. 定义用户消息模板
  102. String userText = """
  103. Enter a prompt, set your preferences, and let AI build a high-converting, fully optimized eCommerce store in minutes—no coding, no design skills, no hassle. Launch your store effortlessly and start selling instantly! 🚀
  104. """;
  105. //// 创建用户提示模板对象
  106. // PromptTemplate userPromptTemplate = new PromptTemplate(userText);
  107. //// 根据模板和参数创建用户消息
  108. // Message userMessage = userPromptTemplate.createMessage();
  109. //
  110. //// 创建系统提示模板对象
  111. // SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(promptSystemMessage);
  112. //// 根据模板和参数创建系统消息
  113. // Message systemMessage = systemPromptTemplate.createMessage();
  114. //// 创建包含其他消息和系统消息的提示
  115. // Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
  116. // 3. 创建包含用户消息和系统消息的提示
  117. // 单个提问
  118. ChatClient chatClient = chatClientBuilder.defaultSystem(promptSystemMessage).build();
  119. return chatClient.prompt().user(userText).call().content();
  120. }
  121. }