Skip to content

Commit 8497b4d

Browse files
authored
Java: Apply proposed builder refactor to make them more consistent (#2270)
Apply proposed Builder updates to make them more consistent
1 parent db4c717 commit 8497b4d

File tree

95 files changed

+1098
-932
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1098
-932
lines changed

java/api-test/api-usage-example/src/main/java/InlineFunctionExample.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.azure.ai.openai.OpenAIClientBuilder;
44
import com.azure.core.credential.AzureKeyCredential;
55
import com.microsoft.semantickernel.Kernel;
6-
import com.microsoft.semantickernel.builders.SKBuilders;
6+
import com.microsoft.semantickernel.SKBuilders;
77
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
88
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;
99
import com.microsoft.semantickernel.textcompletion.TextCompletion;
@@ -25,35 +25,35 @@ public class InlineFunctionExample {
2525
private static final String ENDPOINT;
2626
private static final String TEXT_TO_SUMMARIZE =
2727
"""
28-
Demo (ancient Greek poet)
29-
From Wikipedia, the free encyclopedia
30-
Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the
31-
Roman period, known for a single epigram, engraved upon the Colossus of
32-
Memnon, which bears her name. She speaks of herself therein as a lyric
33-
poetess dedicated to the Muses, but nothing is known of her life.[1]
34-
Identity
35-
Demo was evidently Greek, as her name, a traditional epithet of Demeter,
36-
signifies. The name was relatively common in the Hellenistic world, in
37-
Egypt and elsewhere, and she cannot be further identified. The date of her
38-
visit to the Colossus of Memnon cannot be established with certainty, but
39-
internal evidence on the left leg suggests her poem was inscribed there at
40-
some point in or after AD 196.[2]
41-
Epigram
42-
There are a number of graffiti inscriptions on the Colossus of Memnon.
43-
Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac
44-
couplets, entitled and presumably authored by "Demo" or "Damo" (the
45-
Greek inscription is difficult to read), is a dedication to the Muses.[2]
46-
The poem is traditionally published with the works of Balbilla, though the
47-
internal evidence suggests a different author.[1]
48-
In the poem, Demo explains that Memnon has shown her special respect. In
49-
return, Demo offers the gift for poetry, as a gift to the hero. At the end
50-
of this epigram, she addresses Memnon, highlighting his divine status by
51-
recalling his strength and holiness.[2]
52-
Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic
53-
dialect. The language indicates she was knowledgeable in Homeric
54-
poetry—'bearing a pleasant gift', for example, alludes to the use of that
55-
phrase throughout the Iliad and Odyssey.[a][2];
56-
""";
28+
Demo (ancient Greek poet)
29+
From Wikipedia, the free encyclopedia
30+
Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the
31+
Roman period, known for a single epigram, engraved upon the Colossus of
32+
Memnon, which bears her name. She speaks of herself therein as a lyric
33+
poetess dedicated to the Muses, but nothing is known of her life.[1]
34+
Identity
35+
Demo was evidently Greek, as her name, a traditional epithet of Demeter,
36+
signifies. The name was relatively common in the Hellenistic world, in
37+
Egypt and elsewhere, and she cannot be further identified. The date of her
38+
visit to the Colossus of Memnon cannot be established with certainty, but
39+
internal evidence on the left leg suggests her poem was inscribed there at
40+
some point in or after AD 196.[2]
41+
Epigram
42+
There are a number of graffiti inscriptions on the Colossus of Memnon.
43+
Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac
44+
couplets, entitled and presumably authored by "Demo" or "Damo" (the
45+
Greek inscription is difficult to read), is a dedication to the Muses.[2]
46+
The poem is traditionally published with the works of Balbilla, though the
47+
internal evidence suggests a different author.[1]
48+
In the poem, Demo explains that Memnon has shown her special respect. In
49+
return, Demo offers the gift for poetry, as a gift to the hero. At the end
50+
of this epigram, she addresses Memnon, highlighting his divine status by
51+
recalling his strength and holiness.[2]
52+
Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic
53+
dialect. The language indicates she was knowledgeable in Homeric
54+
poetry—'bearing a pleasant gift', for example, alludes to the use of that
55+
phrase throughout the Iliad and Odyssey.[a][2];
56+
""";
5757

5858
static {
5959
try {
@@ -96,19 +96,22 @@ public static void main(String[] args) {
9696
.credential(new AzureKeyCredential(API_KEY))
9797
.buildAsyncClient();
9898

99-
TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL);
99+
TextCompletion textCompletion =
100+
SKBuilders.textCompletionService()
101+
.withOpenAIClient(client)
102+
.setModelId(MODEL)
103+
.build();
100104
String prompt = "{{$input}}\n" + "Summarize the content above.";
101105

102106
Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build();
103107

104108
CompletionSKFunction summarize =
105109
kernel.getSemanticFunctionBuilder()
106-
.createFunction(
107-
prompt,
108-
"summarize",
109-
null,
110-
null,
111-
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000));
110+
.setPromptTemplate(prompt)
111+
.setFunctionName("summarize")
112+
.setCompletionConfig(
113+
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000))
114+
.build();
112115

113116
if (summarize == null) {
114117
LOGGER.error("Null function");

java/api-test/api-usage-example/src/main/java/InlineFunctionWithPreBuiltSkillExample.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.azure.ai.openai.OpenAIClientBuilder;
44
import com.azure.core.credential.AzureKeyCredential;
55
import com.microsoft.semantickernel.Kernel;
6-
import com.microsoft.semantickernel.builders.SKBuilders;
6+
import com.microsoft.semantickernel.SKBuilders;
77
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
88
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;
99
import com.microsoft.semantickernel.textcompletion.TextCompletion;
@@ -97,19 +97,23 @@ public static void main(String[] args) {
9797
.credential(new AzureKeyCredential(API_KEY))
9898
.buildAsyncClient();
9999

100-
TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL);
100+
TextCompletion textCompletion =
101+
SKBuilders.textCompletionService()
102+
.withOpenAIClient(client)
103+
.setModelId(MODEL)
104+
.build();
101105
String prompt = "{{$input}}\nSummarize the content above.";
102106

103107
Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build();
104108

105109
CompletionSKFunction summarize =
106-
SKBuilders.completionFunctions(kernel)
107-
.createFunction(
108-
prompt,
109-
"summarize",
110-
null,
111-
null,
112-
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000));
110+
SKBuilders.completionFunctions()
111+
.withKernel(kernel)
112+
.setPromptTemplate(prompt)
113+
.setFunctionName("summarize")
114+
.setCompletionConfig(
115+
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000))
116+
.build();
113117

114118
if (summarize == null) {
115119
LOGGER.error("Null function");

java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/AbstractKernelTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import com.azure.ai.openai.OpenAIAsyncClient;
55
import com.microsoft.semantickernel.Kernel;
6+
import com.microsoft.semantickernel.SKBuilders;
67
import com.microsoft.semantickernel.SamplesConfig;
7-
import com.microsoft.semantickernel.builders.SKBuilders;
88
import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion;
99
import com.microsoft.semantickernel.exceptions.ConfigurationException;
1010
import com.microsoft.semantickernel.memory.VolatileMemoryStore;
@@ -29,7 +29,10 @@ public static Kernel buildTextCompletionKernel() throws ConfigurationException {
2929
return SKBuilders.kernel()
3030
.withDefaultAIService(textCompletion)
3131
.withDefaultAIService(
32-
SKBuilders.textEmbeddingGenerationService().build(openAIClient, model))
32+
SKBuilders.textEmbeddingGenerationService()
33+
.withOpenAIClient(openAIClient)
34+
.setModelId(model)
35+
.build())
3336
.withMemoryStorage(new VolatileMemoryStore())
3437
.build();
3538
}

java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/ContextVariableFunctionTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package com.microsoft.semantickernel.e2e;
33

44
import com.microsoft.semantickernel.Kernel;
5-
import com.microsoft.semantickernel.builders.SKBuilders;
5+
import com.microsoft.semantickernel.SKBuilders;
66
import com.microsoft.semantickernel.exceptions.ConfigurationException;
77
import com.microsoft.semantickernel.orchestration.SKContext;
88
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
@@ -48,14 +48,13 @@ public void runContextVariableTest()
4848

4949
CompletionSKFunction chat =
5050
kernel.getSemanticFunctionBuilder()
51-
.createFunction(
52-
prompt,
53-
"ChatBot",
54-
null,
55-
null,
56-
new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000));
51+
.setPromptTemplate(prompt)
52+
.setFunctionName("ChatBot")
53+
.setCompletionConfig(
54+
new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000))
55+
.build();
5756

58-
SKContext readOnlySkContext = SKBuilders.context().build(kernel);
57+
SKContext readOnlySkContext = SKBuilders.context().withKernel(kernel).build();
5958

6059
chat("Hi, I'm looking for book suggestions?", chat, readOnlySkContext)
6160
.flatMap(

java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/InlineFunctionTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ public void executeInlineFunction() throws ConfigurationException {
2424

2525
CompletionSKFunction summarize =
2626
kernel.getSemanticFunctionBuilder()
27-
.createFunction(
28-
prompt,
29-
"summarize",
30-
null,
31-
null,
32-
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000));
27+
.setPromptTemplate(prompt)
28+
.setFunctionName("summarize")
29+
.setCompletionConfig(
30+
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000))
31+
.build();
3332

3433
String text =
3534
"""

java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/KernelTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import com.azure.ai.openai.OpenAIAsyncClient;
55
import com.microsoft.semantickernel.Kernel;
6-
import com.microsoft.semantickernel.builders.SKBuilders;
6+
import com.microsoft.semantickernel.SKBuilders;
77
import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion;
88
import com.microsoft.semantickernel.exceptions.ConfigurationException;
99
import com.microsoft.semantickernel.orchestration.SKContext;
@@ -23,15 +23,14 @@ public class KernelTest extends AbstractKernelTest {
2323
private static void executeCompletion(Kernel kernel) {
2424
CompletionSKFunction summarize =
2525
kernel.getSemanticFunctionBuilder()
26-
.createFunction(
26+
.setPromptTemplate(
2727
"""
2828
{{$input}}
2929
30-
One line TLDR with the fewest words.""",
31-
null,
32-
"",
33-
null,
34-
new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256));
30+
One line TLDR with the fewest words.""")
31+
.setCompletionConfig(
32+
new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256))
33+
.build();
3534

3635
String text1 =
3736
"""

java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/TextEmbeddingsTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import com.azure.ai.openai.OpenAIAsyncClient;
55
import com.microsoft.semantickernel.Kernel;
6-
import com.microsoft.semantickernel.builders.SKBuilders;
6+
import com.microsoft.semantickernel.SKBuilders;
77
import com.microsoft.semantickernel.connectors.ai.openai.textembeddings.OpenAITextEmbeddingGeneration;
88
import com.microsoft.semantickernel.coreskills.TextMemorySkill;
99
import com.microsoft.semantickernel.exceptions.ConfigurationException;
@@ -86,8 +86,10 @@ public void testMemory() throws ConfigurationException {
8686

8787
CompletionSKFunction chat =
8888
kernel.getSemanticFunctionBuilder()
89-
.createFunction(
90-
skPrompt, "recall", "aboutMe", "TextEmbeddingTest#testMemory");
89+
.setPromptTemplate(skPrompt)
90+
.setFunctionName("recall")
91+
.setSkillName("aboutMe")
92+
.build();
9193

9294
VolatileMemoryStore volatileMemoryStore = new VolatileMemoryStore();
9395
volatileMemoryStore.createCollectionAsync("aboutMe").block();
@@ -103,9 +105,9 @@ public void testMemory() throws ConfigurationException {
103105

104106
SKContext context =
105107
SKBuilders.context()
106-
.with(SKBuilders.variables().build())
107-
.with(memory)
108-
.with(kernel.getSkills())
108+
.setVariables(SKBuilders.variables().build())
109+
.setMemory(memory)
110+
.setSkills(kernel.getSkills())
109111
.build();
110112

111113
context.getSemanticMemory()

java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package com.microsoft.semantickernel.planner.sequentialplanner;
33

44
import com.microsoft.semantickernel.Kernel;
5-
import com.microsoft.semantickernel.builders.SKBuilders;
5+
import com.microsoft.semantickernel.SKBuilders;
66
import com.microsoft.semantickernel.orchestration.SKContext;
77
import com.microsoft.semantickernel.planner.actionplanner.Plan;
88
import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction;

java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletion.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
1313
import com.microsoft.semantickernel.chatcompletion.ChatRequestSettings;
1414
import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase;
15+
import com.microsoft.semantickernel.exceptions.NotSupportedException;
1516
import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings;
1617
import java.util.Arrays;
1718
import java.util.HashMap;
@@ -38,8 +39,27 @@ public Mono<List<String>> completeAsync(
3839
public static class Builder implements ChatCompletion.Builder {
3940
public Builder() {}
4041

42+
@Nullable private OpenAIAsyncClient client;
43+
@Nullable private String modelId;
44+
45+
public Builder withOpenAIClient(OpenAIAsyncClient client) {
46+
this.client = client;
47+
return this;
48+
}
49+
50+
public Builder setModelId(String modelId) {
51+
this.modelId = modelId;
52+
return this;
53+
}
54+
4155
@Override
42-
public ChatCompletion<OpenAIChatHistory> build(OpenAIAsyncClient client, String modelId) {
56+
public ChatCompletion build() {
57+
if (client == null) {
58+
throw new NotSupportedException("OpenAI client not set");
59+
}
60+
if (modelId == null) {
61+
throw new NotSupportedException("Model ID not set");
62+
}
4363
return new OpenAIChatCompletion(client, modelId);
4464
}
4565
}
@@ -137,7 +157,7 @@ public OpenAIChatHistory createNewChat(@Nullable String instructions) {
137157

138158
@Override
139159
public Flux<String> generateMessageStream(
140-
ChatHistory chat, @Nullable ChatRequestSettings requestSettings) {
160+
OpenAIChatHistory chat, @Nullable ChatRequestSettings requestSettings) {
141161
return this.getStreamingChatCompletionsAsync(chat, requestSettings)
142162
.concatMap(
143163
chatCompletionResult -> {
@@ -155,7 +175,7 @@ public Flux<String> generateMessageStream(
155175

156176
@Override
157177
public Flux<ChatCompletions> getStreamingChatCompletionsAsync(
158-
ChatHistory chat, ChatRequestSettings requestSettings) {
178+
OpenAIChatHistory chat, ChatRequestSettings requestSettings) {
159179
return internalGetChatStreamingResultsAsync(chat, requestSettings);
160180
}
161181

java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textcompletion/OpenAITextCompletion.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
import com.azure.ai.openai.models.CompletionsOptions;
88
import com.microsoft.semantickernel.ai.AIException;
99
import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase;
10+
import com.microsoft.semantickernel.exceptions.NotSupportedException;
1011
import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings;
1112
import com.microsoft.semantickernel.textcompletion.TextCompletion;
1213
import jakarta.inject.Inject;
1314
import java.util.Collections;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import javax.annotation.Nonnull;
18+
import javax.annotation.Nullable;
1719
import reactor.core.publisher.Mono;
1820

1921
/// <summary>
@@ -69,10 +71,28 @@ protected Mono<List<String>> internalCompleteTextAsync(
6971
.collectList();
7072
}
7173

72-
public static final class Builder extends TextCompletion.Builder {
74+
public static final class Builder implements TextCompletion.Builder {
75+
@Nullable private OpenAIAsyncClient client;
76+
@Nullable private String modelId;
77+
78+
public Builder withOpenAIClient(OpenAIAsyncClient client) {
79+
this.client = client;
80+
return this;
81+
}
82+
83+
public Builder setModelId(String modelId) {
84+
this.modelId = modelId;
85+
return this;
86+
}
7387

7488
@Override
75-
public TextCompletion build(OpenAIAsyncClient client, String modelId) {
89+
public TextCompletion build() {
90+
if (client == null) {
91+
throw new NotSupportedException("OpenAI client not set");
92+
}
93+
if (modelId == null) {
94+
throw new NotSupportedException("Model ID not set");
95+
}
7696
return new OpenAITextCompletion(client, modelId);
7797
}
7898
}

0 commit comments

Comments
 (0)