从0到1使用chatGPT以及简单调用JAVA api实现网页的交互

前言

  本文探讨的是从0到1,如何注册GPT账号,如何编写提示词(prompt),以及简单的java调用api的简单例子,如果有对GPT感兴趣,但是不知道如何开始上手的小伙伴可以看下这篇文章

注册过程

1、你能正常访问到官网:https://openai.com/
2、准备可用的国外邮箱 例如 gmail microsoft 邮箱等等。截止到目前qq邮箱,163等国内邮箱都是不可用的状态
3、准备可用的接受验证码的手机 (例如通过:sms-active)

  以下是注册过程的截图,当前我使用的印度的手机号码亲自测试可以使用

在这里插入图片描述
在这里插入图片描述
4、登陆GPT官网,点击注册,使用海外邮箱,和你购买的手机号,经过短信验证后,你就可以正常使用GPT3.5了
在这里插入图片描述

如何使得提示词更加的准确?

  如图见官网的描述,有以下6个基本测策略。例如:

差的提示词:请给我一段描述汽车的文章
好的提示词:请给我一段描述保时捷跑车的中文文章,文章50个词左右

  简单来说,就是提供更精准的信息,从而大模型才会有更好的相应的匹配信息,而不是让AI去猜你想要的
在这里插入图片描述

api的简单调用

在这里插入图片描述

  看到这里的小伙伴,肯定已经猜到,不就是给open-ai的服务器发送特定格式的http请求,然后我们去解析或者展示respond给我们的信息嘛,所以接下来我们来看下open-ai大致提供了哪些接口,以及如何编写java程序如何去请求这些接口的

扫描二维码关注公众号,回复: 17075087 查看本文章

  前提:要申请密钥sk
在这里插入图片描述

用httpclient去手动填充请求头

  第一种是手动使用HttpClients去构建请求对象,当然pom需要引入httpclient的依赖

       <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>XXX</version>
        </dependency>
//问:请告诉我java序列化的方式
    @RequestMapping("/chat")
    public void streamChat()throws IOException {
    
    
        CloseableHttpClient httpclient = HttpClients.createDefault();

        try {
    
    
            HttpPost httpPost = new HttpPost("https://api.openai.com/v1/chat/completions");

            StringEntity requestEntity = new StringEntity(
                    "{\"model\": \"gpt-3.5-turbo\",\"messages\": [{ \"role\" : \"user\" , \"content\":  \"请告诉我java序列化的方式\"}], \"temperature\": 0.7, \"max_tokens\": 100}",
                    "UTF-8"
            );

            requestEntity.setContentType("application/json");
            //如果有用梯子 代理的端口 ip  
            HttpHost proxy = new HttpHost("代理ip", 代理端口);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setProxy(proxy)
                    .setConnectTimeout(10000)
                    .setSocketTimeout(10000)
                    .setConnectionRequestTimeout(3000)
                    .build();

            httpPost.setConfig(requestConfig);
            httpPost.setEntity(requestEntity);

            httpPost.addHeader("Authorization", "Bearer sk-你的密钥");
            httpPost.addHeader("Content-Type", "application/json");

            CloseableHttpResponse response = httpclient.execute(httpPost);

            try {
    
    
                HttpEntity entity = response.getEntity();
                if (entity != null) {
    
    
                    String result = EntityUtils.toString(entity);
                    System.out.println(result);
                }
            } finally {
    
    
                response.close();
            }
        } finally {
    
    
            httpclient.close();
        }

    }

现成封装的方法

  例如官网指明的的java社区包
TheoKanning/openai-java
在这里插入图片描述

  我发现了一个更好用更简洁的朋友二次封装的包,这是他的项目链接:
https://github.com/asleepyfish/chatgpt
  你可以尝试下,引入这个项目的依赖,然后去测试

    @RestController
    public class ChatGPTController {
    
    
        @GetMapping("/downloadImage")
        public void downloadImage(String prompt, HttpServletResponse response) {
    
    
            OpenAiUtils.downloadImage(prompt, response);
        }
    }

    @GetMapping("/streamChatWithWeb")
    public void streamChatWithWeb(String content, HttpServletResponse response) throws IOException {
    
    
        // 需要指定response的ContentType为流式输出,且字符编码为UTF-8
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        OpenAiUtils.createStreamChatCompletion(content, response.getOutputStream());
    }

最终效果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44716086/article/details/131849308