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