openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Conversations/Example01_ConversationProtocolAsync.cs
77lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Conversations; |
| 3 | using System; |
| 4 | using System.ClientModel; |
| 5 | using System.ClientModel.Primitives; |
| 6 | using System.Text.Json; |
| 7 | using System.Threading.Tasks; |
| 8 | |
| 9 | namespace OpenAI.Examples; |
| 10 | |
| 11 | // This example uses experimental APIs which are subject to change. To use experimental APIs, |
| 12 | // please acknowledge their experimental status by suppressing the corresponding warning. |
| 13 | #pragma warning disable OPENAI001 |
| 14 | |
| 15 | public partial class ConversationExamples |
| 16 | { |
| 17 | [Test] |
| 18 | public async Task Example01_ConversationProtocolAsync() |
| 19 | { |
| 20 | ConversationClient client = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 21 | |
| 22 | BinaryData createConversationParameters = BinaryData.FromBytes(""" |
| 23 | { |
| 24 | "metadata": { "topic": "test" }, |
| 25 | "items": [ |
| 26 | { |
| 27 | "type": "message", |
| 28 | "role": "user", |
| 29 | "content": "Say 'this is a test.'" |
| 30 | }, |
| 31 | { |
| 32 | "type": "message", |
| 33 | "role": "assistant", |
| 34 | "content": "This is a test." |
| 35 | } |
| 36 | ] |
| 37 | } |
| 38 | """u8.ToArray()); |
| 39 | |
| 40 | using BinaryContent createConversationRequestContent = BinaryContent.Create(createConversationParameters); |
| 41 | ClientResult createConversationResult = await client.CreateConversationAsync(createConversationRequestContent); |
| 42 | using JsonDocument createConversationResultAsJson = JsonDocument.Parse(createConversationResult.GetRawResponse().Content.ToString()); |
| 43 | string conversationId = createConversationResultAsJson.RootElement |
| 44 | .GetProperty("id"u8) |
| 45 | .GetString(); |
| 46 | |
| 47 | Console.WriteLine($"Conversation created."); |
| 48 | Console.WriteLine($" Conversation ID: {conversationId}"); |
| 49 | Console.WriteLine(); |
| 50 | |
| 51 | AsyncCollectionResult getConversationItemsResults = client.GetConversationItemsAsync(conversationId); |
| 52 | await foreach(ClientResult result in getConversationItemsResults.GetRawPagesAsync()) |
| 53 | { |
| 54 | using JsonDocument getConversationItemsResultAsJson = JsonDocument.Parse(result.GetRawResponse().Content.ToString()); |
| 55 | foreach (JsonElement element in getConversationItemsResultAsJson.RootElement.GetProperty("data").EnumerateArray()) |
| 56 | { |
| 57 | string messageId = element.GetProperty("id"u8).ToString(); |
| 58 | |
| 59 | Console.WriteLine($"Message retrieved."); |
| 60 | Console.WriteLine($" Message ID: {messageId}"); |
| 61 | Console.WriteLine(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | ClientResult deleteConversationResult = await client.DeleteConversationAsync(conversationId); |
| 66 | using JsonDocument deleteConversationResultAsJson = JsonDocument.Parse(deleteConversationResult.GetRawResponse().Content.ToString()); |
| 67 | bool deleted = deleteConversationResultAsJson.RootElement |
| 68 | .GetProperty("deleted"u8) |
| 69 | .GetBoolean(); |
| 70 | |
| 71 | Console.WriteLine($"Conversation deleted."); |
| 72 | Console.WriteLine($" Deleted: {deleted}"); |
| 73 | Console.WriteLine(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #pragma warning restore OPENAI001 |