Class AzureCosmosDBService
- Namespace
- FoundationaLLM.Common.Services.Azure
- Assembly
- FoundationaLLM.Common.dll
Service to access Azure Cosmos DB for NoSQL.
public class AzureCosmosDBService : IAzureCosmosDBService
- Inheritance
-
AzureCosmosDBService
- Implements
- Inherited Members
- Extension Methods
Constructors
AzureCosmosDBService(IOptions<AzureCosmosDBSettings>, CosmosClient, ILogger<AzureCosmosDBService>)
Initializes a new instance of the AzureCosmosDBService class.
public AzureCosmosDBService(IOptions<AzureCosmosDBSettings> settings, CosmosClient client, ILogger<AzureCosmosDBService> logger)
Parameters
settingsIOptions<AzureCosmosDBSettings>The AzureCosmosDBSettings settings retrieved by the injected IOptions<TOptions>.
clientCosmosClientThe Cosmos DB client.
loggerILogger<AzureCosmosDBService>The logging interface used to log under the AzureCosmosDBService type name.
Exceptions
- ArgumentException
Thrown if any of the required settings are null or empty.
Methods
CreateAgentFile(AgentFileReference, CancellationToken)
Creates an agent file.
public Task CreateAgentFile(AgentFileReference agentFile, CancellationToken cancellationToken = default)
Parameters
agentFileAgentFileReferenceThe agent file to be added.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
CreateAttachment(AttachmentReference, CancellationToken)
Creates an attachment.
public Task CreateAttachment(AttachmentReference attachmentReference, CancellationToken cancellationToken = default)
Parameters
attachmentReferenceAttachmentReferencecancellationTokenCancellationTokenCancellation token for async calls.
Returns
CreateOrUpdateConversationAsync(Conversation, CancellationToken)
Creates or updates a conversation.
public Task<Conversation> CreateOrUpdateConversationAsync(Conversation session, CancellationToken cancellationToken = default)
Parameters
sessionConversationConversation item to create or update.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<Conversation>
Newly created or updated conversation item.
CreateVectorSearchContainerAsync(string, string, string, int, CancellationToken)
Creates a new container for vector search.
public Task CreateVectorSearchContainerAsync(string containerName, string partitionKeyPath, string vectorPropertyPath, int vectorDimensions, CancellationToken cancellationToken = default)
Parameters
containerNamestringThe name of the container to create.
partitionKeyPathstringThe property path that contains the partition key.
vectorPropertyPathstringvectorDimensionsintThe length of each vector (the number of dimensions used for embedding).
cancellationTokenCancellationTokenThe cancellation token to signal the need to cancel the operation.
Returns
DeleteAgentFile(AgentFileReference, CancellationToken)
Deletes an agent file.
public Task DeleteAgentFile(AgentFileReference agentFile, CancellationToken cancellationToken = default)
Parameters
agentFileAgentFileReferenceThe agent file to be deleted.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
DeleteAttachment(AttachmentReference, CancellationToken)
Deletes an attachment.
public Task DeleteAttachment(AttachmentReference attachmentReference, CancellationToken cancellationToken = default)
Parameters
attachmentReferenceAttachmentReferencecancellationTokenCancellationTokenCancellation token for async calls.
Returns
DeleteConversationAsync(string, CancellationToken)
Batch deletes an existing chat session and all related messages.
public Task DeleteConversationAsync(string sessionId, CancellationToken cancellationToken = default)
Parameters
sessionIdstringChat session identifier used to flag messages and sessions for deletion.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
FilterAttachments(string, ResourceFilter, CancellationToken)
Gets list of filtered attachments.
public Task<List<AttachmentReference>> FilterAttachments(string upn, ResourceFilter resourceFilter, CancellationToken cancellationToken = default)
Parameters
upnstringThe user's UPN.
resourceFilterResourceFilterThe resource filter.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<List<AttachmentReference>>
A list of filtered attachments.
GetAgentFile(string, string, string, CancellationToken)
Gets an agent file.
public Task<AgentFileReference?> GetAgentFile(string instanceId, string agentName, string id, CancellationToken cancellationToken = default)
Parameters
instanceIdstringThe instance unique identifier
agentNamestringThe agent name.
idstringThe agent file id.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<AgentFileReference>
An agent file.
GetAgentFiles(string, string, CancellationToken)
Gets a list of agent files.
public Task<List<AgentFileReference>> GetAgentFiles(string instanceId, string agentName, CancellationToken cancellationToken = default)
Parameters
instanceIdstringThe instance unique identifier
agentNamestringThe agent name.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<List<AgentFileReference>>
A list of agent files.
GetAttachment(string, string, CancellationToken)
Gets an attachment.
public Task<AttachmentReference?> GetAttachment(string upn, string resourceName, CancellationToken cancellationToken = default)
Parameters
upnstringThe user's UPN.
resourceNamestringcancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<AttachmentReference>
An attachment.
GetAttachments(string, CancellationToken)
Gets a list of attachments for the signed in user.
public Task<List<AttachmentReference>> GetAttachments(string upn, CancellationToken cancellationToken = default)
Parameters
upnstringThe user principal name used for retrieving the attachments for the signed in user.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<List<AttachmentReference>>
A list of attachments for the signed in user.
GetCompletionPromptAsync(string, string)
Returns the completion prompt for a given session and completion prompt id.
public Task<CompletionPrompt> GetCompletionPromptAsync(string sessionId, string completionPromptId)
Parameters
sessionIdstringThe session id from which to retrieve the completion prompt.
completionPromptIdstringThe id of the completion prompt to retrieve.
Returns
GetCompletionResponseAsync(string, ReadOnlyMemory<float>, decimal)
Gets the completion response for a given user prompt embedding using vector search and a minimum threshold for similarity.
public Task<CompletionResponse?> GetCompletionResponseAsync(string containerName, ReadOnlyMemory<float> userPromptEmbedding, decimal minimumSimilarityScore)
Parameters
containerNamestringThe name of the container holding the vector index.
userPromptEmbeddingReadOnlyMemory<float>The reference embedding used for the vector search.
minimumSimilarityScoredecimalThe threshold used for the similarity score.
Returns
- Task<CompletionResponse>
A CompletionResponse that matches the search criteria. If no item in the vector index matches the criteria, returns
.
GetConversationAsync(string, CancellationToken)
Performs a point read to retrieve a single conversation item.
public Task<Conversation?> GetConversationAsync(string id, CancellationToken cancellationToken = default)
Parameters
idstringcancellationTokenCancellationToken
Returns
- Task<Conversation>
The conversation item. Returns null if the conversation does not exist.
GetConversationsAsync(string, string, CancellationToken)
Gets a list of all current conversations.
public Task<List<Conversation>> GetConversationsAsync(string type, string upn, CancellationToken cancellationToken = default)
Parameters
typestringThe conversation type to return.
upnstringThe user principal name used for retrieving conversations for the signed-in user.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<List<Conversation>>
List of distinct conversation items.
GetItemAsync<T>(string, string, string, CancellationToken)
Gets a single item by its identifier and partition key.
public Task<T?> GetItemAsync<T>(string containerName, string id, string partitionKey, CancellationToken cancellationToken = default)
Parameters
containerNamestringThe name of the container storing the item.
idstringThe identifier of the item.
partitionKeystringThe partition key of the item.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<T>
The retrieved object of type
T.
Type Parameters
TThe type of the item to retrieve.
GetLongRunningOperationContextAsync(string, CancellationToken)
Get the context for a long running operation.
public Task<LongRunningOperationContext> GetLongRunningOperationContextAsync(string operationId, CancellationToken cancellationToken = default)
Parameters
operationIdstringThe identifier of the long running operation.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<LongRunningOperationContext>
A LongRunningOperationContext object providing the context for the long running operation.
GetMessageAsync(string, string, CancellationToken)
Gets a single conversation message by its identifier.
public Task<Message> GetMessageAsync(string id, string sessionId, CancellationToken cancellationToken = default)
Parameters
idstringThe identifier of the message.
sessionIdstringThe identifier of the conversation.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
GetSessionMessagesAsync(string, string, int?, CancellationToken)
Gets a list of all current chat messages for a specified session identifier. Messages are always sorted by TimeStamp in ascending order.
public Task<List<Message>> GetSessionMessagesAsync(string sessionId, string upn, int? max = null, CancellationToken cancellationToken = default)
Parameters
sessionIdstringChat session identifier used to filter messages.
upnstringThe user principal name used for retrieving the messages for the signed in user.
maxint?If provided, limits the number of messages.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
GetUserProfileAsync(string, CancellationToken)
Returns the user profile for a given user via their UPN.
public Task<UserProfile> GetUserProfileAsync(string upn, CancellationToken cancellationToken = default)
Parameters
upnstringThe user principal name used for retrieving the messages for the signed in user.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
InsertMessageAsync(Message, CancellationToken)
Creates a new chat message.
public Task<Message> InsertMessageAsync(Message message, CancellationToken cancellationToken = default)
Parameters
messageMessageChat message item to create.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
PatchConversationPropertiesAsync(string, string, Dictionary<string, object?>, CancellationToken)
Updates conversation properties through a patch operation.
public Task<Conversation> PatchConversationPropertiesAsync(string id, string upn, Dictionary<string, object?> propertyValues, CancellationToken cancellationToken = default)
Parameters
idstringThe conversation id.
upnstringThe user principal name used for policy enforcement.
propertyValuesDictionary<string, object>The dictionary containing property names and updated values.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<Conversation>
Updated conversation item.
PatchItemPropertiesAsync<T>(string, string, string, string, Dictionary<string, object?>, CancellationToken)
Patches an item in the specified container.
public Task<T> PatchItemPropertiesAsync<T>(string containerName, string partitionKey, string id, string upn, Dictionary<string, object?> propertyValues, CancellationToken cancellationToken = default)
Parameters
containerNamestringThe name of the container storing the item.
partitionKeystringThe partition key of the item.
idstringThe item ID.
upnstringThe user principal name used for matching the UPN of the item.
propertyValuesDictionary<string, object>Dictionary of the property names and values to patch.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<T>
The patched object of type
.
Type Parameters
TThe type of the item to patch.
PatchMultipleSessionsItemsInTransactionAsync(string, List<IPatchOperationItem>, CancellationToken)
Updates a subset of the properties of one or more items of a specified type from the Sessions collection.
public Task<Dictionary<string, object>> PatchMultipleSessionsItemsInTransactionAsync(string partitionKey, List<IPatchOperationItem> patchOperations, CancellationToken cancellationToken = default)
Parameters
partitionKeystringThe partition key of the item(s) being updated. Batch operations must occur within the same partition key.
patchOperationsList<IPatchOperationItem>The patch operations to perform on each object, including the object's identifier, dictionary containing the property names and updated values, and the item type.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
PatchOperationsItemPropertiesAsync<T>(string, string, Dictionary<string, object?>, CancellationToken)
Updates a subset of the properties of an item of a specified type from the Operations collection.
public Task<T> PatchOperationsItemPropertiesAsync<T>(string itemId, string partitionKey, Dictionary<string, object?> propertyValues, CancellationToken cancellationToken = default)
Parameters
itemIdstringThe identifier of the item being updated.
partitionKeystringThe partition key of the item being updated.
propertyValuesDictionary<string, object>The dictionary containing property names and updated values.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<T>
Type Parameters
TThe type of the item to update.
PatchSessionsItemPropertiesAsync<T>(string, string, Dictionary<string, object?>, CancellationToken)
Updates a subset of the properties of an item of a specified type from the Sessions collection.
public Task<T> PatchSessionsItemPropertiesAsync<T>(string itemId, string partitionKey, Dictionary<string, object?> propertyValues, CancellationToken cancellationToken = default)
Parameters
itemIdstringThe identifier of the item being updated.
partitionKeystringThe partition key of the item being updated.
propertyValuesDictionary<string, object>The dictionary containing property names and updated values.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<T>
Type Parameters
TThe type of the item to update.
UpdateMessageAsync(Message, CancellationToken)
Updates an existing chat message.
public Task<Message> UpdateMessageAsync(Message message, CancellationToken cancellationToken = default)
Parameters
messageMessageChat message item to update.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
UpsertItemAsync<T>(string, string, T, CancellationToken)
Creates or updates an item in the specified container.
public Task<T?> UpsertItemAsync<T>(string containerName, string partitionKey, T item, CancellationToken cancellationToken = default)
Parameters
containerNamestringThe name of the container storing the item.
partitionKeystringThe partition key of the item.
itemTThe item to be created or updated.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
- Task<T>
The created or updated object of type
T.
Type Parameters
TThe type of the item to create or update.
UpsertLongRunningOperationContextAsync(LongRunningOperationContext, CancellationToken)
Inserts or updates a long running operation context.
public Task UpsertLongRunningOperationContextAsync(LongRunningOperationContext longRunningOperationContext, CancellationToken cancellationToken = default)
Parameters
longRunningOperationContextLongRunningOperationContextThe LongRunningOperationContext object providing the context for the long running operation.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
UpsertSessionBatchAsync(params dynamic[])
Batch create or update chat messages and session.
public Task UpsertSessionBatchAsync(params dynamic[] messages)
Parameters
messagesdynamic[]Chat message and session items to create or replace.
Returns
UpsertUserProfileAsync(UserProfile, CancellationToken)
Inserts or updates a user profile.
public Task UpsertUserProfileAsync(UserProfile userProfile, CancellationToken cancellationToken = default)
Parameters
userProfileUserProfileThe user profile to upsert.
cancellationTokenCancellationTokenCancellation token for async calls.
Returns
UpsertUserSessionAsync(Conversation, CancellationToken)
Create or update a user session from the passed in Session object.
public Task UpsertUserSessionAsync(Conversation session, CancellationToken cancellationToken = default)
Parameters
sessionConversationThe chat session item to create or replace.
cancellationTokenCancellationTokenCancellation token for async calls.