-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add enterprise and batch file search. * Fix
- Loading branch information
Showing
13 changed files
with
772 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
sdk/ai/Azure.AI.Projects/src/Custom/Agent/FileSearchToolResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Azure.AI.Projects | ||
{ | ||
public partial class FileSearchToolResource | ||
{ | ||
public FileSearchToolResource( | ||
IList<string> vectorStoreIds, | ||
IList<VectorStoreConfigurations> vectorStores | ||
) | ||
{ | ||
VectorStoreIds = vectorStoreIds; | ||
if (vectorStores == null) | ||
VectorStores = new ChangeTrackingList<VectorStoreConfigurations>(); | ||
else | ||
VectorStores = vectorStores; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
sdk/ai/Azure.AI.Projects/src/Custom/Agent/MessageAttachment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
namespace Azure.AI.Projects; | ||
|
||
public partial class MessageAttachment | ||
{ | ||
public MessageAttachment(VectorStoreDataSource ds, List<ToolDefinition> tools) | ||
{ | ||
FileId = null; | ||
DataSource = ds; | ||
Tools = serializeJson(tools); | ||
_serializedAdditionalRawData = null; | ||
} | ||
|
||
public MessageAttachment(string fileId, List<ToolDefinition> tools) | ||
{ | ||
FileId = fileId; | ||
DataSource = null; | ||
Tools = serializeJson(tools); | ||
_serializedAdditionalRawData = null; | ||
} | ||
|
||
private static List<BinaryData> serializeJson<T>(List<T> definitions) where T: IJsonModel<T> | ||
{ | ||
List<BinaryData> serializedDefinitions = new(); | ||
foreach (IJsonModel<T> definition in definitions) | ||
{ | ||
var stream = new MemoryStream(); | ||
var writer = new Utf8JsonWriter(stream); | ||
definition.Write(writer, ModelReaderWriterOptions.Json); | ||
writer.Flush(); | ||
string json = Encoding.UTF8.GetString(stream.ToArray()); | ||
serializedDefinitions.Add(new BinaryData(json)); | ||
} | ||
return serializedDefinitions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
sdk/ai/Azure.AI.Projects/tests/Samples/Agent/Sample_Agent_Enterprise_File_Search.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Mail; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
using NUnit.Framework.Internal.Execution; | ||
|
||
namespace Azure.AI.Projects.Tests; | ||
|
||
public partial class Sample_Agent_Enterprise_File_Search : SamplesBase<AIProjectsTestEnvironment> | ||
{ | ||
[Test] | ||
public async Task EnterpriseFileSearch() | ||
{ | ||
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING; | ||
// For now we will take the File URI from the environment variables. | ||
// In future we may want to upload file to Azure here. | ||
var blobURI = TestEnvironment.AZURE_BLOB_URI; | ||
var modelName = TestEnvironment.MODELDEPLOYMENTNAME; | ||
AgentsClient client = new AgentsClient(connectionString, new DefaultAzureCredential()); | ||
|
||
#region Snippet:CreateVectorStoreBlob | ||
var ds = new VectorStoreDataSource( | ||
assetIdentifier: blobURI, | ||
assetType: VectorStoreDataSourceAssetType.UriAsset | ||
); | ||
var vectorStoreTask = await client.CreateVectorStoreAsync( | ||
name: "sample_vector_store", | ||
storeConfiguration: new VectorStoreConfiguration( | ||
dataSources: new List<VectorStoreDataSource> { ds } | ||
) | ||
); | ||
var vectorStore = vectorStoreTask.Value; | ||
|
||
FileSearchToolResource fileSearchResource = new([vectorStore.Id], null); | ||
|
||
List<ToolDefinition> tools = [new FileSearchToolDefinition()]; | ||
Response<Agent> agentResponse = await client.CreateAgentAsync( | ||
model: modelName, | ||
name: "my-assistant", | ||
instructions: "You are helpful assistant.", | ||
tools: tools, | ||
toolResources: new ToolResources() { FileSearch = fileSearchResource } | ||
); | ||
#endregion | ||
Response<AgentThread> threadResponse = await client.CreateThreadAsync(); | ||
AgentThread thread = threadResponse.Value; | ||
|
||
Response<ThreadMessage> messageResponse = await client.CreateMessageAsync( | ||
threadId: thread.Id, | ||
role: MessageRole.User, | ||
content: "What feature does Smart Eyewear offer?" | ||
); | ||
ThreadMessage message = messageResponse.Value; | ||
|
||
Response<ThreadRun> runResponse = await client.CreateRunAsync( | ||
thread.Id, | ||
agentResponse.Value.Id | ||
); | ||
ThreadRun run = runResponse.Value; | ||
|
||
do | ||
{ | ||
await Task.Delay(TimeSpan.FromMilliseconds(500)); | ||
runResponse = await client.GetRunAsync(thread.Id, runResponse.Value.Id); | ||
} | ||
while (runResponse.Value.Status == RunStatus.Queued | ||
|| runResponse.Value.Status == RunStatus.InProgress); | ||
|
||
Response<PageableList<ThreadMessage>> afterRunMessagesResponse | ||
= await client.GetMessagesAsync(thread.Id); | ||
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data; | ||
WriteMessages(messages); | ||
|
||
var delTask = await client.DeleteVectorStoreAsync(vectorStore.Id); | ||
if (delTask.Value.Deleted) | ||
{ | ||
Console.WriteLine($"Deleted vector store {vectorStore.Id}"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Unable to delete vector store {vectorStore.Id}"); | ||
} | ||
await client.DeleteAgentAsync(agentResponse.Value.Id); | ||
} | ||
|
||
private void WriteMessages(IEnumerable<ThreadMessage> messages) | ||
{ | ||
foreach (ThreadMessage threadMessage in messages) | ||
{ | ||
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: "); | ||
foreach (MessageContent contentItem in threadMessage.ContentItems) | ||
{ | ||
if (contentItem is MessageTextContent textItem) | ||
{ | ||
Console.Write(textItem.Text); | ||
} | ||
else if (contentItem is MessageImageFileContent imageFileItem) | ||
{ | ||
Console.Write($"<image from ID: {imageFileItem.FileId}"); | ||
} | ||
Console.WriteLine(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.