aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/main/java/ai/vespa/llm/client/OpenAiClient.java
blob: 3f4475b2482338f398bd23ef6afcf0f7215687e1 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.llm.client;

import ai.vespa.llm.Completion;
import ai.vespa.llm.LanguageModel;
import ai.vespa.llm.Prompt;
import com.theokanning.openai.OpenAiHttpException;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.service.OpenAiService;
import com.yahoo.api.annotations.Beta;
import com.yahoo.yolean.Exceptions;

import java.util.List;

/**
 * A client to the OpenAI language model API. Refer to https://platform.openai.com/docs/api-reference/.
 *
 * @author bratseth
 */
@Beta
public class OpenAiClient implements LanguageModel {

    private final OpenAiService openAiService;
    private final String model;
    private final boolean echo;

    private OpenAiClient(Builder builder) {
        openAiService = new OpenAiService(builder.token);
        this.model = builder.model;
        this.echo = builder.echo;
    }

    @Override
    public List<Completion> complete(Prompt prompt) {
        try {
            CompletionRequest completionRequest = CompletionRequest.builder()
                                                          .prompt(prompt.asString())
                                                          .model(model)
                                                          .echo(echo)
                                                          .build();
            return openAiService.createCompletion(completionRequest).getChoices().stream()
                           .map(c -> new Completion(c.getText(), toFinishReason(c.getFinish_reason()))).toList();
        }
        catch (OpenAiHttpException e) {
            throw new RuntimeException(Exceptions.toMessageString(e));
        }
    }

    private Completion.FinishReason toFinishReason(String finishReasonString) {
        return switch(finishReasonString) {
            case "length" -> Completion.FinishReason.length;
            case "stop" -> Completion.FinishReason.stop;
            default -> throw new IllegalStateException("Unknown OpenAi completion finish reason '" + finishReasonString + "'");
        };
    }

    public static class Builder {

        private final String token;
        private String model = "text-davinci-003";
        private boolean echo = false;

        public Builder(String token) {
            this.token = token;
        }

        /** One of the language models listed at https://platform.openai.com/docs/models */
        public Builder model(String model) {
            this.model = model;
            return this;
        }

        public Builder echo(boolean echo) {
            this.echo = echo;
            return this;
        }

        public OpenAiClient build() {
            return new OpenAiClient(this);
        }

    }

}