Skip to content

Latest commit

 

History

History
151 lines (104 loc) · 4.47 KB

README_en.md

File metadata and controls

151 lines (104 loc) · 4.47 KB

ChatGPT Java Api

stable Maven Central

简体中文文档.

SDK for OpenAI ChatGPT. If you find it helpful, please give it a star in the upper right corner.

Features

Feature Support
GPT 3.5 Supported
GPT 4.0 Supported
GPT 4.0-32k Supported
Streaming Dialog Supported
Blocking Dialog Supported
Front-end None
Context Supported
Token Compute Coming Soon
Multiple Keys Supported
Proxy Supported
Reverse Proxy Supported

image

image

USE

maven

<dependency>
    <groupId>com.github.plexpt</groupId>
    <artifactId>chatgpt</artifactId>
    <version>4.0.1</version>
</dependency>

gradle

implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.0.1'

Quick Start


       ChatGPT chatGPT = ChatGPT.builder()
                .apiKey("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")
                .build()
                .init();
                
        String res = chatGPT.chat("hello!");
        System.out.println(res);

Advanced Usage

   //Proxy is required in some contry
      Proxy proxy = Proxys.http("127.0.0.1", 1080);

      ChatGPT chatGPT = ChatGPT.builder()
                .apiKey("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")
                .proxy(proxy)
                .timeout(900)
                .apiHost("https://api.openai.com/") //Reverse proxy address
                .build()
                .init();
  
        Message system = Message.ofSystem("You are now a poet, specializing in writing seven character quatrains");
        Message message = Message.of("Write a seven-character quatrain poem titled: Future!");

        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
                .messages(Arrays.asList(system, message))
                .maxTokens(3000)
                .temperature(0.9)
                .build();
        ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
        Message res = response.getChoices().get(0).getMessage();
        System.out.println(res);

Streaming Usage

 

      ChatGPTStream chatGPTStream = ChatGPTStream.builder()
                .timeout(600)
                .apiKey("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")
                .build()
                .init();

                
        ConsoleStreamListener listener = new ConsoleStreamListener();
        Message message = Message.of("Write a seven-character quatrain poem titled: Future!");
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .messages(Arrays.asList(message))
                .build();
        chatGPTStream.streamChatCompletion(chatCompletion, listener);

Using Spring SseEmitter with Streaming

Refer to SseStreamListener

    @GetMapping("/chat/sse")
    @CrossOrigin
    public SseEmitter sseEmitter(String prompt) {
     

       ChatGPTStream chatGPTStream = ChatGPTStream.builder()
                .timeout(600)
                .apiKey("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")
                .apiHost("https://api.openai.com/")
                .build()
                .init();
        
        SseEmitter sseEmitter = new SseEmitter(-1L);

        SseStreamListener listener = new SseStreamListener(sseEmitter);
        Message message = Message.of(prompt);

        listener.setOnComplate(msg -> {
            //The answer is complete, do something 
        });
        chatGPTStream.streamChatCompletion(Arrays.asList(message), listener);


        return sseEmitter;
    }

Star History

Star History Chart