Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Last active December 4, 2024 14:26
Show Gist options
  • Save seesharprun/ace6b930706b4906fdbe65b5d8e92124 to your computer and use it in GitHub Desktop.
Save seesharprun/ace6b930706b4906fdbe65b5d8e92124 to your computer and use it in GitHub Desktop.
using Microsoft.Azure.Cosmos;
string endpoint = "<nosql-account-endpoint>";
string key = "<nosql-account-read-write-key>";
CosmosClient client = new(endpoint, key, options);
string databaseName = "<database-name>";
string containerName = "<container-name>";
Container container = client.GetContainer(databaseName, containerName);
// Upsert seed data
{
ChatHistory primaryUserChatHistoryFirst = new(
id: "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1",
entra_oid: "aaaabbbb-0000-cccc-1111-dddd2222eeee",
title: "What is included in my Northwind health plan?",
answers: [
"Your health plan includes a range of benefits and services. You can find out more about what is included in your health plan by checking your plan documents or contacting your health insurance provider.",
"Some of the benefits and services that may be included in your health plan are preventive care, prescription drug coverage, mental health services, and emergency care. Your health plan may also include coverage for hospital stays, surgeries, and other medical procedures.",
"It is important to review your plan documents to understand what is included in your health plan and how to access the benefits and services that are available to you."
],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
ChatHistory secondaryUserChatHistoryFirst = new(
id: "b1b1b1b1-cccc-dddd-eeee-f2f2f2f2f2f2",
entra_oid: "bbbbcccc-1111-dddd-2222-eeee3333ffff",
title: "How do I earn a promotion at Northwind?",
answers: [
"Earning a promotion at Northwind requires hard work, dedication, and a commitment to excellence. To earn a promotion, you should focus on developing your skills, taking on new challenges, and delivering results that exceed expectations.",
],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
ChatHistory tertiraryUserChatHistoryFirst = new(
id: "c2c2c2c2-dddd-eeee-ffff-a3a3a3a3a3a3",
entra_oid: "ccccdddd-2222-eeee-3333-ffff4444aaaa",
title: "How many service years are required to retire from Northwind?",
answers: [
"The number of service years required to retire from Northwind depends on your employment contract and retirement plan. In general, most employees are eligible to retire after reaching a certain age and completing a minimum number of service years.",
"You can find out more about the retirement eligibility requirements by reviewing your employment contract, retirement plan documents, or contacting your human resources department. They can provide you with information about the retirement benefits available to you and the steps you need to take to retire from Northwind."
],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
ChatHistory tertiraryUserChatHistorySecond = new(
id: "d3d3d3d3-eeee-ffff-aaaa-b4b4b4b4b4b4",
entra_oid: "ccccdddd-2222-eeee-3333-ffff4444aaaa",
title: "What does a product manager do at Northwind?",
answers: [
"A product manager at Northwind is responsible for developing and managing the company's products. This includes defining the product vision, strategy, and roadmap, as well as working with cross-functional teams to bring products to market.",
"Product managers work closely with engineering, design, marketing, and other teams to ensure that products meet customer needs and business goals. They are also responsible for analyzing market trends, gathering customer feedback, and monitoring product performance to make data-driven decisions.",
"Product managers play a key role in driving product innovation and growth at Northwind. They are responsible for identifying new opportunities, developing product features, and launching products that drive business success."
],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
SharedChatIndex primaryUserSharedChatsIndex = new(
id: "00000000-0000-0000-0000-000000000000",
entra_oid: "aaaabbbb-0000-cccc-1111-dddd2222eeee",
chats:
[
new SharedChat(
chat_id: "b1b1b1b1-cccc-dddd-eeee-f2f2f2f2f2f2",
entra_oid: "bbbbcccc-1111-dddd-2222-eeee3333ffff"
),
new SharedChat(
chat_id: "c2c2c2c2-dddd-eeee-ffff-a3a3a3a3a3a3",
entra_oid: "ccccdddd-2222-eeee-3333-ffff4444aaaa"
),
new SharedChat(
chat_id: "d3d3d3d3-eeee-ffff-aaaa-b4b4b4b4b4b4",
entra_oid: "ccccdddd-2222-eeee-3333-ffff4444aaaa"
)
],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
SharedChatIndex secondaryUserSharedChatsIndex = new(
id: "00000000-0000-0000-0000-000000000000",
entra_oid: "bbbbcccc-1111-dddd-2222-eeee3333ffff",
chats:
[
new SharedChat(
chat_id: "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1",
entra_oid: "aaaabbbb-0000-cccc-1111-dddd2222eeee"
)
],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
SharedChatIndex tertiaryUserSharedChatsIndex = new(
id: "00000000-0000-0000-0000-000000000000",
entra_oid: "ccccdddd-2222-eeee-3333-ffff4444aaaa",
chats:
[],
timestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds()
);
await container.CreateTransactionalBatch(new PartitionKey("aaaabbbb-0000-cccc-1111-dddd2222eeee"))
.UpsertItem(primaryUserChatHistoryFirst)
.UpsertItem(primaryUserSharedChatsIndex)
.ExecuteAsync();
await container.CreateTransactionalBatch(new PartitionKey("bbbbcccc-1111-dddd-2222-eeee3333ffff"))
.UpsertItem(secondaryUserChatHistoryFirst)
.UpsertItem(secondaryUserSharedChatsIndex)
.ExecuteAsync();
await container.CreateTransactionalBatch(new PartitionKey("ccccdddd-2222-eeee-3333-ffff4444aaaa"))
.UpsertItem(tertiraryUserChatHistoryFirst)
.UpsertItem(tertiraryUserChatHistorySecond)
.UpsertItem(tertiaryUserSharedChatsIndex)
.ExecuteAsync();
Console.WriteLine("Seed data upserted successfully.");
}
// Perform data operations
{
Console.WriteLine("Current user: aaaabbbb-0000-cccc-1111-dddd2222eeee");
// Get all personal chat history
List<ChatHistory> chatHistories = [];
{
Console.WriteLine("Retrieve chat history with query");
string query = "SELECT * FROM chats c WHERE c.type = 'chat_history' AND c.entra_oid = @entra_oid";
QueryDefinition queryDefinition = new(query);
queryDefinition.WithParameter("@entra_oid", "aaaabbbb-0000-cccc-1111-dddd2222eeee");
double requestCharge = 0;
FeedIterator<ChatHistory> queryResultSetIterator = container.GetItemQueryIterator<ChatHistory>(queryDefinition);
while (queryResultSetIterator.HasMoreResults)
{
FeedResponse<ChatHistory> currentResultSet = await queryResultSetIterator.ReadNextAsync();
requestCharge += currentResultSet.RequestCharge;
chatHistories.AddRange(currentResultSet);
}
foreach (ChatHistory chat in chatHistories)
{
Console.WriteLine($"\t[ID] {chat.id}");
}
Console.WriteLine($"Request units consumed: {requestCharge}");
}
// Get chats shared with current user
List<SharedChat> sharedChats = [];
{
Console.WriteLine("Retrieve shared chat index with single point read");
ItemResponse<SharedChatIndex> sharedChatIndexResponse = await container.ReadItemAsync<SharedChatIndex>(
"00000000-0000-0000-0000-000000000000",
new PartitionKey("aaaabbbb-0000-cccc-1111-dddd2222eeee")
);
Console.WriteLine($"\t[ID] {sharedChatIndexResponse.Resource.id}");
Console.WriteLine($"Request units consumed: {sharedChatIndexResponse.RequestCharge}");
sharedChats.AddRange(sharedChatIndexResponse.Resource.chats);
}
// Get chat history of shared chats
List<ChatHistory> sharedChatHistories = [];
{
Console.WriteLine("Retrieve shared chat history with multiple point reads");
List<Task<ItemResponse<ChatHistory>>> sharedChatHistoryTasks = sharedChats.Select(sharedChat =>
container.ReadItemAsync<ChatHistory>(
sharedChat.chat_id,
new PartitionKey(sharedChat.entra_oid)
)
).ToList();
await Task.WhenAll(sharedChatHistoryTasks);
double requestCharge = sharedChatHistoryTasks.Sum(task => task.Result.RequestCharge);
sharedChatHistories.AddRange(sharedChatHistoryTasks.Select(task => task.Result.Resource));
foreach (ChatHistory chat in sharedChatHistories)
{
Console.WriteLine($"\t[ID] {chat.id}");
}
Console.WriteLine($"Request units consumed: {requestCharge}");
}
}
record SharedChatIndex(
string id,
string entra_oid,
SharedChat[] chats,
long timestamp,
string type = "chat_index"
);
record SharedChat(
string chat_id,
string entra_oid
);
record ChatHistory(
string id,
string entra_oid,
string title,
string[] answers,
long timestamp,
string type = "chat_history"
);
@seesharprun
Copy link
Author

To run this:

  1. Make sure you have .NET 9 or later.

  2. In the shell, run:

    dotnet new console
    dotnet add package Microsoft.Azure.Cosmos
  3. Use this code in Program.cs.

  4. In the shell, run:

    dotnet run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment