Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对新手比较好用的 Java JDK 11 版本 #3

Closed
blueokanna opened this issue Feb 4, 2024 · 0 comments
Closed

对新手比较好用的 Java JDK 11 版本 #3

blueokanna opened this issue Feb 4, 2024 · 0 comments
Assignees

Comments

@blueokanna
Copy link

blueokanna commented Feb 4, 2024

这边其实我使用官方的 SDK 感觉不太好用,我自己也写了一个个人认为好用的 SDK, 是基于 JDK 11 版本的。当然和小傅哥版本不一样,我不使用 Springboot, 用简单的 Maven 就可以实现,目前也是支持三种请求方式,同时也支持到 ChatGLM-4 的版本,默认是有角色扮演的 prompt,最后还是得感谢一下智谱官方的这个项目 😆

首先是 Github 地址: ChatGLM-Java-SDK | Blueokanna

下面是使用关于我的ChatGLM-4 Java SDK不同请求的示例代码:

调用异步请求,示例代码如下:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String apiKeyss = loadApiKey();                          //load api key if exist

        if (apiKeyss == null) {                                  //if api key is not exist create txt file to store key in local file
            System.out.println("Enter your API key:");
            apiKeyss = scanner.nextLine();
            saveApiKey(apiKeyss);
        }
        while (scanner.hasNext()) {
            String userInput = scanner.nextLine();

            ChatClient chats = new ChatClient(apiKeyss);      //Initial ChatClient (Instantiation)
            chats.registerShutdownHook();                     //Delete History File for Your Chatting
            chats.AsyncInvoke(userInput);                     //Assign the question you entered to the synchronised request
            System.out.print(chats.getResponseMessage());  //Print out ChatGLM's response
            System.out.println();
        }
    }

调用SSE请求,示例代码如下:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String apiKeyss = loadApiKey();                          //load api key if exist

        if (apiKeyss == null) {                                  //if api key is not exist create txt file to store key in local file
            System.out.println("Enter your API key:");
            apiKeyss = scanner.nextLine();
            saveApiKey(apiKeyss);
        }
        while (scanner.hasNext()) {
            String userInput = scanner.nextLine();

            ChatClient chats = new ChatClient(apiKeyss);      //Initial ChatClient (Instantiation)
            chats.registerShutdownHook();                     //Delete History File for Your Chatting
            chats.SSEInvoke(userInput);                     //Assign the question you entered to the synchronised request
            System.out.print(chats.getResponseMessage());  //Print out ChatGLM's response
            System.out.println();
        }
    }

调用同步请求,示例代码如下:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String apiKeyss = loadApiKey();                          //load api key if exist

        if (apiKeyss == null) {                                  //if api key is not exist create txt file to store key in local file
            System.out.println("Enter your API key:");
            apiKeyss = scanner.nextLine();
            saveApiKey(apiKeyss);
        }
        while (scanner.hasNext()) {
            String userInput = scanner.nextLine();

            ChatClient chats = new ChatClient(apiKeyss);      //Initial ChatClient (Instantiation)
            chats.registerShutdownHook();                     //Delete History File for Your Chatting
            chats.SyncInvoke(userInput);                     //Assign the question you entered to the synchronised request
            System.out.print(chats.getResponseMessage());  //Print out ChatGLM's response
            System.out.println();
        }
    }

更多详情可以点击链接去我的项目了解:ChatGLM-Java-SDK | v0.1.1- Beta

基于 Rust 语言的 SDK:RustGLM | v0.1.2

欢迎大家来 fork 我的项目或者分享我的项目,在这里先谢谢各位,如果有什么使用上的 bug 可以在项目的 issue 发起,我这边会尽可能修复,最后不介意的话也可以给我的项目点个⭐️ ( ̄▽ ̄)~■干杯

@zRzRzRzRzRzRzR zRzRzRzRzRzRzR self-assigned this Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants