Skip to content

Commit b6c3306

Browse files
authored
Refactor OpenAI helpers (Azure#46956)
1 parent 1bb0be0 commit b6c3306

21 files changed

+929
-359
lines changed

sdk/cloudmachine/Azure.CloudMachine/api/Azure.CloudMachine.netstandard2.0.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,57 @@ public static partial class AzureOpenAIExtensions
7575
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this Azure.Core.ClientWorkspace workspace) { throw null; }
7676
}
7777
}
78+
namespace Azure.CloudMachine.OpenAI.Chat
79+
{
80+
public partial class ChatTools
81+
{
82+
public ChatTools(params System.Type[] tools) { }
83+
public System.Collections.Generic.IList<OpenAI.Chat.ChatTool> Definitions { get { throw null; } }
84+
public void Add(System.Reflection.MethodInfo function) { }
85+
public void Add(System.Type functions) { }
86+
public string Call(OpenAI.Chat.ChatToolCall call) { throw null; }
87+
public string Call(string name, object[] arguments) { throw null; }
88+
public System.Collections.Generic.IEnumerable<OpenAI.Chat.ToolChatMessage> CallAll(System.Collections.Generic.IEnumerable<OpenAI.Chat.ChatToolCall> toolCalls) { throw null; }
89+
protected string ClrToJsonTypeUtf16(System.Type clrType) { throw null; }
90+
protected System.ReadOnlySpan<byte> ClrToJsonTypeUtf8(System.Type clrType) { throw null; }
91+
protected virtual string GetMethodInfoToDescription(System.Reflection.MethodInfo function) { throw null; }
92+
protected virtual string GetMethodInfoToName(System.Reflection.MethodInfo function) { throw null; }
93+
protected virtual string GetParameterInfoToDescription(System.Reflection.ParameterInfo parameter) { throw null; }
94+
}
95+
}
96+
namespace Azure.CloudMachine.OpenAI.Embeddings
97+
{
98+
public partial class EmbeddingsVectorbase
99+
{
100+
public EmbeddingsVectorbase(OpenAI.Embeddings.EmbeddingClient client, Azure.CloudMachine.OpenAI.Embeddings.VectorbaseStore store = null, int factChunkSize = 0) { }
101+
public void Add(string text) { }
102+
public System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry> Find(string text, Azure.CloudMachine.OpenAI.Embeddings.FindOptions options = null) { throw null; }
103+
}
104+
public partial class FindOptions
105+
{
106+
public FindOptions() { }
107+
public int MaxEntries { get { throw null; } set { } }
108+
public float Threshold { get { throw null; } set { } }
109+
}
110+
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
111+
public readonly partial struct VectorbaseEntry
112+
{
113+
private readonly object _dummy;
114+
private readonly int _dummyPrimitive;
115+
public VectorbaseEntry(System.ReadOnlyMemory<float> vector, System.BinaryData data, int? id = default(int?)) { throw null; }
116+
public System.BinaryData Data { get { throw null; } }
117+
public int? Id { get { throw null; } }
118+
public System.ReadOnlyMemory<float> Vector { get { throw null; } }
119+
}
120+
public abstract partial class VectorbaseStore
121+
{
122+
protected VectorbaseStore() { }
123+
public abstract int Add(Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry entry);
124+
public abstract void Add(System.Collections.Generic.IReadOnlyList<Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry> entry);
125+
public static float CosineSimilarity(System.ReadOnlySpan<float> x, System.ReadOnlySpan<float> y) { throw null; }
126+
public abstract System.Collections.Generic.IEnumerable<Azure.CloudMachine.OpenAI.Embeddings.VectorbaseEntry> Find(System.ReadOnlyMemory<float> vector, Azure.CloudMachine.OpenAI.Embeddings.FindOptions options);
127+
}
128+
}
78129
namespace Azure.Core
79130
{
80131
public partial class ClientCache

sdk/cloudmachine/Azure.CloudMachine/src/Azure.CloudMachine.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<!-- Disable warning CS1591: Missing XML comment for publicly visible type or member -->
1010
<NoWarn>CS1591</NoWarn>
11+
<NoWarn>OPENAI001</NoWarn>
1112
</PropertyGroup>
1213

1314
<ItemGroup>
@@ -19,6 +20,7 @@
1920
<PackageReference Include="Azure.Storage.Blobs" />
2021
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
2122
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" VersionOverride="8.0.0" />
23+
<PackageReference Include="Microsoft.Bcl.Numerics" />
2224
</ItemGroup>
2325

2426
</Project>

sdk/cloudmachine/Azure.CloudMachine/src/ClientCache.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@
77
namespace Azure.Core;
88

99
// TODO: this is a very demo implementation. We need to do better
10+
/// <summary>
11+
/// The client cache.
12+
/// </summary>
1013
public class ClientCache
1114
{
1215
private readonly Dictionary<(Type, string), object> _clients = new Dictionary<(Type, string), object>();
1316

17+
/// <summary>
18+
/// Gets a client from the cache.
19+
/// </summary>
20+
/// <typeparam name="T"></typeparam>
21+
/// <param name="value"></param>
22+
/// <param name="id"></param>
23+
/// <returns></returns>
1424
public T Get<T>(Func<T> value, string id = default) where T: class
1525
{
1626
var client = (typeof(T), id);

sdk/cloudmachine/Azure.CloudMachine/src/ClientWorkspace.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,109 @@
66

77
namespace Azure.Core;
88

9+
/// <summary>
10+
/// Retrieves the connection options for a specified client type and instance ID.
11+
/// Represents a workspace for client operations.
12+
/// </summary>
913
public abstract class ClientWorkspace
1014
{
15+
/// <summary>
16+
/// Retrieves the connection options for a specified client type and instance ID.
17+
/// </summary>
18+
/// <param name="clientType">The type of the client.</param>
19+
/// <param name="instanceId">The instance ID of the client.</param>
20+
/// <returns>The connection options for the specified client type and instance ID.</returns>
1121
public abstract ClientConnectionOptions GetConnectionOptions(Type clientType, string instanceId = default);
1222

23+
/// <summary>
24+
/// Gets the cache of subclients.
25+
/// </summary>
1326
[EditorBrowsable(EditorBrowsableState.Never)]
1427
public ClientCache Subclients { get; } = new ClientCache();
1528
}
1629

30+
/// <summary>
31+
/// Represents the connection options for a client.
32+
/// </summary>
1733
public readonly struct ClientConnectionOptions
1834
{
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="ClientConnectionOptions"/> struct with the specified endpoint and API key.
37+
/// </summary>
38+
/// <param name="endpoint">The endpoint URI.</param>
39+
/// <param name="apiKey">The API key credential.</param>
1940
public ClientConnectionOptions(Uri endpoint, string apiKey)
2041
{
2142
Endpoint = endpoint;
2243
ApiKeyCredential = apiKey;
2344
ConnectionKind = ClientConnectionKind.ApiKey;
2445
}
46+
47+
/// <summary>
48+
/// Initializes a new instance of the <see cref="ClientConnectionOptions"/> struct with the specified endpoint and token credential.
49+
/// </summary>
50+
/// <param name="endpoint">The endpoint URI.</param>
51+
/// <param name="credential">The token credential.</param>
2552
public ClientConnectionOptions(Uri endpoint, TokenCredential credential)
2653
{
2754
Endpoint = endpoint;
2855
TokenCredential = credential;
2956
ConnectionKind = ClientConnectionKind.EntraId;
3057
}
58+
59+
/// <summary>
60+
/// Initializes a new instance of the <see cref="ClientConnectionOptions"/> struct with the specified subclient ID.
61+
/// </summary>
62+
/// <param name="subclientId">The subclient ID.</param>
3163
public ClientConnectionOptions(string subclientId)
3264
{
3365
Id = subclientId;
3466
ConnectionKind = ClientConnectionKind.OutOfBand;
3567
}
3668

69+
/// <summary>
70+
/// Gets the kind of connection used by the client.
71+
/// </summary>
3772
public ClientConnectionKind ConnectionKind { get; }
3873

74+
/// <summary>
75+
/// Gets the endpoint URI.
76+
/// </summary>
3977
public Uri Endpoint { get; }
78+
79+
/// <summary>
80+
/// Gets the subclient ID.
81+
/// </summary>
4082
public string Id { get; }
83+
84+
/// <summary>
85+
/// Gets the API key credential.
86+
/// </summary>
4187
public string ApiKeyCredential { get; }
88+
89+
/// <summary>
90+
/// Gets the token credential.
91+
/// </summary>
4292
public TokenCredential TokenCredential { get; }
4393
}
4494

95+
/// <summary>
96+
/// Specifies the kind of connection used by the client.
97+
/// </summary>
4598
public enum ClientConnectionKind
4699
{
100+
/// <summary>
101+
/// Represents a connection using Entra ID.
102+
/// </summary>
47103
EntraId,
104+
105+
/// <summary>
106+
/// Represents a connection using an API key.
107+
/// </summary>
48108
ApiKey,
109+
110+
/// <summary>
111+
/// Represents a connection using an out-of-band method.
112+
/// </summary>
49113
OutOfBand
50114
}

sdk/cloudmachine/Azure.CloudMachine/src/CloudMachineClient.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66

77
namespace Azure.CloudMachine;
88

9+
/// <summary>
10+
/// The cloud machine client.
11+
/// </summary>
912
public partial class CloudMachineClient : CloudMachineWorkspace
1013
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="CloudMachineClient"/> class for mocking purposes..
16+
/// </summary>
1117
protected CloudMachineClient()
1218
{
1319
Messaging = new MessagingServices(this);
1420
Storage = new StorageServices(this);
1521
}
1622
#pragma warning disable AZC0007 // DO provide a minimal constructor that takes only the parameters required to connect to the service.
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="CloudMachineClient"/> class.
25+
/// </summary>
26+
/// <param name="credential">The token credential.</param>
27+
/// <param name="configuration">The configuration settings.</param>
1728
public CloudMachineClient(TokenCredential credential = default, IConfiguration configuration = default)
1829
#pragma warning restore AZC0007 // DO provide a minimal constructor that takes only the parameters required to connect to the service.
1930
: base(credential, configuration)
@@ -22,6 +33,13 @@ public CloudMachineClient(TokenCredential credential = default, IConfiguration c
2233
Storage = new StorageServices(this);
2334
}
2435

36+
/// <summary>
37+
/// Gets the messaging services.
38+
/// </summary>
2539
public MessagingServices Messaging { get; }
40+
41+
/// <summary>
42+
/// Gets the storage services.
43+
/// </summary>
2644
public StorageServices Storage { get; }
2745
}

sdk/cloudmachine/Azure.CloudMachine/src/CloudMachineWorkspace.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,28 @@
1313

1414
namespace Azure.CloudMachine;
1515

16+
/// <summary>
17+
/// The cloud machine workspace.
18+
/// </summary>
1619
public class CloudMachineWorkspace : ClientWorkspace
1720
{
1821
private TokenCredential Credential { get; } = new ChainedTokenCredential(
1922
new AzureCliCredential(),
2023
new AzureDeveloperCliCredential()
2124
);
2225

26+
/// <summary>
27+
/// The cloud machine ID.
28+
/// </summary>
2329
[EditorBrowsable(EditorBrowsableState.Never)]
2430
public string Id { get; }
2531

32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="CloudMachineWorkspace"/> class.
34+
/// </summary>
35+
/// <param name="credential"></param>
36+
/// <param name="configuration"></param>
37+
/// <exception cref="Exception"></exception>
2638
[SuppressMessage("Usage", "AZC0007:DO provide a minimal constructor that takes only the parameters required to connect to the service.", Justification = "<Pending>")]
2739
public CloudMachineWorkspace(TokenCredential credential = default, IConfiguration configuration = default)
2840
{
@@ -46,6 +58,13 @@ public CloudMachineWorkspace(TokenCredential credential = default, IConfiguratio
4658
Id = cmid!;
4759
}
4860

61+
/// <summary>
62+
/// Retrieves the connection options for a specified client type and instance ID.
63+
/// </summary>
64+
/// <param name="clientType"></param>
65+
/// <param name="instanceId"></param>
66+
/// <returns></returns>
67+
/// <exception cref="Exception"></exception>
4968
[EditorBrowsable(EditorBrowsableState.Never)]
5069
public override ClientConnectionOptions GetConnectionOptions(Type clientType, string instanceId = default)
5170
{
@@ -75,10 +94,15 @@ public override ClientConnectionOptions GetConnectionOptions(Type clientType, st
7594
}
7695
}
7796

97+
/// <inheritdoc/>
7898
[EditorBrowsable(EditorBrowsableState.Never)]
7999
public override bool Equals(object obj) => base.Equals(obj);
100+
101+
/// <inheritdoc/>
80102
[EditorBrowsable(EditorBrowsableState.Never)]
81103
public override int GetHashCode() => base.GetHashCode();
104+
105+
/// <inheritdoc/>
82106
[EditorBrowsable(EditorBrowsableState.Never)]
83107
public override string ToString() => Id;
84108

sdk/cloudmachine/Azure.CloudMachine/src/CoreServices/MessagingServices.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88

99
namespace Azure.CloudMachine;
1010

11+
/// <summary>
12+
/// The messaging services for the cloud machine.
13+
/// </summary>
1114
public readonly struct MessagingServices
1215
{
1316
private readonly CloudMachineClient _cm;
1417
internal MessagingServices(CloudMachineClient cm) => _cm = cm;
1518

19+
/// <summary>
20+
/// Sends a message to the service bus.
21+
/// </summary>
22+
/// <param name="serializable"></param>
1623
public void SendMessage(object serializable)
1724
{
1825
ServiceBusSender sender = GetServiceBusSender();
@@ -24,6 +31,10 @@ public void SendMessage(object serializable)
2431
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult().
2532
}
2633

34+
/// <summary>
35+
/// Adds a function to be called when a message is received.
36+
/// </summary>
37+
/// <param name="received"></param>
2738
public void WhenMessageReceived(Action<string> received)
2839
{
2940
var processor = _cm.Messaging.GetServiceBusProcessor();

sdk/cloudmachine/Azure.CloudMachine/src/CoreServices/StorageFile.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77

88
namespace Azure.CloudMachine;
99

10+
/// <summary>
11+
/// The storage file for the cloud machine.
12+
/// </summary>
1013
public class StorageFile
1114
{
1215
private readonly Response _response;
1316

1417
private StorageServices _storage;
18+
19+
/// <summary>
20+
/// The path of the file in the storage account.
21+
/// </summary>
1522
public string Path { get; internal set; }
1623

1724
/// <summary>
@@ -26,14 +33,24 @@ public class StorageFile
2633
/// <remarks>returns null if the file is not created as a return value of a service method call.</remarks>
2734
public static implicit operator Response(StorageFile result) => result._response;
2835

36+
/// <summary>
37+
/// The cancellation token for the storage operation.
38+
/// </summary>
2939
public CancellationToken CancellationToken { get; internal set; }
3040

41+
/// <summary>
42+
/// Downloads the file from the storage account.
43+
/// </summary>
44+
/// <returns></returns>
3145
public BinaryData Download()
3246
=> _storage.DownloadBlob(Path);
3347

3448
// public async Task<BinaryData> DownloadAsync()
3549
// => await _storage.DownloadBlobAsync(Path).ConfigureAwait(false);
3650

51+
/// <summary>
52+
/// Deletes the file from the storage account.
53+
/// </summary>
3754
public void Delete()
3855
=> _storage.DeleteBlob(Path);
3956

@@ -54,12 +71,15 @@ internal StorageFile(StorageServices storage, string path, string requestId, Res
5471
_response = response;
5572
}
5673

74+
/// <inheritdoc />
5775
[EditorBrowsable(EditorBrowsableState.Never)]
5876
public override bool Equals(object obj) => base.Equals(obj);
5977

78+
/// <inheritdoc />
6079
[EditorBrowsable(EditorBrowsableState.Never)]
6180
public override int GetHashCode() => base.GetHashCode();
6281

82+
/// <inheritdoc />
6383
[EditorBrowsable(EditorBrowsableState.Never)]
6484
public override string ToString() => $"{Path}";
6585
}

0 commit comments

Comments
 (0)