Skip to content

Realtime chat #8

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

Merged
merged 3 commits into from
Mar 24, 2023
Merged

Realtime chat #8

merged 3 commits into from
Mar 24, 2023

Conversation

CJCrafter
Copy link
Owner

Allows you to get responses in real time. This is done using OpenAI's stream=true option. This will let users see responses without waiting for the entire message to generate.

Java

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String key = Dotenv.load().get("OPENAI_TOKEN");

        // Create the initial prompt, we will reuse it later.
        String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
        List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
        ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
        ChatBot bot = new ChatBot(key);

        while (true) {
            System.out.println("Enter text below:\n\n");
            String input = scan.nextLine();

            // Generate a response, and print it to the user.
            messages.add(new ChatMessage(ChatUser.USER, input));
            bot.streamResponse(request, message -> {
                System.out.print(message.get(0).getDelta());

                if (message.get(0).getFinishReason() != null) {
                    messages.add(message.get(0).getMessage());
                }
            });
        }
    }

Kotlin

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)
    val key = dotenv()["OPENAI_TOKEN"]

    // Create the initial prompt, we will reuse it later.
    val initialPrompt = "Follow the users instructions"
    val messages = mutableListOf(initialPrompt.toSystemMessage())
    val request = ChatRequest("gpt-3.5-turbo", messages)
    val bot = ChatBot(key)

    while (true) {
        println("Enter text below:\n")
        val input = scan.nextLine()

        // Generate a response, and print it to the user.
        messages.add(input.toUserMessage())
        bot.streamResponseKotlin(request) {
            print(choices[0].delta)

            if (choices[0].finishReason != null) {
                messages.add(choices[0].message)
            }
        }
    }
}

@CJCrafter CJCrafter merged commit 78a8af0 into master Mar 24, 2023
@CJCrafter CJCrafter deleted the realtime-chat branch March 24, 2023 16:28
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

Successfully merging this pull request may close these issues.

1 participant