package com.producthunt.server; import java.util.ArrayList; import java.util.List; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.chat.prompt.PromptTemplate; import org.springframework.ai.chat.prompt.SystemPromptTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; import org.springframework.graphql.client.GraphQlClient; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.producthunt.server.core.config.ProductHuntConfig; import com.producthunt.server.feign.ProductHuntFeign; import com.producthunt.server.feign.dto.OauthRequest; import com.producthunt.server.feign.dto.OauthResponse; /** * @author tyuio * @version 1.0.0 * @description TODO * @date 2025/2/17 19:17 */ @RestController @RequestMapping("/test") public class TestController { @Autowired private ProductHuntFeign productHuntFeign; @Autowired private ProductHuntConfig productHuntConfig; @GetMapping("/t1") public OauthResponse t1() { OauthRequest oauthRequest = new OauthRequest(); oauthRequest.setClientId(productHuntConfig.getClientId()); oauthRequest.setClientSecret(productHuntConfig.getClientSecret()); return productHuntFeign.getOauthToken(oauthRequest); } @Autowired private GraphQlClient graphQlClient; /** * Product Hunt 查询模板(按投票数排序,限定时间范围) */ private final String queryTemplate = """ { posts(order: VOTES, postedAfter: "%sT00:00:00Z", postedBefore: "%sT23:59:59Z", after: "%s") { nodes { id name tagline description votesCount createdAt featuredAt website url } pageInfo { hasNextPage endCursor } } } """; @GetMapping("/t2") public List t2() { List posts = new ArrayList<>(); boolean hasNextPage = true; String endCursor = ""; String dateStr = "2025-02-22"; while (hasNextPage) { // 设置查询模板并发起查询,好像无法直接一次性查询指定数量,只能分页查询并且要自己控制所需数量 String queryDocument = String.format(queryTemplate, dateStr, dateStr, endCursor); PostResponse postResponse = graphQlClient.document(queryDocument).retrieveSync("posts").toEntity(PostResponse.class); // 把获取到的帖子信息放入容器 if (!CollectionUtils.isEmpty(postResponse.getNodes())) { posts.addAll(postResponse.getNodes()); } // 帖子数量大于30则退出 if (posts.size() > 30) { break; } // 没有下一页了则跳出循环 if (!postResponse.getPageInfo().getHasNextPage()) { break; } hasNextPage = postResponse.getPageInfo().getHasNextPage(); } return posts.subList(0, 30); } @Autowired private ChatClient.Builder chatClientBuilder; // 通过@Value注解注入资源文件 @Value("classpath:/prompts/system-message.st") private Resource promptSystemMessage; @GetMapping("/t3") public String t3() { // // 1. 定义用户消息模板 String userText = """ 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! 🚀 """; //// 创建用户提示模板对象 // PromptTemplate userPromptTemplate = new PromptTemplate(userText); //// 根据模板和参数创建用户消息 // Message userMessage = userPromptTemplate.createMessage(); // //// 创建系统提示模板对象 // SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(promptSystemMessage); //// 根据模板和参数创建系统消息 // Message systemMessage = systemPromptTemplate.createMessage(); //// 创建包含其他消息和系统消息的提示 // Prompt prompt = new Prompt(List.of(userMessage, systemMessage)); // 3. 创建包含用户消息和系统消息的提示 // 单个提问 ChatClient chatClient = chatClientBuilder.defaultSystem(promptSystemMessage).build(); return chatClient.prompt().user(userText).call().content(); } }