iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring Boot项目中使用OpenAI-Java的示例详解
  • 951
分享到

Spring Boot项目中使用OpenAI-Java的示例详解

Spring Boot使用OpenAI-JavaSpring Boot OpenAI-Java 2023-05-17 11:05:21 951人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录前言准备工作集成达芬奇模型效果展示前言 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定

前言

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

准备工作

1、初始化一个SpringBoot项目

参考地址:https://www.jb51.net/article/232551.htm

2、访问OPENai官网获取API密钥

地址:Https://platfORM.openai.com/account/api-keys

3、通过OPENA开源的JAVA SDK (OpenAI-Java)访问 API

地址:GitHub - TheoKanning/openai-java: OpenAI GPT-3 Api Client in Java

集成达芬奇模型

1、编写SpringBoot项目中的pom文件

<dependency>
   <groupId>com.theokanning.openai-gpt3-java</groupId>
   <artifactId>client</artifactId>
   <version>0.9.0</version>
</dependency>

2、初始化OpenAiService类

import com.theokanning.openai.OpenAiService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.time.Duration;
 

@Configuration
public class OpenAiConfiguration {
 
    @Value("${open.ai.key}")
    private String openAiKey;
    @Value("${open.ai.request.timeout}")
    private long timeout;
    
    @Bean
    public OpenAiService openAiService(){
        return new OpenAiService(openAiKey, Duration.ofSeconds(timeout));
    }
}

3、配置密钥、超时时间和使用的模型

#application.properties
server.port=8081
#密钥
open.ai.key=xxxxxxxx
#超时时间
open.ai.request.timeout=100000
#达芬奇模型
open.ai.model=text-davinci-003

3、编写访问业务类

import com.Google.common.collect.Maps;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.util.Arrays;
import java.util.Map;
 
@Slf4j
@Service
public class OpenAiChatBiz {
 
    @Value("${open.ai.model}")
    private String openAiModel;
    @Autowired
    private OpenAiService openAiService;
    
    public String chat(String prompt){
        CompletionRequest completionRequest = CompletionRequest.builder()
                .prompt(prompt)
                .model(openAiModel)
                .echo(true)
                .temperature(0.7)
                .topP(1d)
                .frequencyPenalty(0d)
                .presencePenalty(0d)
                .maxTokens(1000)
                .build();
        CompletionResult completionResult = openAiService.createCompletion(completionRequest);
        String text = completionResult.getChoices().get(0).getText();
        return text;
    }
}

4、编写访问接口

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.WEB.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class OpenAiChatApi {
 
    @Autowired
    private OpenAiChatBiz openAiChatBiz;
 
    @RequestMapping(path = "/chat/question",method = RequestMethod.GET)
    public String openAiChat(@RequestParam("question")String question){
        if(StringUtils.isBlank(question)){
            return "Please Input";
        }
        return openAiChatBiz.chat(question);
    }
}

效果展示

使用google的API Tester插件进行测试

到此这篇关于Spring Boot项目中使用OpenAI-Java的示例详解的文章就介绍到这了,更多相关Spring Boot使用OpenAI-Java内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring Boot项目中使用OpenAI-Java的示例详解

本文链接: https://www.lsjlt.com/news/211132.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作