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

Implement ResourceBundle usage #198

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.microsoft.semantickernel.exceptions.AIException;
import com.microsoft.semantickernel.exceptions.SKCheckedException;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.orchestration.FunctionResult;
import com.microsoft.semantickernel.orchestration.FunctionResultMetadata;
import com.microsoft.semantickernel.orchestration.InvocationContext;
Expand Down Expand Up @@ -97,7 +98,8 @@ private Mono<List<ChatMessageContent<?>>> internalChatMessageContentsAsync(
try {
GenerativeModel model = getGenerativeModel(kernel, invocationContext);
return MonoConverter.fromApiFuture(model.generateContentAsync(contents))
.doOnError(e -> LOGGER.error("Error generating chat completion", e))
.doOnError(e -> LOGGER.error(
SemanticKernelResources.getString("error.generating.chat.completion"), e))
.flatMap(result -> {
// Get ChatMessageContent from the response
GeminiChatMessageContent<?> response = getGeminiChatMessageContentFromResponse(
Expand Down Expand Up @@ -253,7 +255,8 @@ private GenerativeModel getGenerativeModel(@Nullable Kernel kernel,

if (settings.getResultsPerPrompt() < 1
|| settings.getResultsPerPrompt() > MAX_RESULTS_PER_PROMPT) {
throw SKCheckedException.build("Error building generative model.",
throw SKCheckedException.build(
SemanticKernelResources.getString("error.building.generative.model"),
new AIException(AIException.ErrorCodes.INVALID_REQUEST,
String.format(
"Results per prompt must be in range between 1 and %d, inclusive.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.microsoft.semantickernel.contextvariables.CaseInsensitiveMap;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.plugin.KernelPlugin;
import com.microsoft.semantickernel.semanticfunctions.KernelFunction;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionMetadata;
Expand Down Expand Up @@ -122,7 +123,8 @@ KernelPlugin getPlugin(String pluginName) {
*/
void add(KernelPlugin plugin) {
if (plugins.containsKey(plugin.getName())) {
LOGGER.warn("Plugin {} already exists, overwriting existing plugin", plugin.getName());
LOGGER.warn(SemanticKernelResources.getString(
"plugin.already.exists.overwriting.existing.plugin"), plugin.getName());
}

plugins.put(plugin.getName(), plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.microsoft.semantickernel.contextvariables;

import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -144,7 +145,8 @@ public <U> U toObject(ContextVariableTypes types, @Nullable Object t, Class<U> c
return (U) converter.get().toObject((T) t);
}

LOGGER.warn("No converter found for {} to {}", t.getClass(), clazz);
LOGGER.warn(SemanticKernelResources.getString("no.converter.found.for.to"), t.getClass(),
clazz);
return null;
}

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

import com.microsoft.semantickernel.localization.SemanticKernelResources;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -65,53 +66,57 @@ public enum ErrorCodes {
/**
* Unknown error.
*/
UNKNOWN_ERROR("Unknown error"),
UNKNOWN_ERROR(SemanticKernelResources.getString("unknown.error")),

/**
* No response.
*/
NO_RESPONSE("No response"),
NO_RESPONSE(SemanticKernelResources.getString("no.response")),
/**
* Access denied.
*/
ACCESS_DENIED("Access is denied"),
ACCESS_DENIED(SemanticKernelResources.getString("access.is.denied")),

/**
* Invalid request.
*/
INVALID_REQUEST("The request was invalid"),
INVALID_REQUEST(SemanticKernelResources.getString("the.request.was.invalid")),
/**
* Invalid response.
*/
INVALID_RESPONSE_CONTENT("The content of the response was invalid"),
INVALID_RESPONSE_CONTENT(
SemanticKernelResources.getString("the.content.of.the.response.was.invalid")),

/**
* Throttling.
*/
THROTTLING("The request was throttled"),
THROTTLING(SemanticKernelResources.getString("the.request.was.throttled")),
/**
* Request timeout.
*/
REQUEST_TIMEOUT("The request timed out"),
REQUEST_TIMEOUT(SemanticKernelResources.getString("the.request.timed.out")),

/**
* Service error.
*/
SERVICE_ERROR("There was an error in the service"),
SERVICE_ERROR(SemanticKernelResources.getString("there.was.an.error.in.the.service")),

/**
* Model not available.
*/
MODEL_NOT_AVAILABLE("The requested model is not available"),
MODEL_NOT_AVAILABLE(
SemanticKernelResources.getString("the.requested.model.is.not.available")),

/**
* Invalid configuration.
*/
INVALID_CONFIGURATION("The supplied configuration was invalid"),
INVALID_CONFIGURATION(
SemanticKernelResources.getString("the.supplied.configuration.was.invalid")),
/**
* Function type not supported.
*/
FUNCTION_TYPE_NOT_SUPPORTED("The function is not supported");
FUNCTION_TYPE_NOT_SUPPORTED(
SemanticKernelResources.getString("the.function.is.not.supported"));

private final String message;

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

import com.microsoft.semantickernel.localization.SemanticKernelResources;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -65,27 +66,31 @@ public enum ErrorCodes {
/**
* Unknown error
*/
UNKNOWN_ERROR("Unknown error"),
UNKNOWN_ERROR(SemanticKernelResources.getString("unknown.error")),

/**
* Could not find configuration file
*/
CONFIGURATION_NOT_FOUND("Could not find configuration file"),
CONFIGURATION_NOT_FOUND(
SemanticKernelResources.getString("could.not.find.configuration.file")),

/**
* Could not parse or load configuration file
*/
COULD_NOT_READ_CONFIGURATION("Could not parse or load configuration file"),
COULD_NOT_READ_CONFIGURATION(
SemanticKernelResources.getString("could.not.parse.or.load.configuration.file")),

/**
* Could not find any valid configuration settings
*/
NO_VALID_CONFIGURATIONS_FOUND("Could not find any valid configuration settings"),
NO_VALID_CONFIGURATIONS_FOUND(
SemanticKernelResources.getString("could.not.find.any.valid.configuration.settings")),

/**
* Could not find value for configuration key
*/
VALUE_NOT_FOUND("Could not find value for configuration key");
VALUE_NOT_FOUND(
SemanticKernelResources.getString("could.not.find.value.for.configuration.key"));

private final String message;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
package com.microsoft.semantickernel.implementation;

import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -126,7 +128,10 @@ private static String readInputStream(String fileName, InputStream inputStream)
return bf.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
// IGNORE
LOGGER.trace("Failed to load file: " + fileName, e);
LOGGER.trace(
MessageFormat.format(SemanticKernelResources.getString("failed.to.load.file.0"),
fileName),
e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.ServiceLoader;
import java.util.function.Supplier;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,11 +55,13 @@ public static <T> Supplier<T> findServiceLoader(Class<T> clazz, String alternati
| IllegalAccessException
| NoSuchMethodException
| RuntimeException e) {
LOGGER.error("Unable to load service " + clazz.getName() + " ", e);
LOGGER.error(String.format(
SemanticKernelResources.getString("unable.to.load.service.s"), clazz.getName()),
e);
}

if (impl == null) {
throw new RuntimeException("Service not found: " + clazz.getName());
throw new RuntimeException(String.format("Service not found: %s", clazz.getName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.orchestration.ToolCallBehavior;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -203,7 +204,7 @@ private static <T> ChatPromptParseVisitor<T> getFunctionDefinitions(String promp
}
}
} catch (IOException | XMLStreamException | IllegalArgumentException e) {
LOGGER.error("Error parsing prompt", e);
LOGGER.error(SemanticKernelResources.getString("error.parsing.prompt"), e);
}
return chatPromptParseVisitor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.microsoft.semantickernel.contextvariables.ContextVariableType;
import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.orchestration.FunctionResult;
import com.microsoft.semantickernel.orchestration.InvocationContext;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionArguments;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionMetadata;
import com.microsoft.semantickernel.templateengine.semantickernel.TemplateException;
import com.microsoft.semantickernel.templateengine.semantickernel.TemplateException.ErrorCodes;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -44,12 +46,14 @@ public CodeBlock(List<Block> tokens, String content) {
public boolean isValid() {
Optional<Block> invalid = tokens.stream().filter(token -> !token.isValid()).findFirst();
if (invalid.isPresent()) {
LOGGER.error("Invalid block" + invalid.get().getContent());
LOGGER.error(MessageFormat.format(SemanticKernelResources.getString("invalid.block.0"),
invalid.get().getContent()));
return false;
}

if (!this.tokens.isEmpty() && this.tokens.get(0).getType() == BlockTypes.NAMED_ARG) {
LOGGER.error("Unexpected named argument found. Expected function name first.");
LOGGER.error(SemanticKernelResources.getString(
"unexpected.named.argument.found.expected.function.name.first"));
return false;
}

Expand All @@ -62,23 +66,27 @@ public boolean isValid() {

private boolean isValidFunctionCall() {
if (this.tokens.get(0).getType() != BlockTypes.FUNCTION_ID) {
LOGGER.error("Unexpected second token found: " + tokens.get(1).getContent());
LOGGER.error(MessageFormat.format(
SemanticKernelResources.getString("unexpected.second.token.found.0"),
tokens.get(1).getContent()));
return false;
}

if (this.tokens.get(1).getType() != BlockTypes.VALUE &&
this.tokens.get(1).getType() != BlockTypes.VARIABLE &&
this.tokens.get(1).getType() != BlockTypes.NAMED_ARG) {
LOGGER.error(
"The first arg of a function must be a quoted string, variable or named argument");
SemanticKernelResources.getString(
"the.first.arg.of.a.function.must.be.a.quoted.string.variable.or.named.argument"));
return false;
}

for (int i = 2; i < this.tokens.size(); i++) {
if (this.tokens.get(i).getType() != BlockTypes.NAMED_ARG) {
LOGGER.error(
"Functions only support named arguments after the first argument. Argument " + i
+ " is not named.");
SemanticKernelResources.getString(
"functions.only.support.named.arguments.after.the.first.argument"),
i);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.implementation.Verify;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionArguments;
import javax.annotation.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -109,18 +110,20 @@ private static String splitAndGetPart(String text, int x) {
@Override
public boolean isValid() {
if (Verify.isNullOrEmpty(this.name)) {
LOGGER.error("A named argument must have a name");
LOGGER.error(SemanticKernelResources.getString("a.named.argument.must.have.a.name"));
return false;
}

if (this.valBlock != null && !this.valBlock.isValid()) {
LOGGER.error("There was an issue with the named argument value for '" + name);
LOGGER.error(SemanticKernelResources.getString(
"there.was.an.issue.with.the.named.argument.value.for"), name);
return false;
} else if (this.varBlock != null && !this.varBlock.isValid()) {
LOGGER.error("There was an issue with the named argument value for '" + name);
LOGGER.error(SemanticKernelResources.getString(
"there.was.an.issue.with.the.named.argument.value.for"), name);
return false;
} else if (this.valBlock == null && this.varBlock == null) {
LOGGER.error("A named argument must have a value");
LOGGER.error(SemanticKernelResources.getString("a.named.argument.must.have.a.value"));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.microsoft.semantickernel.implementation.templateengine.tokenizer.blocks;

import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionArguments;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -22,7 +23,8 @@ public ValBlock(String quotedValue) {
super(quotedValue.trim(), BlockTypes.VALUE);

if (this.getContent().length() < 2) {
LOGGER.error("A value must have single quotes or double quotes on both sides");
LOGGER.error(SemanticKernelResources.getString(
"a.value.must.have.single.quotes.or.double.quotes.on.both.sides"));
return;
}

Expand All @@ -47,15 +49,16 @@ public String render(ContextVariableTypes types, @Nullable KernelFunctionArgumen
public boolean isValid() {
// Content includes the quotes, so it must be at least 2 chars long
if (this.getContent().length() < 2) {
LOGGER.error("A value must have single quotes or double quotes on both sides");
LOGGER.error(SemanticKernelResources.getString(
"a.value.must.have.single.quotes.or.double.quotes.on.both.sides"));
return false;
}

// Check if delimiting chars are consistent
if (first != last) {
LOGGER.error(
"A value must be defined using either single quotes or double quotes, not"
+ " both");
SemanticKernelResources.getString(
"a.value.must.be.defined.using.either.single.quotes.or.double.quotes.not.both"));
return false;
}

Expand Down
Loading
Loading