当前位置:   article > 正文

spring Ai框架调用openai大模型简易demo_spring ai demo

spring ai demo

前提
需要科学上网,key没有官方的,就找中转的key

1 pom依赖,注意添加的依赖和仓库配置

  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.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>spring_openAi</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring_openAi</name>
  15. <description>spring_openAi</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. <spring-ai.version>0.8.1</spring-ai.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.ai</groupId>
  27. <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-devtools</artifactId>
  32. <scope>runtime</scope>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. <optional>true</optional>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-test</artifactId>
  43. <scope>test</scope>
  44. </dependency>
  45. </dependencies>
  46. <dependencyManagement>
  47. <dependencies>
  48. <dependency>
  49. <groupId>org.springframework.ai</groupId>
  50. <artifactId>spring-ai-bom</artifactId>
  51. <version>${spring-ai.version}</version>
  52. <type>pom</type>
  53. <scope>import</scope>
  54. </dependency>
  55. </dependencies>
  56. </dependencyManagement>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. <configuration>
  63. <excludes>
  64. <exclude>
  65. <groupId>org.projectlombok</groupId>
  66. <artifactId>lombok</artifactId>
  67. </exclude>
  68. </excludes>
  69. </configuration>
  70. </plugin>
  71. </plugins>
  72. </build>
  73. <repositories>
  74. <repository>
  75. <id>spring-milestones</id>
  76. <name>Spring Milestones</name>
  77. <url>https://repo.spring.io/milestone</url>
  78. <snapshots>
  79. <enabled>false</enabled>
  80. </snapshots>
  81. </repository>
  82. </repositories>
  83. </project>

2 yml配置,如果是官方key,base-url就不用填了

  1. spring:
  2. ai:
  3. openai:
  4. base-url: https://xxxxx
  5. api-key: xxxxxxx
  6. chat:
  7. options:
  8. model: gpt-3.5-turbo
  9. temperature: 0.6

3 测试调用

  1. import org.springframework.ai.chat.ChatResponse;
  2. import org.springframework.ai.chat.prompt.Prompt;
  3. import org.springframework.ai.openai.OpenAiChatClient;
  4. import org.springframework.ai.openai.OpenAiChatOptions;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("/openai")
  10. public class AiController_1 {
  11. @Autowired
  12. private OpenAiChatClient openAiChatClient;
  13. @RequestMapping("/chart_1")
  14. public Object chart_1() {
  15. String call = openAiChatClient.call("随便说点什么,10个字以内");
  16. return call;
  17. }
  18. @RequestMapping("/chart_2")
  19. public Object chart_2(String message) {
  20. //代码中配置,会覆盖application.yml中的配置
  21. Prompt prompt = new Prompt(
  22. message,
  23. OpenAiChatOptions.builder()
  24. .withModel("gpt-3.5-turbo") //大模型用哪个
  25. .withTemperature(0.9f) //温度高,更发散,准确性降低,温度低,更保守,准确性高
  26. .build());
  27. ChatResponse call = openAiChatClient.call(prompt);
  28. return call.getResult().getOutput();
  29. }
  30. }


 

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

闽ICP备14008679号