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

[azopenai] Adding in support for using Azure Chat Extensions with external data sources #21426

Merged
merged 7 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 5 additions & 2 deletions sdk/ai/azopenai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Release History

## 0.1.2 (Unreleased)
## 0.2.0 (Unreleased)

### Features Added

- ChatCompletions supports Azure OpenAI's newest feature to use Azure OpenAI with your own data. See `example_client_getchatcompletions_extensions_test.go`
for a working example. (PR#21426)

### Breaking Changes

- ChatCompletionsOptions, CompletionsOptions, EmbeddingsOptions `DeploymentID` field renamed to `Deployment`.
Expand All @@ -13,7 +16,7 @@

- EventReader, used by GetChatCompletionsStream and GetCompletionsStream for streaming results, would not return an
error if the underlying Body reader was closed or EOF'd before the actual DONE: token arrived. This could result in an
infinite loop for callers. (PR#)
infinite loop for callers. (PR#21323)

### Other Changes

Expand Down
2 changes: 1 addition & 1 deletion sdk/ai/azopenai/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/ai/azopenai",
"Tag": "go/ai/azopenai_2bf13bba09"
"Tag": "go/ai/azopenai_7be6ae3c15"
}
50 changes: 50 additions & 0 deletions sdk/ai/azopenai/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ directive:
- from: openapi-document
where: $.components.schemas["ChatChoice"].properties.finish_reason
transform: $["$ref"] = "#/components/schemas/CompletionsFinishReason"; delete $.oneOf;
- from: openapi-document
where: $.components.schemas["AzureChatExtensionConfiguration"].properties.type
transform: $["$ref"] = "#/components/schemas/AzureChatExtensionType"; delete $.allOf;
- from: openapi-document
where: $.components.schemas["AzureChatExtensionConfiguration"].properties.type
transform: $["$ref"] = "#/components/schemas/AzureChatExtensionType"; delete $.allOf;
- from: openapi-document
where: $.components.schemas["AzureCognitiveSearchChatExtensionConfiguration"].properties.queryType
transform: $["$ref"] = "#/components/schemas/AzureCognitiveSearchQueryType"; delete $.allOf;
# Fix "AutoGenerated" models
- from: openapi-document
where: $.components.schemas["ChatCompletions"].properties.usage
Expand Down Expand Up @@ -294,4 +303,45 @@ directive:
return $
.replace(/populate\(objectMap, "model", (c|e).Model\)/g, 'populate(objectMap, "model", &$1.Deployment)')
.replace(/err = unpopulate\(val, "Model", &(c|e).Model\)/g, 'err = unpopulate(val, "Model", &$1.Deployment)');

# Make the Azure extensions internal - we expose these through the GetChatCompletions*() functions
# and just treat which endpoint we use as an implementation detail.
- from: client.go
where: $
transform: |
return $
.replace(/GetChatCompletionsWithAzureExtensions([ (])/g, "getChatCompletionsWithAzureExtensions$1")
.replace(/GetChatCompletions([ (])/g, "getChatCompletions$1");

# move the Azure extensions options into place
- from: models.go
where: $
transform: return $.replace(/(\/\/ The configuration entries for Azure OpenAI.+?)DataSources \[\]AzureChatExtensionConfiguration/s, "$1AzureExtensionsOptions *AzureChatExtensionOptions");
- from: models_serde.go
where: $
transform: |
return $
.replace(/populate\(objectMap, "dataSources", c.DataSources\)/, 'if c.AzureExtensionsOptions != nil { populate(objectMap, "dataSources", c.AzureExtensionsOptions.Extensions) }')
// technically not used, but let's be completionists...
.replace(/err = unpopulate\(val, "DataSources", &c.DataSources\)/, 'c.AzureExtensionsOptions = &AzureChatExtensionOptions{}; err = unpopulate(val, "DataSources", &c.AzureExtensionsOptions.Extensions)')

# try to fix some of the generated types.

# swap the `Parameters` and `Type` fields (Type really drives what's in Parameters)
- from: models.go
where: $
transform: |
let typeRE = /(\/\/ REQUIRED; The label for the type of an Azure chat extension.*?Type \*AzureChatExtensionType)/s;
let paramsRE = /(\/\/ REQUIRED; The configuration payload used for the Azure chat extension.*?Parameters any)/s;

return $
.replace(paramsRE, "")
.replace(typeRE, $.match(typeRE)[1] + "\n\n" + $.match(paramsRE)[1]);

- from: constants.go
where: $
transform: |
return $.replace(
/(AzureChatExtensionTypeAzureCognitiveSearch AzureChatExtensionType)/,
"// AzureChatExtensionTypeAzureCognitiveSearch enables the use of an Azure Cognitive Search index with chat completions.\n// [AzureChatExtensionConfiguration.Parameter] should be of type [AzureCognitiveSearchChatExtensionConfiguration].\n$1");
```
6 changes: 6 additions & 0 deletions sdk/ai/azopenai/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ stages:
OPENAI_EMBEDDINGS_MODEL: $(OPENAI-EMBEDDINGS-MODEL)
OPENAI_CHAT_COMPLETIONS_MODEL: $(OPENAI-CHAT-COMPLETIONS-MODEL)
OPENAI_COMPLETIONS_MODEL: $(OPENAI-COMPLETIONS-MODEL)

# used for BYOD scenarios with ChatCompletions
COGNITIVE_SEARCH_API_ENDPOINT: $(COGNITIVE-SEARCH-API-ENDPOINT)
COGNITIVE_SEARCH_API_INDEX: $(COGNITIVE-SEARCH-API-INDEX)
COGNITIVE_SEARCH_API_KEY: $(COGNITIVE-SEARCH-API-KEY)

80 changes: 66 additions & 14 deletions sdk/ai/azopenai/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions sdk/ai/azopenai/client_chat_completions_extensions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//go:build go1.18
// +build go1.18

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package azopenai_test

import (
"context"
"errors"
"io"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/stretchr/testify/require"
)

func TestChatCompletions_extensions_bringYourOwnData(t *testing.T) {
client := newAzureOpenAIClientForTest(t, azureOpenAI)

resp, err := client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
Messages: []azopenai.ChatMessage{
{Content: to.Ptr("What does PR complete mean?"), Role: to.Ptr(azopenai.ChatRoleUser)},
},
MaxTokens: to.Ptr[int32](512),
AzureExtensionsOptions: &azopenai.AzureChatExtensionOptions{
Extensions: []azopenai.AzureChatExtensionConfiguration{
{
Type: to.Ptr(azopenai.AzureChatExtensionTypeAzureCognitiveSearch),
Parameters: azureOpenAI.Cognitive,
},
},
},
Deployment: "gpt-4",
}, nil)
require.NoError(t, err)

// when you BYOD you get some extra content showing you metadata/info from the external
// data source.
msgContext := resp.Choices[0].Message.Context
require.NotEmpty(t, msgContext.Messages[0].Content)
require.Equal(t, azopenai.ChatRoleTool, *msgContext.Messages[0].Role)

require.NotEmpty(t, *resp.Choices[0].Message.Content)
require.Equal(t, azopenai.CompletionsFinishReasonStop, *resp.Choices[0].FinishReason)
}

func TestChatExtensionsStreaming_extensions_bringYourOwnData(t *testing.T) {
client := newAzureOpenAIClientForTest(t, azureOpenAI)

streamResp, err := client.GetChatCompletionsStream(context.Background(), azopenai.ChatCompletionsOptions{
Messages: []azopenai.ChatMessage{
{Content: to.Ptr("What does PR complete mean?"), Role: to.Ptr(azopenai.ChatRoleUser)},
},
MaxTokens: to.Ptr[int32](512),
AzureExtensionsOptions: &azopenai.AzureChatExtensionOptions{
Extensions: []azopenai.AzureChatExtensionConfiguration{
{
Type: to.Ptr(azopenai.AzureChatExtensionTypeAzureCognitiveSearch),
Parameters: azureOpenAI.Cognitive,
},
},
},
Deployment: "gpt-4",
}, nil)

require.NoError(t, err)
defer streamResp.ChatCompletionsStream.Close()

text := ""

first := false

for {
event, err := streamResp.ChatCompletionsStream.Read()

if errors.Is(err, io.EOF) {
break
}

require.NoError(t, err)

if first {
// when you BYOD you get some extra content showing you metadata/info from the external
// data source.
first = false
msgContext := event.Choices[0].Message.Context
require.NotEmpty(t, msgContext.Messages[0].Content)
require.Equal(t, azopenai.ChatRoleTool, *msgContext.Messages[0].Role)
}

for _, choice := range event.Choices {
if choice.Delta != nil && choice.Delta.Content != nil {
text += *choice.Delta.Content
}
}
}

require.NotEmpty(t, text)
}
Loading