Skip to content

Commit

Permalink
Merge pull request #254 from johnoliver/bump-oai
Browse files Browse the repository at this point in the history
Bump azure-ai-openai
  • Loading branch information
johnoliver authored Oct 28, 2024
2 parents 4bfe3e9 + 6a6dece commit 43c6390
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.3.1

- Upgrade to azure-ai-openai 1.0.0-beta.12

# 1.3.0

- Added support for Json Schema to Open AI Chat Completions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.chatcompletion;

import com.azure.core.util.BinaryData;
import javax.annotation.Nullable;

public class BinaryDataUtils {

@Nullable
public static String toString(@Nullable BinaryData b) {
if (b == null) {
return null;
}
return b.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.ai.openai.models.ChatCompletions;
import com.azure.ai.openai.models.ChatCompletionsFunctionToolCall;
import com.azure.ai.openai.models.ChatCompletionsFunctionToolDefinition;
import com.azure.ai.openai.models.ChatCompletionsFunctionToolDefinitionFunction;
import com.azure.ai.openai.models.ChatCompletionsJsonResponseFormat;
import com.azure.ai.openai.models.ChatCompletionsNamedToolSelection;
import com.azure.ai.openai.models.ChatCompletionsOptions;
Expand All @@ -26,6 +27,7 @@
import com.azure.ai.openai.models.ChatResponseMessage;
import com.azure.ai.openai.models.CompletionsUsage;
import com.azure.ai.openai.models.FunctionCall;
import com.azure.ai.openai.models.FunctionDefinition;
import com.azure.json.JsonOptions;
import com.azure.json.implementation.DefaultJsonReader;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -734,7 +736,8 @@ private List<ChatMessageContent<?>> toOpenAIChatMessageContent(
if (message instanceof ChatRequestUserMessage) {
return new OpenAIChatMessageContent<>(
AuthorRole.USER,
((ChatRequestUserMessage) message).getContent().toString(),
BinaryDataUtils
.toString(((ChatRequestUserMessage) message).getContent()),
null,
null,
null,
Expand All @@ -743,7 +746,8 @@ private List<ChatMessageContent<?>> toOpenAIChatMessageContent(
} else if (message instanceof ChatRequestSystemMessage) {
return new OpenAIChatMessageContent<>(
AuthorRole.SYSTEM,
((ChatRequestSystemMessage) message).getContent(),
BinaryDataUtils
.toString(((ChatRequestSystemMessage) message).getContent()),
null,
null,
null,
Expand All @@ -755,7 +759,8 @@ private List<ChatMessageContent<?>> toOpenAIChatMessageContent(
((ChatRequestAssistantMessage) message).getToolCalls());
return new OpenAIChatMessageContent<>(
AuthorRole.ASSISTANT,
((ChatRequestAssistantMessage) message).getContent(),
BinaryDataUtils
.toString(((ChatRequestAssistantMessage) message).getContent()),
null,
null,
null,
Expand All @@ -767,7 +772,8 @@ private List<ChatMessageContent<?>> toOpenAIChatMessageContent(
} else if (message instanceof ChatRequestToolMessage) {
return new OpenAIChatMessageContent<>(
AuthorRole.TOOL,
((ChatRequestToolMessage) message).getContent(),
BinaryDataUtils
.toString(((ChatRequestToolMessage) message).getContent()),
null,
null,
null,
Expand Down Expand Up @@ -966,10 +972,14 @@ private static void configureToolCallBehaviorOptions(

List<ChatCompletionsToolDefinition> toolDefinitions = new ArrayList<>();

FunctionDefinition function = OpenAIFunction.toFunctionDefinition(
toolChoice.getMetadata(),
toolChoice.getPluginName());

toolDefinitions.add(new ChatCompletionsFunctionToolDefinition(
OpenAIFunction.toFunctionDefinition(
toolChoice.getMetadata(),
toolChoice.getPluginName())));
new ChatCompletionsFunctionToolDefinitionFunction(function.getName())
.setDescription(function.getDescription())
.setParameters(function.getParameters())));

options.setTools(toolDefinitions);
try {
Expand Down Expand Up @@ -1003,7 +1013,10 @@ private static void configureToolCallBehaviorOptions(
function.getName());
})
.map(OpenAIFunction::getFunctionDefinition)
.map(ChatCompletionsFunctionToolDefinition::new)
.map(it -> new ChatCompletionsFunctionToolDefinitionFunction(it.getName())
.setDescription(it.getDescription())
.setParameters(it.getParameters()))
.map(it -> new ChatCompletionsFunctionToolDefinition(it))
.collect(Collectors.toList());

if (toolDefinitions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,23 @@ public static ChatRequestMessage unescapeRequest(ChatRequestMessage message) {
if (message instanceof ChatRequestUserMessage) {
ChatRequestUserMessage chatRequestMessage = (ChatRequestUserMessage) message;
String content = StringEscapeUtils.unescapeXml(
chatRequestMessage.getContent().toString());
BinaryDataUtils.toString(chatRequestMessage.getContent()));

return new ChatRequestUserMessage(content)
.setName(chatRequestMessage.getName());
} else if (message instanceof ChatRequestSystemMessage) {
ChatRequestSystemMessage chatRequestMessage = (ChatRequestSystemMessage) message;
String content = StringEscapeUtils.unescapeXml(chatRequestMessage.getContent());
String content = StringEscapeUtils
.unescapeXml(
BinaryDataUtils.toString(chatRequestMessage.getContent()));

return new ChatRequestSystemMessage(content)
.setName(chatRequestMessage.getName());
} else if (message instanceof ChatRequestAssistantMessage) {
ChatRequestAssistantMessage chatRequestMessage = (ChatRequestAssistantMessage) message;
String content = StringEscapeUtils.unescapeXml(chatRequestMessage.getContent());
String content = StringEscapeUtils
.unescapeXml(
BinaryDataUtils.toString(chatRequestMessage.getContent()));

return new ChatRequestAssistantMessage(content)
.setToolCalls(chatRequestMessage.getToolCalls())
Expand All @@ -145,7 +149,9 @@ public static ChatRequestMessage unescapeRequest(ChatRequestMessage message) {
content);
} else if (message instanceof ChatRequestToolMessage) {
ChatRequestToolMessage chatRequestMessage = (ChatRequestToolMessage) message;
String content = StringEscapeUtils.unescapeXml(chatRequestMessage.getContent());
String content = StringEscapeUtils
.unescapeXml(
BinaryDataUtils.toString(chatRequestMessage.getContent()));

return new ChatRequestToolMessage(
content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ public void canHandleIterableReturnFromFunction() {
.block();

String requestBody = wm.getAllServeEvents().get(0).getRequest().getBodyAsString();
Assertions.assertTrue(requestBody.contains("{\"content\":\"[]"));
Assertions.assertTrue(requestBody.contains("{\"content\":\"[1, 2, 3]"));
Assertions.assertTrue(requestBody.contains("{\"content\":\"[4, 5, 6]"));
Assertions.assertTrue(requestBody.contains("{\"content\":\"[7, 8, 9]"));
Assertions.assertTrue(requestBody.contains("\"content\":\"[]"));
Assertions.assertTrue(requestBody.contains("\"content\":\"[1, 2, 3]"));
Assertions.assertTrue(requestBody.contains("\"content\":\"[4, 5, 6]"));
Assertions.assertTrue(requestBody.contains("\"content\":\"[7, 8, 9]"));
}

private Kernel buildTextKernel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private ChatCompletionService getChatCompletionService() {

public static MappingBuilder buildTextResponse(String bodyMatcher, String responseBody) {
return post(urlEqualTo(
"//openai/deployments/gpt-35-turbo-2/completions?api-version=2024-07-01-preview"))
"//openai/deployments/gpt-35-turbo-2/completions?api-version=2024-08-01-preview"))
.withRequestBody(new ContainsPattern(bodyMatcher))
.willReturn(
aResponse()
Expand All @@ -293,7 +293,7 @@ public static MappingBuilder buildTextResponse(String bodyMatcher, String respon

public static MappingBuilder buildResponse(String bodyMatcher, String responseBody) {
return post(urlEqualTo(
"//openai/deployments/gpt-35-turbo-2/chat/completions?api-version=2024-07-01-preview"))
"//openai/deployments/gpt-35-turbo-2/chat/completions?api-version=2024-08-01-preview"))
.withRequestBody(new ContainsPattern(bodyMatcher))
.willReturn(
aResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"priority": 1,
"request": {
"method": "POST",
"url": "//openai/deployments/gpt-35-turbo-2/chat/completions?api-version=2024-07-01-preview",
"url": "//openai/deployments/gpt-35-turbo-2/chat/completions?api-version=2024-08-01-preview",
"bodyPatterns": [
{
"contains": "That is all"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "POST",
"url": "//openai/deployments/gpt-35-turbo-2/chat/completions?api-version=2024-07-01-preview"
"url": "//openai/deployments/gpt-35-turbo-2/chat/completions?api-version=2024-08-01-preview"
},
"response": {
"body": "{\"id\":\"chatcmpl-xxx\",\"object\":\"chat.completion\",\"created\":1707253061,\"model\":\"gpt-35-turbo\",\"prompt_filter_results\":[{\"prompt_index\":0,\"content_filter_results\":{\"hate\":{\"filtered\":false,\"severity\":\"safe\"},\"self_harm\":{\"filtered\":false,\"severity\":\"safe\"},\"sexual\":{\"filtered\":false,\"severity\":\"safe\"},\"violence\":{\"filtered\":false,\"severity\":\"safe\"}}}],\"choices\":[{\"finish_reason\":\"stop\",\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"1979b4ce-5463-4cfb-8ec8-1d05c4b44ccf\"},\"content_filter_results\":{\"hate\":{\"filtered\":false,\"severity\":\"safe\"},\"self_harm\":{\"filtered\":false,\"severity\":\"safe\"},\"sexual\":{\"filtered\":false,\"severity\":\"safe\"},\"violence\":{\"filtered\":false,\"severity\":\"safe\"}}}],\"usage\":{\"prompt_tokens\":17,\"completion_tokens\":67,\"total_tokens\":84}}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "POST",
"url": "//openai/deployments/gpt-35-turbo/chat/completions?api-version=2024-07-01-preview"
"url": "//openai/deployments/gpt-35-turbo/chat/completions?api-version=2024-08-01-preview"
},
"response": {
"body": "{\"id\":\"chatcmpl-xxx\",\"object\":\"chat.completion\",\"created\":1707253039,\"model\":\"gpt-35-turbo\",\"prompt_filter_results\":[{\"prompt_index\":0,\"content_filter_results\":{\"hate\":{\"filtered\":false,\"severity\":\"safe\"},\"self_harm\":{\"filtered\":false,\"severity\":\"safe\"},\"sexual\":{\"filtered\":false,\"severity\":\"safe\"},\"violence\":{\"filtered\":false,\"severity\":\"safe\"}}}],\"choices\":[{\"finish_reason\":\"stop\",\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"ac9817bc-7e1a-48e4-b06c-0ff7618b88c6\"},\"content_filter_results\":{\"hate\":{\"filtered\":false,\"severity\":\"safe\"},\"self_harm\":{\"filtered\":false,\"severity\":\"safe\"},\"sexual\":{\"filtered\":false,\"severity\":\"safe\"},\"violence\":{\"filtered\":false,\"severity\":\"safe\"}}}],\"usage\":{\"prompt_tokens\":26,\"completion_tokens\":131,\"total_tokens\":157}}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "POST",
"url": "//openai/deployments/text-davinci-003/completions?api-version=2024-07-01-preview"
"url": "//openai/deployments/text-davinci-003/completions?api-version=2024-08-01-preview"
},
"response": {
"body": "{\"id\":\"cmpl-xxx\",\"object\":\"text_completion\",\"created\":1707253062,\"model\":\"text-davinci-003\",\"prompt_filter_results\":[{\"prompt_index\":0,\"content_filter_results\":{\"hate\":{\"filtered\":false,\"severity\":\"safe\"},\"self_harm\":{\"filtered\":false,\"severity\":\"safe\"},\"sexual\":{\"filtered\":false,\"severity\":\"safe\"},\"violence\":{\"filtered\":false,\"severity\":\"safe\"}}}],\"choices\":[{\"text\":\"7949e981-c632-422f-9b76-335a2379cd83\",\"index\":0,\"finish_reason\":\"stop\",\"logprobs\":null,\"content_filter_results\":{\"hate\":{\"filtered\":false,\"severity\":\"safe\"},\"self_harm\":{\"filtered\":false,\"severity\":\"safe\"},\"sexual\":{\"filtered\":false,\"severity\":\"safe\"},\"violence\":{\"filtered\":false,\"severity\":\"safe\"}}}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":26,\"total_tokens\":36}}",
Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
<description>Parent pom for the Semantic Kernel Project</description>

<properties>
<!-- TODO change this to beta.12 (which has some API changes) -->
<azure-ai-openai.version>1.0.0-beta.11</azure-ai-openai.version>
<azure-ai-openai.version>1.0.0-beta.12</azure-ai-openai.version>
<checkstyle.version>10.18.2</checkstyle.version>
<com.uber.nullaway.version>0.10.21</com.uber.nullaway.version>
<compilingJdk8>false</compilingJdk8>
Expand Down
3 changes: 1 addition & 2 deletions semantickernel-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<!-- change to beta.12 -->
<version>1.0.0-beta.11</version>
<version>1.0.0-beta.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
Expand Down

0 comments on commit 43c6390

Please sign in to comment.