Skip to content

Commit

Permalink
Java: Update to Service registry pattern (microsoft#1923)
Browse files Browse the repository at this point in the history
Resolves microsoft#1628 

Also setKernelConfig -> withKernelConfig, to be in keeping with other
builder functions
  • Loading branch information
johnoliver authored Jul 10, 2023
1 parent 9aa4a85 commit da80599
Show file tree
Hide file tree
Showing 40 changed files with 492 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void main(String[] args) {
.addTextCompletionService(MODEL, kernel -> textCompletion)
.build();

Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();

CompletionSKFunction summarize =
kernel.getSemanticFunctionBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void main(String[] args) {
.addTextCompletionService(MODEL, kernel -> textCompletion)
.build();

Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();

CompletionSKFunction summarize =
SKBuilders.completionFunctions(kernel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Kernel buildTextCompletionKernel() throws IOException {
.build();

return SKBuilders.kernel()
.setKernelConfig(kernelConfig)
.withKernelConfig(kernelConfig)
.withMemoryStore(new VolatileMemoryStore())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static Kernel buildKernel(OpenAIAsyncClient client, String model) {
.addTextCompletionService(model, kernel -> textCompletion)
.build();

return SKBuilders.kernel().setKernelConfig(kernelConfig).build();
return SKBuilders.kernel().withKernelConfig(kernelConfig).build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static Kernel getKernel(OpenAIAsyncClient client) {
.build();

Kernel kernel = SKBuilders.kernel()
.setKernelConfig(config)
.withKernelConfig(config)
.build();

return kernel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static Kernel getKernel(OpenAIAsyncClient client) {
// TODO: Add Volatile memory

Kernel kernel = SKBuilders.kernel()
.setKernelConfig(config)
.withKernelConfig(config)
.build();

return kernel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class Example02_Pipeline {
public static void main(String[] args) {
KernelConfig kernelConfig = SKBuilders.kernelConfig().build();
Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();

// Load native skill
ReadOnlyFunctionCollection text = kernel.importSkill(new TextSkill(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Mono<String> appendDay(

public static void main(String[] args) {
KernelConfig kernelConfig = SKBuilders.kernelConfig().build();
Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();

// Load native skill
ReadOnlyFunctionCollection functionCollection =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args) throws IOException {
.addTextCompletionService("text-davinci-003", kernel -> textCompletion)
.build();

Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();
kernel.importSkill(new SearchEngineSkill(), null);
kernel.importSkillFromDirectory("SummarizeSkill", "./samples/skills", "SummarizeSkill");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) throws IOException {
.addTextCompletionService("text-davinci-003", kernel -> textCompletion)
.build();

Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();

System.out.println("======== Inline Function Definition ========");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) throws IOException {
.build();

Kernel kernel = SKBuilders.kernel()
.setKernelConfig(kernelConfig)
.withKernelConfig(kernelConfig)
.build();

// Load native skill into the kernel skill collection, sharing its functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void main(String[] args) throws IOException {

Kernel kernel = SKBuilders
.kernel()
.setKernelConfig(kernelConfig)
.withKernelConfig(kernelConfig)
.build();

CompletionSKFunction summarizeFunc = SKBuilders.completionFunctions(kernel)
Expand All @@ -89,7 +89,6 @@ public static void main(String[] args) throws IOException {
new PromptTemplateConfig.CompletionConfig(
0.2, 0.5, 0, 0, 2000));


kernel.registerSemanticFunction(summarizeFunc);

SKContext context = summarizeFunc.invokeAsync(text).block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) throws IOException {
.addTextCompletionService("text-davinci-003", kernel -> textCompletion)
.build();

Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();
Kernel kernel = SKBuilders.kernel().withKernelConfig(kernelConfig).build();

System.out.println("======== Native function types ========");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static void memorySampleAsync() throws IOException {
private static Kernel initializeKernel() throws IOException {
OpenAIAsyncClient client = Config.getClient();
var kernel = SKBuilders.kernel()
.setKernelConfig(SKBuilders
.withKernelConfig(SKBuilders
.kernelConfig()
.addTextCompletionService("text-davinci-003", kernel1 -> SKBuilders.textCompletionService()
.build(client, "text-davinci-003"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ private static Kernel initializeKernel(Config.ClientType clientType) throws IOEx

KernelConfig kernelConfig =
new KernelConfig.Builder()
.addTextCompletionService("text-davinci-003", kernel -> textCompletion)
.build();

return SKBuilders.kernel().setKernelConfig(kernelConfig).build();
return SKBuilders.kernel()
.withKernelConfig(kernelConfig)
.withAIService("text-davinci-003", textCompletion, true, TextCompletion.class).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.connectors.memory.azurecognitivesearch.AzureCognitiveSearchMemory;
import com.microsoft.semantickernel.memory.SemanticTextMemory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -42,7 +41,7 @@ public static void main(String[] args) throws Exception
var kernelConfig = SKBuilders.kernelConfig().build();

var kernelWithACS = SKBuilders.kernel()
.setKernelConfig(kernelConfig)
.withKernelConfig(kernelConfig)
.withMemory(new AzureCognitiveSearchMemory(System.getenv("ACS_ENDPOINT"), System.getenv("ACS_API_KEY")))
.build();

Expand All @@ -69,7 +68,7 @@ public static void main(String[] args) throws Exception
.build();

var kernelWithCustomDb = SKBuilders.kernel()
.setKernelConfig(kernelConfigWithTextEmbedding)
.withKernelConfig(kernelConfigWithTextEmbedding)
.withMemoryStore(SKBuilders.memoryStore().build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.Config;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.KernelConfig;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.chatcompletion.ChatCompletion;
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
Expand All @@ -21,14 +20,17 @@ public class Example17_ChatGPT {
public static void main(String[] args) throws IOException {
OpenAIAsyncClient client = Config.ClientType.AZURE_OPEN_AI.getClient();

KernelConfig kernelConfig = SKBuilders.kernelConfig()
.addChatCompletionService(
Kernel kernel = SKBuilders.kernel()
.withKernelConfig(SKBuilders.kernelConfig()
.build())
.withAIService(
"chat-test",
kernel -> SKBuilders.chatCompletion().build(client, "chat-test"))
SKBuilders.chatCompletion().build(client, "chat-test"),
true,
ChatCompletion.class
)
.build();

Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build();

ChatCompletion<OpenAIChatHistory> chatGPT = kernel.getService(null, ChatCompletion.class);

OpenAIChatHistory chatHistory = chatGPT.createNewChat("You are a librarian, expert about books");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void main(String[] args) throws IOException {
System.out.println("======== Action Planner ========");

var kernel = SKBuilders.kernel()
.setKernelConfig(SKBuilders
.withKernelConfig(SKBuilders
.kernelConfig()
.addTextCompletionService("text-davinci-002", kernel1 -> SKBuilders.textCompletionService()
.build(client, "text-davinci-002"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
import com.microsoft.semantickernel.orchestration.ContextVariables;
import com.microsoft.semantickernel.orchestration.SKContext;
import com.microsoft.semantickernel.orchestration.SKFunction;
import com.microsoft.semantickernel.services.AIService;
import com.microsoft.semantickernel.services.AIServiceCollection;
import com.microsoft.semantickernel.services.AIServiceProvider;
import com.microsoft.semantickernel.templateengine.PromptTemplateEngine;
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;

import reactor.core.publisher.Mono;

import java.util.function.Function;
import java.util.function.Supplier;

import javax.annotation.Nullable;

/** Interface for the semantic kernel. */
Expand Down Expand Up @@ -66,7 +72,8 @@ public interface Kernel extends SkillExecutor {
CompletionSKFunction.Builder getSemanticFunctionBuilder();

/** Obtains the service with the given name and type */
<T> T getService(@Nullable String name, Class<T> clazz) throws KernelException;
<T extends AIService> T getService(@Nullable String name, Class<T> clazz)
throws KernelException;

/** Registers a semantic functon on this kernel */
<RequestConfiguration, FunctionType extends SKFunction<RequestConfiguration>>
Expand All @@ -80,14 +87,15 @@ class Builder {
@Nullable private KernelConfig kernelConfig = null;
@Nullable private PromptTemplateEngine promptTemplateEngine = null;
@Nullable private MemoryStore memoryStore = null;
@Nullable private AIServiceCollection aiServices = new AIServiceCollection();
@Nullable private SemanticTextMemory memory = null;

public Builder setKernelConfig(KernelConfig kernelConfig) {
public Builder withKernelConfig(KernelConfig kernelConfig) {
this.kernelConfig = kernelConfig;
return this;
}

public Builder setPromptTemplateEngine(PromptTemplateEngine promptTemplateEngine) {
public Builder withPromptTemplateEngine(PromptTemplateEngine promptTemplateEngine) {
this.promptTemplateEngine = promptTemplateEngine;
return this;
}
Expand All @@ -97,6 +105,65 @@ public Builder withMemoryStore(MemoryStore memoryStore) {
return this;
}

/**
* Adds an instance to the services collection
*
* @param instance The instance.
* @param clazz The class of the instance.
* @return The builder.
*/
public <T extends AIService> Builder withDefaultAIService(T instance, Class<T> clazz) {
this.aiServices.setService(instance, clazz);
return this;
}

/**
* Adds an instance to the services collection
*
* @param serviceId The service ID
* @param instance The instance.
* @param setAsDefault Optional: set as the default AI service for type T
* @param clazz The class of the instance.
*/
public <T extends AIService> Builder withAIService(
@Nullable String serviceId, T instance, boolean setAsDefault, Class<T> clazz) {
this.aiServices.setService(serviceId, instance, setAsDefault, clazz);
return this;
}

/**
* Adds a factory method to the services collection
*
* @param factory The factory method that creates the AI service instances of type T.
* @param clazz The class of the instance.
*/
public <T extends AIService> Builder withDefaultAIService(
Supplier<T> factory, Class<T> clazz) {
this.aiServices.setService(factory, clazz);
return this;
}

/**
* Adds a factory method to the services collection
*
* @param serviceId The service ID
* @param factory The factory method that creates the AI service instances of type T.
* @param setAsDefault Optional: set as the default AI service for type T
* @param clazz The class of the instance.
*/
public <T extends AIService> Builder withAIService(
@Nullable String serviceId,
Function<KernelConfig, T> factory,
boolean setAsDefault,
Class<T> clazz) {
this.aiServices.setService(
serviceId,
(Supplier<T>) () -> factory.apply(this.kernelConfig),
setAsDefault,
clazz);
return this;
}

public Builder withMemory(SemanticTextMemory memory) {
this.memory = memory;
return this;
Expand All @@ -109,7 +176,12 @@ public Kernel build() {

return BuildersSingleton.INST
.getKernelBuilder()
.build(kernelConfig, promptTemplateEngine, memory, memoryStore);
.build(
kernelConfig,
promptTemplateEngine,
memory,
memoryStore,
aiServices.build());
}
}

Expand All @@ -118,6 +190,7 @@ Kernel build(
KernelConfig kernelConfig,
@Nullable PromptTemplateEngine promptTemplateEngine,
@Nullable SemanticTextMemory memory,
@Nullable MemoryStore memoryStore);
@Nullable MemoryStore memoryStore,
@Nullable AIServiceProvider aiServiceProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
package com.microsoft.semantickernel.ai.embeddings;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.services.AIService;

import reactor.core.publisher.Mono;

import java.util.List;

/** Interface for text embedding generation services */
public interface EmbeddingGeneration<TValue, TEmbedding extends Number> {
public interface EmbeddingGeneration<TValue, TEmbedding extends Number> extends AIService {
/**
* Generates a list of embeddings associated to the data
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
package com.microsoft.semantickernel.chatcompletion;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.microsoft.semantickernel.services.AIService;

import reactor.core.publisher.Mono;

import javax.annotation.Nullable;

public interface ChatCompletion<ChatHistoryType extends ChatHistory> {
public interface ChatCompletion<ChatHistoryType extends ChatHistory> extends AIService {
/**
* Generate a new chat message
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.services;

/** Marker interface for AI services */
public interface AIService {}
Loading

0 comments on commit da80599

Please sign in to comment.