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

updates for demo #46997

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<PackageReference Update="System.ClientModel" Version="1.2.1" />
<PackageReference Update="System.IO.Hashing" Version="6.0.0" />
<PackageReference Update="System.Memory" Version="4.5.5" />
<PackageReference Update="System.Memory.Data" Version="6.0.0" />
<PackageReference Update="System.Memory.Data" Version="8.0.0" />
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Update="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Update="System.Net.Http" Version="4.3.4" />
<PackageReference Update="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public readonly partial struct MessagingServices
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public void SendMessage(object serializable) { }
public void SendJson(object serializable) { }
public System.Threading.Tasks.Task SendJsonAsync(object serializable) { throw null; }
public void WhenMessageReceived(System.Action<string> received) { }
}
public partial class StorageFile
Expand All @@ -50,14 +51,15 @@ public readonly partial struct StorageServices
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public void DeleteBlob(string path) { }
public System.BinaryData DownloadBlob(string path) { throw null; }
public string UploadBinaryData(System.BinaryData data, string name = null, bool overwrite = false) { throw null; }
public string UploadBytes(byte[] bytes, string name = null, bool overwrite = false) { throw null; }
public string UploadBytes(System.ReadOnlyMemory<byte> bytes, string name = null, bool overwrite = false) { throw null; }
public void Delete(string path) { }
public System.BinaryData Download(string path) { throw null; }
public string Upload(System.BinaryData data, string name = null, bool overwrite = false) { throw null; }
public string Upload(System.IO.Stream fileStream, string name = null, string contentType = null, bool overwrite = false) { throw null; }
public System.Threading.Tasks.Task<string> UploadAsync(System.BinaryData data, string name = null, bool overwrite = false) { throw null; }
public System.Threading.Tasks.Task<string> UploadAsync(System.IO.Stream fileStream, string name = null, string contentType = null, bool overwrite = false) { throw null; }
public string UploadJson(object json, string name = null, bool overwrite = false) { throw null; }
public string UploadStream(System.IO.Stream fileStream, string name = null, bool overwrite = false) { throw null; }
public void WhenBlobUploaded(System.Action<Azure.CloudMachine.StorageFile> function) { }
public void WhenUploaded(System.Action<Azure.CloudMachine.StorageFile> function) { }
public void WhenUploaded(System.Action<System.BinaryData> function) { }
}
}
namespace Azure.CloudMachine.KeyVault
Expand All @@ -71,12 +73,11 @@ namespace Azure.CloudMachine.OpenAI
{
public static partial class AzureOpenAIExtensions
{
public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages, System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.VectorbaseEntry> entries) { }
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
public static void Trim(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages) { }
}
}
namespace Azure.CloudMachine.OpenAI.Chat
{
public partial class ChatTools
{
public ChatTools(params System.Type[] tools) { }
Expand All @@ -92,14 +93,11 @@ public void Add(System.Type functions) { }
protected virtual string GetMethodInfoToName(System.Reflection.MethodInfo function) { throw null; }
protected virtual string GetParameterInfoToDescription(System.Reflection.ParameterInfo parameter) { throw null; }
}
}
namespace Azure.CloudMachine.OpenAI.Embeddings
{
public partial class EmbeddingsVectorbase
{
public EmbeddingsVectorbase(OpenAI.Embeddings.EmbeddingClient client, Azure.CloudMachine.OpenAI.Embeddings.VectorbaseStore store = null, int factChunkSize = 0) { }
public EmbeddingsVectorbase(OpenAI.Embeddings.EmbeddingClient client, Azure.CloudMachine.OpenAI.VectorbaseStore store = null, int factChunkSize = 0) { }
public void Add(string text) { }
public System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry> Find(string text, Azure.CloudMachine.OpenAI.Embeddings.FindOptions options = null) { throw null; }
public System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.VectorbaseEntry> Find(string text, Azure.CloudMachine.OpenAI.FindOptions options = null) { throw null; }
}
public partial class FindOptions
{
Expand All @@ -120,10 +118,10 @@ public readonly partial struct VectorbaseEntry
public abstract partial class VectorbaseStore
{
protected VectorbaseStore() { }
public abstract int Add(Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry entry);
public abstract void Add(System.Collections.Generic.IReadOnlyList<Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry> entry);
public abstract int Add(Azure.CloudMachine.OpenAI.VectorbaseEntry entry);
public abstract void Add(System.Collections.Generic.IReadOnlyList<Azure.CloudMachine.OpenAI.VectorbaseEntry> entry);
public static float CosineSimilarity(System.ReadOnlySpan<float> x, System.ReadOnlySpan<float> y) { throw null; }
public abstract System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry> Find(System.ReadOnlyMemory<float> vector, Azure.CloudMachine.OpenAI.Embeddings.FindOptions options);
public abstract System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.VectorbaseEntry> Find(System.ReadOnlyMemory<float> vector, Azure.CloudMachine.OpenAI.FindOptions options);
}
}
namespace Azure.Core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" VersionOverride="8.0.0" />
<PackageReference Include="Microsoft.Bcl.Numerics" />
<PackageReference Include="System.Memory.Data"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ public readonly struct MessagingServices
/// Sends a message to the service bus.
/// </summary>
/// <param name="serializable"></param>
public void SendMessage(object serializable)
public void SendJson(object serializable)
{
#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult().
SendJsonAsync(serializable).GetAwaiter().GetResult();
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult().
}

/// <summary>
/// Sends a message to the service bus.
/// </summary>
/// <param name="serializable"></param>
public async Task SendJsonAsync(object serializable)
{
ServiceBusSender sender = GetServiceBusSender();

BinaryData serialized = BinaryData.FromObjectAsJson(serializable);
ServiceBusMessage message = new(serialized);
#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult().
sender.SendMessageAsync(message).GetAwaiter().GetResult();
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult().
await sender.SendMessageAsync(message).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class StorageFile
/// </summary>
/// <returns></returns>
public BinaryData Download()
=> _storage.DownloadBlob(Path);
=> _storage.Download(Path);

// public async Task<BinaryData> DownloadAsync()
// => await _storage.DownloadBlobAsync(Path).ConfigureAwait(false);
Expand All @@ -52,7 +52,7 @@ public BinaryData Download()
/// Deletes the file from the storage account.
/// </summary>
public void Delete()
=> _storage.DeleteBlob(Path);
=> _storage.Delete(Path);

// public async Task DeleteAsync()
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved
// => await _storage.DeleteBlobAsync(Path).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,76 +78,118 @@ public string UploadJson(object json, string name = default, bool overwrite = fa
/// </summary>
/// <param name="fileStream"></param>
/// <param name="name"></param>
/// <param name="contentType"></param>
/// <param name="overwrite"></param>
/// <returns></returns>
public string UploadStream(Stream fileStream, string name = default, bool overwrite = false)
public string Upload(Stream fileStream, string name = default, string contentType = default, bool overwrite = false)
{
BlobContainerClient container = GetDefaultContainer();

if (name == default)
name = $"b{Guid.NewGuid()}";

var client = container.GetBlockBlobClient(name);
var options = new BlobUploadOptions
{
Conditions = overwrite ? null : new BlobRequestConditions { IfNoneMatch = new ETag("*") },
HttpHeaders = new BlobHttpHeaders { ContentType = ContentType.ApplicationOctetStream.ToString() }
};
BlockBlobClient client;
BlobUploadOptions options;
name = PrepareForUpload(name, overwrite, container, contentType, out client, out options);

client.Upload(fileStream, options);
return name;
}

/// <summary>
/// Uploads a binary data object to the storage account.
/// Uploads a file to the storage account.
/// </summary>
/// <param name="data"></param>
/// <param name="fileStream"></param>
/// <param name="name"></param>
/// <param name="contentType"></param>
/// <param name="overwrite"></param>
/// <returns></returns>
public string UploadBinaryData(BinaryData data, string name = default, bool overwrite = false)
public async Task<string> UploadAsync(Stream fileStream, string name = default, string contentType = default, bool overwrite = false)
{
BlobContainerClient container = GetDefaultContainer();

BlockBlobClient client;
BlobUploadOptions options;
name = PrepareForUpload(name, overwrite, container, contentType, out client, out options);

await client.UploadAsync(fileStream, options).ConfigureAwait(false);
return name;
}

private static string PrepareForUpload(string name, bool overwrite, BlobContainerClient container, string contentType, out BlockBlobClient client, out BlobUploadOptions options)
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved
{
if (name == default)
name = $"b{Guid.NewGuid()}";

var client = container.GetBlockBlobClient(name);
var options = new BlobUploadOptions
if (contentType == null)
contentType = "application/octet-stream";

client = container.GetBlockBlobClient(name);
options = new BlobUploadOptions
{
Conditions = overwrite ? null : new BlobRequestConditions { IfNoneMatch = new ETag("*") },
HttpHeaders = new BlobHttpHeaders { ContentType = ContentType.ApplicationOctetStream.ToString() }
};

client.Upload(data.ToStream(), options);
return name;
}

/// <summary>
/// Uploads a byte array to the storage account.
/// Uploads a binary data object to the storage account.
/// </summary>
/// <param name="bytes"></param>
/// <param name="data"></param>
/// <param name="name"></param>
/// <param name="overwrite"></param>
/// <returns></returns>
public string UploadBytes(byte[] bytes, string name = default, bool overwrite = false)
=> UploadBinaryData(BinaryData.FromBytes(bytes), name, overwrite);
public string Upload(BinaryData data, string name = default, bool overwrite = false)
{
BlockBlobClient client;
BlobUploadOptions options;
name = PrepareForUpload(data, name, overwrite, out client, out options);

client.Upload(data.ToStream(), options);
return name;
}

/// <summary>
/// Uploads a byte array to the storage account.
/// Uploads a binary data object to the storage account.
/// </summary>
/// <param name="bytes"></param>
/// <param name="data"></param>
/// <param name="name"></param>
/// <param name="overwrite"></param>
/// <returns></returns>
public string UploadBytes(ReadOnlyMemory<byte> bytes, string name = default, bool overwrite = false)
=> UploadBinaryData(BinaryData.FromBytes(bytes), name, overwrite);
public async Task<string> UploadAsync(BinaryData data, string name = default, bool overwrite = false)
{
BlockBlobClient client;
BlobUploadOptions options;
name = PrepareForUpload(data, name, overwrite, out client, out options);

await client.UploadAsync(data.ToStream(), options).ConfigureAwait(false);
return name;
}

private string PrepareForUpload(BinaryData data, string name, bool overwrite, out BlockBlobClient client, out BlobUploadOptions options)
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved
{
BlobContainerClient container = GetDefaultContainer();
if (name == default)
name = $"b{Guid.NewGuid()}";

string contentType = data.MediaType;
if (contentType == null)
contentType = "application/octet-stream";
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved

client = container.GetBlockBlobClient(name);
options = new BlobUploadOptions
{
Conditions = overwrite ? null : new BlobRequestConditions { IfNoneMatch = new ETag("*") },
HttpHeaders = new BlobHttpHeaders { ContentType = ContentType.ApplicationOctetStream.ToString() },
};
options.Metadata.Add("Content-Type", contentType);
return name;
}

/// <summary>
/// Uploads a file to the storage account.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public BinaryData DownloadBlob(string path)
public BinaryData Download(string path)
{
BlobClient blob = GetBlobClientFromPath(path, null);
BlobDownloadResult result = blob.DownloadContent();
Expand All @@ -158,7 +200,7 @@ public BinaryData DownloadBlob(string path)
/// Deletes a blob from the storage account.
/// </summary>
/// <param name="path"></param>
public void DeleteBlob(string path)
public void Delete(string path)
{
BlobClient blob = GetBlobClientFromPath(path, null);
blob.DeleteIfExists();
Expand Down Expand Up @@ -198,7 +240,7 @@ private static string ConvertPathToBlobPath(string path, BlobContainerClient con
/// Adds a function to be called when a blob is uploaded.
/// </summary>
/// <param name="function"></param>
public void WhenBlobUploaded(Action<StorageFile> function)
public void WhenUploaded(Action<StorageFile> function)
{
CloudMachineClient cm = _cm;
// TODO (Pri 0): once the cache gets GCed, we will stop receiving events
Expand Down Expand Up @@ -231,4 +273,17 @@ public void WhenBlobUploaded(Action<StorageFile> function)
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult().

}

/// <summary>
/// Adds a function to be called when a blob is uploaded.
/// </summary>
/// <param name="function"></param>
public void WhenUploaded(Action<BinaryData> function)
KrzysztofCwalina marked this conversation as resolved.
Show resolved Hide resolved
{
WhenUploaded((StorageFile file) =>
{
BinaryData data = file.Download();
function(data);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.ClientModel;
using System.Collections.Generic;
using Azure.AI.OpenAI;
using Azure.Core;
using OpenAI.Chat;
Expand Down Expand Up @@ -72,4 +73,26 @@ private static EmbeddingClient CreateEmbeddingsClient(this ClientWorkspace works
EmbeddingClient embeddings = client.GetEmbeddingClient(connection.Id);
return embeddings;
}

/// <summary>
/// Trims list of chat messages.
/// </summary>
/// <param name="messages"></param>
public static void Trim(this List<ChatMessage> messages)
{
messages.RemoveRange(0, messages.Count / 2);
}

/// <summary>
/// Adds a list of vectorbase entries to the list of chat messages.
/// </summary>
/// <param name="messages"></param>
/// <param name="entries"></param>
public static void Add(this List<ChatMessage> messages, IEnumerable<VectorbaseEntry> entries)
{
foreach (VectorbaseEntry entry in entries)
{
messages.Add(ChatMessage.CreateSystemMessage(entry.Data.ToString()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Text.Json;
using OpenAI.Chat;

namespace Azure.CloudMachine.OpenAI.Chat;
namespace Azure.CloudMachine.OpenAI;

/// <summary> The service client for the OpenAI Chat Completions endpoint. </summary>
public class ChatTools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using OpenAI.Embeddings;

namespace Azure.CloudMachine.OpenAI.Embeddings;
namespace Azure.CloudMachine.OpenAI;

/// <summary>
/// The vectorbase for storing embeddings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Generic;
using System;

namespace Azure.CloudMachine.OpenAI.Embeddings;
namespace Azure.CloudMachine.OpenAI;

internal class MemoryVectorbaseStore : VectorbaseStore
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Generic;
using System;

namespace Azure.CloudMachine.OpenAI.Embeddings;
namespace Azure.CloudMachine.OpenAI;

/// <summary>
/// The base class for a vectorbase store.
Expand Down
Loading
Loading