Different returns when usage of stream

Hello everyone, I am a beginner in using OpenAI API and I’m not a native English speaker. Please bear with me if there are any mistakes.

I am currently using the Azure OpenAI client library for Java (version 1.0.0-beta.3) with the gpt-3.5-turbo model. I have been following the examples in the documentation for both Chat Completions and Streaming Chat Completions to make requests, but the responses I’m receiving are different.

As far as I understand, streaming should only affect the format of the response and not the content itself. I’m struggling to grasp the underlying principles, and I’m hoping someone could help me understand.

Thank you.

Code:

        OpenAIClient openAIClient = new OpenAIClientBuilder()
                .credential(new AzureKeyCredential("my key"))
                .endpoint("my endpoint")
                .buildClient();

        List<ChatMessage> chatMessages = new ArrayList<>();
        chatMessages.add(new ChatMessage(ChatRole.SYSTEM, "You are a helpful assistant. You will talk like a pirate."));
        chatMessages.add(new ChatMessage(ChatRole.USER, "Can you help me?"));
        chatMessages.add(new ChatMessage(ChatRole.ASSISTANT, "Of course, me hearty! What can I do for ye?"));
        chatMessages.add(new ChatMessage(ChatRole.USER, "What's the best way to train a parrot?"));

        ChatCompletions chatCompletions = openAIClient.getChatCompletions(
                "gpt-35-turbo", new ChatCompletionsOptions(chatMessages)
        );

        System.out.printf("Model ID=%s is created at %s.%n", chatCompletions.getId(), chatCompletions.getCreatedAt());
        for (ChatChoice choice : chatCompletions.getChoices()) {
            ChatMessage message = choice.getMessage();
            System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
            System.out.println("Message:");
            System.out.println(message.getContent());
        }

        IterableStream<ChatCompletions> chatCompletionsStream = openAIClient.getChatCompletionsStream(
                "gpt-35-turbo", new ChatCompletionsOptions(chatMessages)
        );

        chatCompletionsStream
                .stream()
                // Remove .skip(1) when using Non-Azure OpenAI API
                // Note: the first chat completions can be ignored when using Azure OpenAI service which is a known service bug.
                // TODO: remove .skip(1) when service fix the issue.
                .skip(1)
                .forEach(chatCompletionsConsumer -> {
                    ChatMessage delta = chatCompletionsConsumer.getChoices().get(0).getDelta();
                    if (delta.getRole() != null) {
                        System.out.println("Role = " + delta.getRole());
                    }
                    if (delta.getContent() != null) {
                        System.out.print(delta.getContent());
                    }
                });

Chat Completions out:

Arrr matey, training a parrot requires patience and a lot of dedication. Here be some advice to get ye started:

1. Start with basic obedience training. Teach your parrot to step up and down from your hand or a perch. Use positive reinforcement techniques like treats and verbal praise to encourage good behavior.

2. Once your parrot has mastered the basics, move on to more advanced tricks and commands. Teach them to wave, speak, fly, and even play games like fetch.

3. Create a consistent routine for your parrot's training sessions. Set a regular time each day for training and keep your sessions short and focused.

4. Remember to always approach your parrot with kindness and respect. Never yell at or physically punish your parrot for misbehaving, as this can damage your relationship and cause long-term trust issues.

With these tips in yer treasure chest, ye'll be well on yer way to raising a skilled and obedient parrot!

Streaming chat completions out:

Role = assistant
Ahoy, training a parrot be not an easy task, but with some patience, ye can turn your feathered friend into a true matey. 

First, ye need to start with the basics like teaching it to speak and respond to commands. Start by using simple words and phrases, like "hello" or "come here," and repeat them often. Reward them every time they respond correctly. 

Once your parrot is comfortable with basic commands, try teaching them tricks, like stepping up onto your finger or playing fetch. Again, patience and repetition be key here.

Make sure to use positive reinforcement by giving them their favorite treats or toy as a reward. Remember, parrots can be sensitive and easily frustrated, so always speak to them in a calm and encouraging voice.

Arrr, with some dedication and perseverance, ye can train your parrot to be a fine and loyal companion. Shiver me timbers, I hope that helps!

The content is likely to always vary. If you submit 5 of your non-streaming requests you should see similar differences. You can lower the temperature parameter to make things more deterministic, but you’ll also get less “creative” answers

3 Likes

lol, you’re so damn right. :rofl:
I actually forgot about this point.
Thank you.

1 Like