openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Assistants/Example01_RetrievalAugmentedGeneration.cs
152lines · modeblame
9f9f2936Jose Arriaga Maldonado2 years ago | 1 | using NUnit.Framework; |
| 2 | using OpenAI.Assistants; | |
| 3 | using OpenAI.Files; | |
| 4 | using System; | |
| 5 | using System.ClientModel; | |
| 6 | using System.IO; | |
| 7 | using System.Threading; | |
| 8 | | |
8cc6643fJose Arriaga Maldonado2 years ago | 9 | namespace OpenAI.Examples; |
9f9f2936Jose Arriaga Maldonado2 years ago | 10 | |
5dce104aJose Arriaga Maldonado1 years ago | 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 | | |
8cc6643fJose Arriaga Maldonado2 years ago | 15 | public partial class AssistantExamples |
9f9f2936Jose Arriaga Maldonado2 years ago | 16 | { |
| 17 | [Test] | |
8cc6643fJose Arriaga Maldonado2 years ago | 18 | public void Example01_RetrievalAugmentedGeneration() |
9f9f2936Jose Arriaga Maldonado2 years ago | 19 | { |
| 20 | OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | |
31c2ba63Jose Arriaga Maldonado1 years ago | 21 | OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient(); |
9f9f2936Jose Arriaga Maldonado2 years ago | 22 | AssistantClient assistantClient = openAIClient.GetAssistantClient(); |
| 23 | | |
| 24 | // First, let's contrive a document we'll use retrieval with and upload it. | |
e4af1691Jose Arriaga Maldonado1 years ago | 25 | using Stream document = BinaryData.FromBytes(""" |
9f9f2936Jose Arriaga Maldonado2 years ago | 26 | { |
| 27 | "description": "This document contains the sale history data for Contoso products.", | |
| 28 | "sales": [ | |
| 29 | { | |
| 30 | "month": "January", | |
| 31 | "by_product": { | |
| 32 | "113043": 15, | |
| 33 | "113045": 12, | |
| 34 | "113049": 2 | |
| 35 | } | |
| 36 | }, | |
| 37 | { | |
| 38 | "month": "February", | |
| 39 | "by_product": { | |
| 40 | "113045": 22 | |
| 41 | } | |
| 42 | }, | |
| 43 | { | |
| 44 | "month": "March", | |
| 45 | "by_product": { | |
| 46 | "113045": 16, | |
| 47 | "113055": 5 | |
| 48 | } | |
| 49 | } | |
| 50 | ] | |
| 51 | } | |
e4af1691Jose Arriaga Maldonado1 years ago | 52 | """u8.ToArray()).ToStream(); |
9f9f2936Jose Arriaga Maldonado2 years ago | 53 | |
19ceae44ShivangiReja1 years ago | 54 | OpenAIFile salesFile = fileClient.UploadFile( |
9f9f2936Jose Arriaga Maldonado2 years ago | 55 | document, |
| 56 | "monthly_sales.json", | |
| 57 | FileUploadPurpose.Assistants); | |
| 58 | | |
| 59 | // Now, we'll create a client intended to help with that data | |
| 60 | AssistantCreationOptions assistantOptions = new() | |
| 61 | { | |
| 62 | Name = "Example: Contoso sales RAG", | |
| 63 | Instructions = | |
| 64 | "You are an assistant that looks up sales data and helps visualize the information based" | |
| 65 | + " on user queries. When asked to generate a graph, chart, or other visualization, use" | |
| 66 | + " the code interpreter tool to do so.", | |
| 67 | Tools = | |
| 68 | { | |
| 69 | new FileSearchToolDefinition(), | |
| 70 | new CodeInterpreterToolDefinition(), | |
| 71 | }, | |
| 72 | ToolResources = new() | |
| 73 | { | |
| 74 | FileSearch = new() | |
| 75 | { | |
| 76 | NewVectorStores = | |
| 77 | { | |
| 78 | new VectorStoreCreationHelper([salesFile.Id]), | |
| 79 | } | |
| 80 | } | |
| 81 | }, | |
| 82 | }; | |
| 83 | | |
| 84 | Assistant assistant = assistantClient.CreateAssistant("gpt-4o", assistantOptions); | |
| 85 | | |
| 86 | // Now we'll create a thread with a user query about the data already associated with the assistant, then run it | |
| 87 | ThreadCreationOptions threadOptions = new() | |
| 88 | { | |
1c40de67Krzysztof Cwalina2 years ago | 89 | InitialMessages = { "How well did product 113045 sell in February? Graph its trend over time." } |
9f9f2936Jose Arriaga Maldonado2 years ago | 90 | }; |
| 91 | | |
| 92 | ThreadRun threadRun = assistantClient.CreateThreadAndRun(assistant.Id, threadOptions); | |
| 93 | | |
| 94 | // Check back to see when the run is done | |
| 95 | do | |
| 96 | { | |
| 97 | Thread.Sleep(TimeSpan.FromSeconds(1)); | |
| 98 | threadRun = assistantClient.GetRun(threadRun.ThreadId, threadRun.Id); | |
| 99 | } while (!threadRun.Status.IsTerminal); | |
| 100 | | |
| 101 | // Finally, we'll print out the full history for the thread that includes the augmented generation | |
2ab1a942Jose Arriaga Maldonado1 years ago | 102 | CollectionResult<ThreadMessage> messages |
| 103 | = assistantClient.GetMessages(threadRun.ThreadId, new MessageCollectionOptions() { Order = MessageCollectionOrder.Ascending }); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 104 | |
| 105 | foreach (ThreadMessage message in messages) | |
| 106 | { | |
| 107 | Console.Write($"[{message.Role.ToString().ToUpper()}]: "); | |
| 108 | foreach (MessageContent contentItem in message.Content) | |
| 109 | { | |
| 110 | if (!string.IsNullOrEmpty(contentItem.Text)) | |
| 111 | { | |
| 112 | Console.WriteLine($"{contentItem.Text}"); | |
| 113 | | |
| 114 | if (contentItem.TextAnnotations.Count > 0) | |
| 115 | { | |
| 116 | Console.WriteLine(); | |
| 117 | } | |
| 118 | | |
| 119 | // Include annotations, if any. | |
| 120 | foreach (TextAnnotation annotation in contentItem.TextAnnotations) | |
| 121 | { | |
| 122 | if (!string.IsNullOrEmpty(annotation.InputFileId)) | |
| 123 | { | |
| 124 | Console.WriteLine($"* File citation, file ID: {annotation.InputFileId}"); | |
| 125 | } | |
| 126 | if (!string.IsNullOrEmpty(annotation.OutputFileId)) | |
| 127 | { | |
| 128 | Console.WriteLine($"* File output, new file ID: {annotation.OutputFileId}"); | |
| 129 | } | |
| 130 | } | |
| 131 | } | |
| 132 | if (!string.IsNullOrEmpty(contentItem.ImageFileId)) | |
| 133 | { | |
19ceae44ShivangiReja1 years ago | 134 | OpenAIFile imageInfo = fileClient.GetFile(contentItem.ImageFileId); |
9f9f2936Jose Arriaga Maldonado2 years ago | 135 | BinaryData imageBytes = fileClient.DownloadFile(contentItem.ImageFileId); |
| 136 | using FileStream stream = File.OpenWrite($"{imageInfo.Filename}.png"); | |
| 137 | imageBytes.ToStream().CopyTo(stream); | |
| 138 | | |
| 139 | Console.WriteLine($"<image: {imageInfo.Filename}.png>"); | |
| 140 | } | |
| 141 | } | |
| 142 | Console.WriteLine(); | |
| 143 | } | |
| 144 | | |
| 145 | // Optionally, delete any persistent resources you no longer need. | |
| 146 | _ = assistantClient.DeleteThread(threadRun.ThreadId); | |
a330c2e7Jose Arriaga Maldonado1 years ago | 147 | _ = assistantClient.DeleteAssistant(assistant.Id); |
13a9c686Jose Arriaga Maldonado1 years ago | 148 | _ = fileClient.DeleteFile(salesFile.Id); |
9f9f2936Jose Arriaga Maldonado2 years ago | 149 | } |
| 150 | } | |
5dce104aJose Arriaga Maldonado1 years ago | 151 | |
| 152 | #pragma warning restore OPENAI001 |