-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
AIServices.java
68 lines (56 loc) · 2.68 KB
/
AIServices.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
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.documentationexamples;
import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.KeyCredential;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.services.chatcompletion.ChatCompletionService;
import com.microsoft.semantickernel.services.textcompletion.TextGenerationService;
public class AIServices {
// CLIENT_KEY is for an OpenAI client
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
// AZURE_CLIENT_KEY and CLIENT_ENDPOINT are for an Azure client
// CLIENT_ENDPOINT required if AZURE_CLIENT_KEY is set
private static final String AZURE_CLIENT_KEY = System.getenv("AZURE_CLIENT_KEY");
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
private static final String CHAT_MODEL_ID = System.getenv()
.getOrDefault("CHAT_MODEL_ID", "gpt-3.5-turbo");
private static final String TEXT_MODEL_ID = System.getenv()
.getOrDefault("TEXT_MODEL_ID", "text-davinci-002");
public static void main(String[] args) {
System.out.println("======== AI Services ========");
OpenAIAsyncClient client = null;
if (AZURE_CLIENT_KEY != null && CLIENT_ENDPOINT != null) {
client = new OpenAIClientBuilder()
.credential(new AzureKeyCredential(AZURE_CLIENT_KEY))
.endpoint(CLIENT_ENDPOINT)
.buildAsyncClient();
} else if (CLIENT_KEY != null) {
client = new OpenAIClientBuilder()
.credential(new KeyCredential(CLIENT_KEY))
.buildAsyncClient();
} else {
System.out.println("No client key found");
return;
}
// <CreateChatCompletionService>
ChatCompletionService chatCompletionService = ChatCompletionService.builder()
.withModelId(CHAT_MODEL_ID)
.withOpenAIAsyncClient(client)
.build();
// </CreateChatCompletionService>
// <CreateTextGenerationService>
TextGenerationService textGenerationService = TextGenerationService.builder()
.withModelId(TEXT_MODEL_ID)
.withOpenAIAsyncClient(client)
.build();
// </CreateTextGenerationService>
// <CreateKernel>
Kernel kernel = Kernel.builder()
.withAIService(ChatCompletionService.class, chatCompletionService)
.withAIService(TextGenerationService.class, textGenerationService)
.build();
// </CreateKernel>
}
}