aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/ai/vespa/llm/client/openai/OpenAiClientCompletionTest.java
blob: 45ef7e270aac92bd6c3e962693ed258e74399d64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.llm.client.openai;

import ai.vespa.llm.completion.Completion;
import ai.vespa.llm.completion.StringPrompt;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
 * @author bratseth
 */
public class OpenAiClientCompletionTest {

    private static final String apiKey = "your-api-key-here";

    @Test
    @Disabled
    public void testClient() {
        var client = new OpenAiClient.Builder(apiKey).maxTokens(10).build();
        String input = "You are an unhelpful assistant who never answers questions straightforwardly. " +
                "Be as long-winded as possible. Are humans smarter than cats?\n\n";
        StringPrompt prompt = StringPrompt.from(input);
        System.out.print(prompt);
        for (int i = 0; i < 10; i++) {
            var completion = client.complete(prompt).get(0);
            System.out.print(completion.text());
            if (completion.finishReason() == Completion.FinishReason.stop) break;
            prompt = prompt.append(completion.text());
        }
    }

    @Test
    @Disabled
    public void testAsyncClient() {
        var client = new OpenAiClient.Builder(apiKey).build();
        String input = "You are an unhelpful assistant who never answers questions straightforwardly. " +
                "Be as long-winded as possible. Are humans smarter than cats?\n\n";
        StringPrompt prompt = StringPrompt.from(input);
        System.out.print(prompt);
        var future = client.completeAsync(prompt, completion -> {
            System.out.print(completion.text());
        });
        System.out.println("Waiting for completion...");
        System.out.println("\nFinished streaming because of " + future.join());
    }

}