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