Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Convert PromptTemplateConfig and ChatMessageContent to immutable #5067

Merged
merged 13 commits into from
Feb 22, 2024
5 changes: 2 additions & 3 deletions java/aiservices/openai/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai;

import com.azure.core.http.HttpHeaderName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.chatcompletion;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down Expand Up @@ -110,7 +111,7 @@ private Mono<List<ChatMessageContent<?>>> internalChatMessageContentsAsync(
if (kernel != null) {
kernel.getPlugins()
.forEach(plugin -> plugin.getFunctions().forEach((name, function) -> functions
.add(new OpenAIFunction(function.getMetadata(), plugin.getName()))));
.add(OpenAIFunction.build(function.getMetadata(), plugin.getName()))));
}

// Create copy to avoid reactor exceptions when updating request messages internally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.chatcompletion;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.chatcompletion;

import com.azure.ai.openai.models.FunctionDefinition;
Expand All @@ -9,20 +10,32 @@
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionMetadata;
import com.microsoft.semantickernel.semanticfunctions.KernelParameterMetadata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

class OpenAIFunction {

private final String pluginName;
private final String name;
private final FunctionDefinition functionDefinition;

public OpenAIFunction(KernelFunctionMetadata<?> metadata, String pluginName) {
this.name = metadata.getName();
protected OpenAIFunction(
@Nonnull String name,
@Nonnull String pluginName,
@Nonnull FunctionDefinition functionDefinition) {
this.name = name;
this.pluginName = pluginName;
this.functionDefinition = toFunctionDefinition(metadata, pluginName);
this.functionDefinition = functionDefinition;
}

public static OpenAIFunction build(KernelFunctionMetadata<?> metadata, String pluginName) {
String name = metadata.getName();
FunctionDefinition functionDefinition = toFunctionDefinition(metadata, pluginName);
return new OpenAIFunction(name, pluginName, functionDefinition);
}

public String getName() {
Expand All @@ -38,7 +51,8 @@ public FunctionDefinition getFunctionDefinition() {
}

/**
* Gets the separator used between the plugin name and the function name, if a plugin name is present.
* Gets the separator used between the plugin name and the function name, if a plugin name is
* present.
*
* @return The separator used between the plugin name and the function name.
*/
Expand All @@ -58,7 +72,8 @@ private static String getFullyQualifiedName(
}

/**
* Converts a KernelFunctionMetadata representation to the SDK's FunctionDefinition representation.
* Converts a KernelFunctionMetadata representation to the SDK's FunctionDefinition
* representation.
*
* @return A FunctionDefinition containing all the function information.
*/
Expand Down Expand Up @@ -97,24 +112,40 @@ public static FunctionDefinition toFunctionDefinition(KernelFunctionMetadata<?>
}

private static class OpenAIFunctionParameter {

@JsonProperty("type")
private String type;
@JsonProperty("required")
private List<String> required;
@JsonProperty("properties")
private Map<String, JsonNode> properties;

public OpenAIFunctionParameter(String type, List<String> required,
public OpenAIFunctionParameter(
String type,
List<String> required,
Map<String, JsonNode> properties) {
this.type = type;
this.required = required;
this.properties = properties;
this.required = Collections.unmodifiableList(required);
this.properties = Collections.unmodifiableMap(properties);
}

public String getType() {
return type;
}

public List<String> getRequired() {
return required;
}

public Map<String, JsonNode> getProperties() {
return properties;
}
}

private static String getSchemaForFunctionParameter(@Nullable String description) {
if (description == null)
if (description == null) {
return "{\"type\":\"string\"}";
}
return String.format("{\"type\":\"string\", \"description\":\"%s\"}", description);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.chatcompletion;

import com.azure.ai.openai.models.ChatRequestMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.chatcompletion;

import com.azure.ai.openai.models.ChatRequestMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.textcompletion;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
5 changes: 2 additions & 3 deletions java/api-test/integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>api-test</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.

package com.microsoft.semantickernel.syntaxexamples;

import com.microsoft.semantickernel.plugins.text.TextPlugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.syntaxexamples;

import com.github.tomakehurst.wiremock.client.WireMock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.syntaxexamples;

import com.github.tomakehurst.wiremock.WireMockServer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.syntaxexamples;

import com.fasterxml.jackson.databind.JsonNode;
Expand Down
5 changes: 2 additions & 3 deletions java/api-test/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
5 changes: 2 additions & 3 deletions java/connectors/semantickernel-connectors-web-bing/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.connectors.web.bing;

import com.azure.core.http.HttpClient;
Expand Down
2 changes: 1 addition & 1 deletion java/plugins/semantickernel-plugin-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.plugins.web;

import com.microsoft.semantickernel.semanticfunctions.annotations.DefineKernelFunction;
Expand Down
7 changes: 3 additions & 4 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<packaging>pom</packaging>

<url>https://www.github.com/microsoft/semantic-kernel</url>
Expand Down Expand Up @@ -663,11 +662,11 @@
<style>AOSP</style>
<reflowLongStrings>true</reflowLongStrings>
</googleJavaFormat>
-->
<licenseHeader>
<content>// Copyright (c) Microsoft. All rights reserved.</content>
</licenseHeader>
<toggleOffOn />
-->
</java>
</configuration>
</plugin>
Expand Down
11 changes: 5 additions & 6 deletions java/samples/pom.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>semantickernel-samples-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Semantic Kernel Samples Parent</name>
<description>Parent pom for sample code for using the Semantic Kernel project</description>
Expand Down Expand Up @@ -131,8 +130,8 @@
<encoding>${project.build.sourceEncoding}</encoding>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg></arg>
<compilerArg></compilerArg>
<arg />
<compilerArg />
</compilerArgs>
</configuration>
</plugin>
Expand Down
5 changes: 2 additions & 3 deletions java/samples/sample-code/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-samples-parent</artifactId>
<version>1.0.0-rc1-SNAPSHOT</version>
<version>1.0.0-rc2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.plugins;

import com.microsoft.semantickernel.Kernel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.plugins;

public class PromptFunctionConstants {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.microsoft.semantickernel.plugins.text.TextPlugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down Expand Up @@ -41,7 +42,9 @@ public static void main(String[] args) throws ConfigurationException {

String question = "How popular is the Polly library?";

KernelFunction<String> fuction = KernelFunctionFromPrompt.create(question);
KernelFunction<String> fuction = KernelFunctionFromPrompt.<String>builder()
.withTemplate(question)
.build();

try {
// Will retry 3 times with exponential backoff
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import static com.microsoft.semantickernel.contextvariables.ContextVariableTypes.convert;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.microsoft.semantickernel.Kernel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.samples.syntaxexamples;

import com.azure.ai.openai.OpenAIAsyncClient;
Expand Down
Loading
Loading