当前位置:   article > 正文

SpringAI初体验----拒绝繁杂请求构造,即开即用

SpringAI初体验----拒绝繁杂请求构造,即开即用

        使用这个框架可以让你省去一些对ChatGPT的一些网络请求构造,这个框架集成了一些基础的请求方法,只需要调用方法就可以实现调用ChatGPT的接口了,但是对于当前网络环境使用ChatGPT确实比较尴尬,不过可以使用CloudFlare对中的Workers功能对请求进行转发,使用购买的域名进行替换即可,直接搜索CSDN就可以看到答案。使用此方法需要在配置文件中多配置一个代理域名,表示使用这个域名替换原来openai的域名。

        第一步引入依赖(本文使用的Java17,Springboot版本是3.2.3)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.2.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.wxy</groupId>
  12. <artifactId>ai</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>ai</name>
  15. <description>ai</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <!-- 配置仓库,如果不生效,就把原来Maven中的Mirror给注释了,就成功了-->
  20. <repositories>
  21. <repository>
  22. <id>spring-milestones</id>
  23. <name>Spring Milestones</name>
  24. <url>https://repo.spring.io/milestone</url>
  25. <snapshots>
  26. <enabled>false</enabled>
  27. </snapshots>
  28. </repository>
  29. <repository>
  30. <id>spring-snapshots</id>
  31. <name>Spring Snapshots</name>
  32. <url>https://repo.spring.io/snapshot</url>
  33. <releases>
  34. <enabled>false</enabled>
  35. </releases>
  36. </repository>
  37. </repositories>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.ai</groupId>
  42. <artifactId>spring-ai-bom</artifactId>
  43. <version>0.8.1</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>
  49. <dependencies>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-web</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-actuator</artifactId>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.springframework.ai</groupId>
  60. <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-devtools</artifactId>
  65. <scope>runtime</scope>
  66. <optional>true</optional>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-configuration-processor</artifactId>
  71. <optional>true</optional>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.projectlombok</groupId>
  75. <artifactId>lombok</artifactId>
  76. <optional>true</optional>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.springframework.boot</groupId>
  80. <artifactId>spring-boot-starter-test</artifactId>
  81. <scope>test</scope>
  82. </dependency>
  83. </dependencies>
  84. <build>
  85. <plugins>
  86. <plugin>
  87. <groupId>org.springframework.boot</groupId>
  88. <artifactId>spring-boot-maven-plugin</artifactId>
  89. <configuration>
  90. <excludes>
  91. <exclude>
  92. <groupId>org.projectlombok</groupId>
  93. <artifactId>lombok</artifactId>
  94. </exclude>
  95. </excludes>
  96. </configuration>
  97. </plugin>
  98. </plugins>
  99. </build>
  100. </project>

创建application.yaml进行简单配置:

  1. spring:
  2. application:
  3. name: spring-ai
  4. ai:
  5. openai:
  6. api-key: 在openai官网进行获取
  7. base-url: 填入代理域名
  8. server:
  9. port: 8083

经过以上的操作就可以直接使用了,直接编写Controller:

  1. package com.wxy.ai.controller;
  2. import org.springframework.ai.chat.ChatClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.Map;
  8. @RestController
  9. public class SimpleAiController {
  10. private final ChatClient chatClient;
  11. @Autowired
  12. public SimpleAiController(ChatClient chatClient) {
  13. this.chatClient = chatClient;
  14. }
  15. @GetMapping("/ai/simple")
  16. public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
  17. return Map.of("generation", chatClient.call(message));
  18. }
  19. }

效果如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/611791
推荐阅读
相关标签
  

闽ICP备14008679号