Skip to content

Commit

Permalink
Java: Apply proposed builder refactor to make them more consistent (#…
Browse files Browse the repository at this point in the history
…2270)

Apply proposed Builder updates to make them more consistent
  • Loading branch information
johnoliver authored Aug 10, 2023
1 parent db4c717 commit 8497b4d
Show file tree
Hide file tree
Showing 95 changed files with 1,098 additions and 932 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;
import com.microsoft.semantickernel.textcompletion.TextCompletion;
Expand All @@ -25,35 +25,35 @@ public class InlineFunctionExample {
private static final String ENDPOINT;
private static final String TEXT_TO_SUMMARIZE =
"""
Demo (ancient Greek poet)
From Wikipedia, the free encyclopedia
Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the
Roman period, known for a single epigram, engraved upon the Colossus of
Memnon, which bears her name. She speaks of herself therein as a lyric
poetess dedicated to the Muses, but nothing is known of her life.[1]
Identity
Demo was evidently Greek, as her name, a traditional epithet of Demeter,
signifies. The name was relatively common in the Hellenistic world, in
Egypt and elsewhere, and she cannot be further identified. The date of her
visit to the Colossus of Memnon cannot be established with certainty, but
internal evidence on the left leg suggests her poem was inscribed there at
some point in or after AD 196.[2]
Epigram
There are a number of graffiti inscriptions on the Colossus of Memnon.
Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac
couplets, entitled and presumably authored by "Demo" or "Damo" (the
Greek inscription is difficult to read), is a dedication to the Muses.[2]
The poem is traditionally published with the works of Balbilla, though the
internal evidence suggests a different author.[1]
In the poem, Demo explains that Memnon has shown her special respect. In
return, Demo offers the gift for poetry, as a gift to the hero. At the end
of this epigram, she addresses Memnon, highlighting his divine status by
recalling his strength and holiness.[2]
Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic
dialect. The language indicates she was knowledgeable in Homeric
poetry—'bearing a pleasant gift', for example, alludes to the use of that
phrase throughout the Iliad and Odyssey.[a][2];
""";
Demo (ancient Greek poet)
From Wikipedia, the free encyclopedia
Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the
Roman period, known for a single epigram, engraved upon the Colossus of
Memnon, which bears her name. She speaks of herself therein as a lyric
poetess dedicated to the Muses, but nothing is known of her life.[1]
Identity
Demo was evidently Greek, as her name, a traditional epithet of Demeter,
signifies. The name was relatively common in the Hellenistic world, in
Egypt and elsewhere, and she cannot be further identified. The date of her
visit to the Colossus of Memnon cannot be established with certainty, but
internal evidence on the left leg suggests her poem was inscribed there at
some point in or after AD 196.[2]
Epigram
There are a number of graffiti inscriptions on the Colossus of Memnon.
Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac
couplets, entitled and presumably authored by "Demo" or "Damo" (the
Greek inscription is difficult to read), is a dedication to the Muses.[2]
The poem is traditionally published with the works of Balbilla, though the
internal evidence suggests a different author.[1]
In the poem, Demo explains that Memnon has shown her special respect. In
return, Demo offers the gift for poetry, as a gift to the hero. At the end
of this epigram, she addresses Memnon, highlighting his divine status by
recalling his strength and holiness.[2]
Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic
dialect. The language indicates she was knowledgeable in Homeric
poetry—'bearing a pleasant gift', for example, alludes to the use of that
phrase throughout the Iliad and Odyssey.[a][2];
""";

static {
try {
Expand Down Expand Up @@ -96,19 +96,22 @@ public static void main(String[] args) {
.credential(new AzureKeyCredential(API_KEY))
.buildAsyncClient();

TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL);
TextCompletion textCompletion =
SKBuilders.textCompletionService()
.withOpenAIClient(client)
.setModelId(MODEL)
.build();
String prompt = "{{$input}}\n" + "Summarize the content above.";

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

CompletionSKFunction summarize =
kernel.getSemanticFunctionBuilder()
.createFunction(
prompt,
"summarize",
null,
null,
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000));
.setPromptTemplate(prompt)
.setFunctionName("summarize")
.setCompletionConfig(
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000))
.build();

if (summarize == null) {
LOGGER.error("Null function");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;
import com.microsoft.semantickernel.textcompletion.TextCompletion;
Expand Down Expand Up @@ -97,19 +97,23 @@ public static void main(String[] args) {
.credential(new AzureKeyCredential(API_KEY))
.buildAsyncClient();

TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL);
TextCompletion textCompletion =
SKBuilders.textCompletionService()
.withOpenAIClient(client)
.setModelId(MODEL)
.build();
String prompt = "{{$input}}\nSummarize the content above.";

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

CompletionSKFunction summarize =
SKBuilders.completionFunctions(kernel)
.createFunction(
prompt,
"summarize",
null,
null,
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000));
SKBuilders.completionFunctions()
.withKernel(kernel)
.setPromptTemplate(prompt)
.setFunctionName("summarize")
.setCompletionConfig(
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000))
.build();

if (summarize == null) {
LOGGER.error("Null function");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.SamplesConfig;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import com.microsoft.semantickernel.memory.VolatileMemoryStore;
Expand All @@ -29,7 +29,10 @@ public static Kernel buildTextCompletionKernel() throws ConfigurationException {
return SKBuilders.kernel()
.withDefaultAIService(textCompletion)
.withDefaultAIService(
SKBuilders.textEmbeddingGenerationService().build(openAIClient, model))
SKBuilders.textEmbeddingGenerationService()
.withOpenAIClient(openAIClient)
.setModelId(model)
.build())
.withMemoryStorage(new VolatileMemoryStore())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package com.microsoft.semantickernel.e2e;

import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import com.microsoft.semantickernel.orchestration.SKContext;
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
Expand Down Expand Up @@ -48,14 +48,13 @@ public void runContextVariableTest()

CompletionSKFunction chat =
kernel.getSemanticFunctionBuilder()
.createFunction(
prompt,
"ChatBot",
null,
null,
new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000));
.setPromptTemplate(prompt)
.setFunctionName("ChatBot")
.setCompletionConfig(
new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000))
.build();

SKContext readOnlySkContext = SKBuilders.context().build(kernel);
SKContext readOnlySkContext = SKBuilders.context().withKernel(kernel).build();

chat("Hi, I'm looking for book suggestions?", chat, readOnlySkContext)
.flatMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ public void executeInlineFunction() throws ConfigurationException {

CompletionSKFunction summarize =
kernel.getSemanticFunctionBuilder()
.createFunction(
prompt,
"summarize",
null,
null,
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000));
.setPromptTemplate(prompt)
.setFunctionName("summarize")
.setCompletionConfig(
new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000))
.build();

String text =
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import com.microsoft.semantickernel.orchestration.SKContext;
Expand All @@ -23,15 +23,14 @@ public class KernelTest extends AbstractKernelTest {
private static void executeCompletion(Kernel kernel) {
CompletionSKFunction summarize =
kernel.getSemanticFunctionBuilder()
.createFunction(
.setPromptTemplate(
"""
{{$input}}
One line TLDR with the fewest words.""",
null,
"",
null,
new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256));
One line TLDR with the fewest words.""")
.setCompletionConfig(
new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256))
.build();

String text1 =
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.connectors.ai.openai.textembeddings.OpenAITextEmbeddingGeneration;
import com.microsoft.semantickernel.coreskills.TextMemorySkill;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
Expand Down Expand Up @@ -86,8 +86,10 @@ public void testMemory() throws ConfigurationException {

CompletionSKFunction chat =
kernel.getSemanticFunctionBuilder()
.createFunction(
skPrompt, "recall", "aboutMe", "TextEmbeddingTest#testMemory");
.setPromptTemplate(skPrompt)
.setFunctionName("recall")
.setSkillName("aboutMe")
.build();

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

SKContext context =
SKBuilders.context()
.with(SKBuilders.variables().build())
.with(memory)
.with(kernel.getSkills())
.setVariables(SKBuilders.variables().build())
.setMemory(memory)
.setSkills(kernel.getSkills())
.build();

context.getSemanticMemory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package com.microsoft.semantickernel.planner.sequentialplanner;

import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.orchestration.SKContext;
import com.microsoft.semantickernel.planner.actionplanner.Plan;
import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
import com.microsoft.semantickernel.chatcompletion.ChatRequestSettings;
import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase;
import com.microsoft.semantickernel.exceptions.NotSupportedException;
import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -38,8 +39,27 @@ public Mono<List<String>> completeAsync(
public static class Builder implements ChatCompletion.Builder {
public Builder() {}

@Nullable private OpenAIAsyncClient client;
@Nullable private String modelId;

public Builder withOpenAIClient(OpenAIAsyncClient client) {
this.client = client;
return this;
}

public Builder setModelId(String modelId) {
this.modelId = modelId;
return this;
}

@Override
public ChatCompletion<OpenAIChatHistory> build(OpenAIAsyncClient client, String modelId) {
public ChatCompletion build() {
if (client == null) {
throw new NotSupportedException("OpenAI client not set");
}
if (modelId == null) {
throw new NotSupportedException("Model ID not set");
}
return new OpenAIChatCompletion(client, modelId);
}
}
Expand Down Expand Up @@ -137,7 +157,7 @@ public OpenAIChatHistory createNewChat(@Nullable String instructions) {

@Override
public Flux<String> generateMessageStream(
ChatHistory chat, @Nullable ChatRequestSettings requestSettings) {
OpenAIChatHistory chat, @Nullable ChatRequestSettings requestSettings) {
return this.getStreamingChatCompletionsAsync(chat, requestSettings)
.concatMap(
chatCompletionResult -> {
Expand All @@ -155,7 +175,7 @@ public Flux<String> generateMessageStream(

@Override
public Flux<ChatCompletions> getStreamingChatCompletionsAsync(
ChatHistory chat, ChatRequestSettings requestSettings) {
OpenAIChatHistory chat, ChatRequestSettings requestSettings) {
return internalGetChatStreamingResultsAsync(chat, requestSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import com.azure.ai.openai.models.CompletionsOptions;
import com.microsoft.semantickernel.ai.AIException;
import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase;
import com.microsoft.semantickernel.exceptions.NotSupportedException;
import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings;
import com.microsoft.semantickernel.textcompletion.TextCompletion;
import jakarta.inject.Inject;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import reactor.core.publisher.Mono;

/// <summary>
Expand Down Expand Up @@ -69,10 +71,28 @@ protected Mono<List<String>> internalCompleteTextAsync(
.collectList();
}

public static final class Builder extends TextCompletion.Builder {
public static final class Builder implements TextCompletion.Builder {
@Nullable private OpenAIAsyncClient client;
@Nullable private String modelId;

public Builder withOpenAIClient(OpenAIAsyncClient client) {
this.client = client;
return this;
}

public Builder setModelId(String modelId) {
this.modelId = modelId;
return this;
}

@Override
public TextCompletion build(OpenAIAsyncClient client, String modelId) {
public TextCompletion build() {
if (client == null) {
throw new NotSupportedException("OpenAI client not set");
}
if (modelId == null) {
throw new NotSupportedException("Model ID not set");
}
return new OpenAITextCompletion(client, modelId);
}
}
Expand Down
Loading

0 comments on commit 8497b4d

Please sign in to comment.