-
Notifications
You must be signed in to change notification settings - Fork 7
/
AzureOpenAiChatCompletionsChain.java
106 lines (94 loc) · 5.57 KB
/
AzureOpenAiChatCompletionsChain.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package io.github.cupybara.javalangchains.chains.llm.azure.chat;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
import org.springframework.web.util.UriComponentsBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.cupybara.javalangchains.chains.llm.openai.chat.OpenAiChatCompletionsChain;
import io.github.cupybara.javalangchains.chains.llm.openai.chat.OpenAiChatCompletionsParameters;
import io.github.cupybara.javalangchains.chains.llm.openai.chat.OpenAiChatCompletionsRequest;
/**
* {@link OpenAiChatCompletionsChain} adopted for usage of Azure OpenAI Services
*/
public final class AzureOpenAiChatCompletionsChain extends OpenAiChatCompletionsChain {
private final URI requestUri;
/**
* @param resourceName Name of the azure resource
* @param deploymentName Name of the azure openai service deployment
* @param apiVersion The target API Version
* @param promptTemplate The template which contains placeholders in the form
* ${myPlaceholder} that are replaced for input documents
* before creating a request to a LLM.
* @param parameters The {@link OpenAiChatCompletionsParameters} allows to
* finetune requests to the OpenAI API
* @param apiKey The API-Key used for Authentication (passed using the
* "api-key" Header)
* @param systemTemplate The template for the system role which contains
* placeholders in the form ${myPlaceholder} that are
* replaced for input documents before creating a request
* to a LLM.
* @param objectMapper The {@link ObjectMapper} used for body serialization
* and deserialization
* @param webClient The {@link WebClient} used for executing requests to
* the OpenAI API
*/
public AzureOpenAiChatCompletionsChain(final String resourceName, final String deploymentName,
final String apiVersion, final String promptTemplate, final OpenAiChatCompletionsParameters parameters,
final String apiKey, final String systemTemplate, final ObjectMapper objectMapper,
final WebClient webClient) {
super(promptTemplate, parameters, apiKey, systemTemplate, objectMapper, webClient);
if (parameters.getModel() != null) {
throw new IllegalArgumentException(
"the model parameter cannot be used for the Azure OpenAI Services, it is passed via deploymentName instead");
}
this.requestUri = UriComponentsBuilder.newInstance().scheme("https")
.host(String.format("%s.openai.azure.com", resourceName)).queryParam("api-version", apiVersion)
.path(String.format("/openai/deployments/%s/chat/completions", deploymentName)).build().toUri();
}
/**
* @param resourceName Name of the azure resource
* @param deploymentName Name of the azure openai service deployment
* @param apiVersion The target API Version
* @param promptTemplate The template which contains placeholders in the form
* ${myPlaceholder} that are replaced for input documents
* before creating a request to a LLM.
* @param parameters The {@link OpenAiChatCompletionsParameters} allows to
* finetune requests to the OpenAI API
* @param apiKey The API-Key used for Authentication (passed using the
* "api-key" Header)
* @param systemTemplate The template for the system role which contains
* placeholders in the form ${myPlaceholder} that are
* replaced for input documents before creating a request
*/
public AzureOpenAiChatCompletionsChain(final String resourceName, final String deploymentName,
final String apiVersion, final String promptTemplate, final OpenAiChatCompletionsParameters parameters,
final String apiKey, final String systemTemplate) {
this(resourceName, deploymentName, apiVersion, promptTemplate, parameters, apiKey, systemTemplate,
createDefaultObjectMapper(), createDefaultWebClient());
}
/**
* @param resourceName Name of the azure resource
* @param deploymentName Name of the azure openai service deployment
* @param apiVersion The target API Version
* @param promptTemplate The template which contains placeholders in the form
* ${myPlaceholder} that are replaced for input documents
* before creating a request to a LLM.
* @param parameters The {@link OpenAiChatCompletionsParameters} allows to
* finetune requests to the OpenAI API
* @param apiKey The API-Key used for Authentication (passed using the
* "api-key" Header)
*/
public AzureOpenAiChatCompletionsChain(final String resourceName, final String deploymentName,
final String apiVersion, final String promptTemplate, final OpenAiChatCompletionsParameters parameters,
final String apiKey) {
this(resourceName, deploymentName, apiVersion, promptTemplate, parameters, apiKey, null);
}
@Override
protected ResponseSpec createResponseSpec(final OpenAiChatCompletionsRequest request, final WebClient webClient,
final ObjectMapper objectMapper) {
return webClient.post().uri(requestUri).contentType(MediaType.APPLICATION_JSON).header("api-key", getApiKey())
.body(BodyInserters.fromValue(requestToBody(request, objectMapper))).retrieve();
}
}