Artificial Intelligence (AI) has transformed many industries by rendering capabilities that were previously held to be futuristic a present reality. OpenAI offers the Assistant API, so developers can easily integrate AI-based conversational agents into the applications. The Assistant API that OpenAI offers is based on powerful natural language processing capabilities to be integrated into.NET. This article will guide the reader through the process for setting up and using Assistant API in a.NET environment, using C#.
Prerequisites
Before we begin, ensure you have the following.
- Visual Studio (or any .NET IDE)
- .NET SDK installed
- OpenAI API key (sign up at OpenAI’s website if you haven’t already)
Step 1. Setting up your .NET ProjectCreate a new .NET project in Visual Studio
- Open Visual Studio and select Create a new project.
- Choose the appropriate project template (e.g., Console Application).
- Name your project and click Create.
Install the OpenAI package
- Open the NuGet Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console).
- Run the following command to install the OpenAI package: Install-Package OpenAI.API.
Step 2. Configuring API Credentials
Retrieve your OpenAI API key
- Log in to your OpenAI account and navigate to your API settings.
- Copy your API key.
Retrieve your Assistant ID
- Log in to your OpenAI account and navigate to Assistants from the left panel. Then click on Create Assistant.
- Now fill out the details for the assistant, who will be a weather agent.
After your assistant successfully creates, it will be assigned an assistant ID. We will use this assistant programmatically in the future, so grab the ID.
Store your API key and Assistant ID securely.
For demonstration purposes, we’ll store the API key and Assistant ID in an environment variable or a secure configuration file. Ensure it’s not hard-coded in your source code.
Step 3. Implementing API Integration
Create a class named OpenAiAssistantHandler in your project and add the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <span class="token keyword keyword-using">using</span> <span class="token namespace">Newtonsoft<span class="token punctuation">.</span>Json</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">Newtonsoft<span class="token punctuation">.</span>Json<span class="token punctuation">.</span>Linq</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">RestSharp</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">System</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">System<span class="token punctuation">.</span>Net<span class="token punctuation">.</span>Http</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">System<span class="token punctuation">.</span>Text</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token namespace">System<span class="token punctuation">.</span>Threading<span class="token punctuation">.</span>Tasks</span><span class="token punctuation">;</span> <span class="token keyword keyword-namespace">namespace</span> <span class="token namespace">YourNamespace</span> <span class="token punctuation">{</span> <span class="token keyword keyword-public">public</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">OpenAiAssistantHandler</span> <span class="token punctuation">{</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-readonly">readonly</span> <span class="token class-name">HttpClient</span> httpClient <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">HttpClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-readonly">readonly</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> OPEN_AI_BASE_URL <span class="token operator">=</span> <span class="token string">"https://api.openai.com/v1"</span><span class="token punctuation">;</span> <span class="token keyword keyword-public">public</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span><span class="token keyword keyword-string">string</span><span class="token punctuation">></span></span> <span class="token function">CreateThreadAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> createThreadUrl <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">$"</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">OPEN_AI_BASE_URL</span><span class="token punctuation">}</span></span><span class="token string">/threads"</span></span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> requestBody <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token punctuation">{</span> <span class="token comment">/* Include any necessary data for creating a thread */</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword keyword-try">try</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> httpRequestMessage <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">HttpRequestMessage</span><span class="token punctuation">(</span>HttpMethod<span class="token punctuation">.</span>Post<span class="token punctuation">,</span> createThreadUrl<span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Headers<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"Authorization"</span><span class="token punctuation">,</span> <span class="token interpolation-string"><span class="token string">$"Bearer </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">apiKey</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Headers<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"OpenAI-Beta"</span><span class="token punctuation">,</span> <span class="token string">"assistants=v2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Content <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">StringContent</span><span class="token punctuation">(</span>JsonConvert<span class="token punctuation">.</span><span class="token function">SerializeObject</span><span class="token punctuation">(</span>requestBody<span class="token punctuation">)</span><span class="token punctuation">,</span> Encoding<span class="token punctuation">.</span>UTF8<span class="token punctuation">,</span> <span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-var">var</span></span> httpResponseMessage <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> httpClient<span class="token punctuation">.</span><span class="token function">SendAsync</span><span class="token punctuation">(</span>httpRequestMessage<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>httpResponseMessage<span class="token punctuation">.</span>IsSuccessStatusCode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> jsonResponse <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> httpResponseMessage<span class="token punctuation">.</span>Content<span class="token punctuation">.</span><span class="token function">ReadAsStringAsync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">JObject</span> threadData <span class="token operator">=</span> JObject<span class="token punctuation">.</span><span class="token function">Parse</span><span class="token punctuation">(</span>jsonResponse<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> threadData<span class="token punctuation">[</span><span class="token string">"id"</span><span class="token punctuation">]</span><span class="token punctuation">?.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Error: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">httpResponseMessage<span class="token punctuation">.</span>ReasonPhrase</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword keyword-catch">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Exception occurred: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">ex<span class="token punctuation">.</span>Message</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword keyword-public">public</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span><span class="token keyword keyword-string">string</span><span class="token punctuation">></span></span> <span class="token function">CallAssistantAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantId<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> threadId<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> userPrompt<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-try">try</span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token keyword keyword-string">string</span><span class="token punctuation">.</span><span class="token function">IsNullOrEmpty</span><span class="token punctuation">(</span>threadId<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token keyword keyword-string">string</span><span class="token punctuation">.</span><span class="token function">IsNullOrEmpty</span><span class="token punctuation">(</span>userPrompt<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword keyword-throw">throw</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">ArgumentException</span><span class="token punctuation">(</span><span class="token string">"Thread ID and user prompt must not be null or empty."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> messageId <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> <span class="token function">AddMessageToThreadAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">,</span> userPrompt<span class="token punctuation">,</span> threadId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> runId <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> <span class="token function">RunMessageThreadAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">,</span> assistantId<span class="token punctuation">,</span> threadId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token keyword keyword-string">string</span><span class="token punctuation">.</span><span class="token function">IsNullOrEmpty</span><span class="token punctuation">(</span>runId<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword keyword-throw">throw</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">InvalidOperationException</span><span class="token punctuation">(</span><span class="token string">"Failed to start assistant on the thread."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantResponse <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> <span class="token function">GetAssistantResponseAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">,</span> threadId<span class="token punctuation">,</span> messageId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> assistantResponse <span class="token operator">??</span> <span class="token string">"Seems to be a delay in response. Please try again, or try back later."</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-catch">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Handle exceptions or log errors here</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Error: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">ex<span class="token punctuation">.</span>Message</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span><span class="token keyword keyword-string">string</span><span class="token punctuation">></span></span> <span class="token function">AddMessageToThreadAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> userPrompt<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> threadId<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> url <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">$"</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">OPEN_AI_BASE_URL</span><span class="token punctuation">}</span></span><span class="token string">/threads/</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">threadId</span><span class="token punctuation">}</span></span><span class="token string">/messages"</span></span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> requestBody <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token punctuation">{</span> role <span class="token operator">=</span> <span class="token string">"user"</span><span class="token punctuation">,</span> content <span class="token operator">=</span> userPrompt <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> response <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> <span class="token function">SendPostRequestAsync</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> apiKey<span class="token punctuation">,</span> requestBody<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> response<span class="token punctuation">?.</span><span class="token function">GetValue</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">?.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span><span class="token keyword keyword-string">string</span><span class="token punctuation">></span></span> <span class="token function">RunMessageThreadAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantId<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> threadId<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> url <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">$"</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">OPEN_AI_BASE_URL</span><span class="token punctuation">}</span></span><span class="token string">/threads/</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">threadId</span><span class="token punctuation">}</span></span><span class="token string">/runs"</span></span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> requestBody <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token punctuation">{</span> assistant_id <span class="token operator">=</span> assistantId <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> response <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> <span class="token function">SendPostRequestAsync</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> apiKey<span class="token punctuation">,</span> requestBody<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> response<span class="token punctuation">?.</span><span class="token function">GetValue</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">?.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span><span class="token keyword keyword-string">string</span><span class="token punctuation">></span></span> <span class="token function">GetAssistantResponseAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> threadId<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> messageId<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-int">int</span></span> maxAttempts <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-int">int</span></span> attempts <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantResponse <span class="token operator">=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">;</span> <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span>attempts <span class="token operator"><</span> maxAttempts<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-await">await</span> Task<span class="token punctuation">.</span><span class="token function">Delay</span><span class="token punctuation">(</span><span class="token number">4000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Wait for 4 seconds before checking for a response</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> url <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">$"</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">OPEN_AI_BASE_URL</span><span class="token punctuation">}</span></span><span class="token string">/threads/</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">threadId</span><span class="token punctuation">}</span></span><span class="token string">/messages"</span></span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> response <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> <span class="token function">SendGetRequestAsync</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> apiKey<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> messages <span class="token operator">=</span> response<span class="token punctuation">?.</span><span class="token function">GetValue</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">)</span> <span class="token keyword keyword-as">as</span> <span class="token class-name">JArray</span><span class="token punctuation">;</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>messages <span class="token operator">!=</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-foreach">foreach</span> <span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-var">var</span></span> message <span class="token keyword keyword-in">in</span> messages<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>message<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">Value</span><span class="token generic class-name"><span class="token punctuation"><</span><span class="token keyword keyword-string">string</span><span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token string">"role"</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">"assistant"</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> assistantResponse <span class="token operator">=</span> message<span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span><span class="token punctuation">?.</span><span class="token function">FirstOrDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">?</span><span class="token punctuation">[</span><span class="token string">"text"</span><span class="token punctuation">]</span><span class="token punctuation">?</span><span class="token punctuation">[</span><span class="token string">"value"</span><span class="token punctuation">]</span><span class="token punctuation">?.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-break">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword keyword-string">string</span><span class="token punctuation">.</span><span class="token function">IsNullOrEmpty</span><span class="token punctuation">(</span>assistantResponse<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword keyword-break">break</span><span class="token punctuation">;</span> attempts<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-return">return</span> assistantResponse<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span>JObject<span class="token punctuation">></span></span> <span class="token function">SendPostRequestAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> url<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-object">object</span></span> requestBody<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> httpRequestMessage <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">HttpRequestMessage</span><span class="token punctuation">(</span>HttpMethod<span class="token punctuation">.</span>Post<span class="token punctuation">,</span> url<span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Headers<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"Authorization"</span><span class="token punctuation">,</span> <span class="token interpolation-string"><span class="token string">$"Bearer </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">apiKey</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Headers<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"OpenAI-Beta"</span><span class="token punctuation">,</span> <span class="token string">"assistants=v2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Content <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">StringContent</span><span class="token punctuation">(</span>JsonConvert<span class="token punctuation">.</span><span class="token function">SerializeObject</span><span class="token punctuation">(</span>requestBody<span class="token punctuation">)</span><span class="token punctuation">,</span> Encoding<span class="token punctuation">.</span>UTF8<span class="token punctuation">,</span> <span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-var">var</span></span> httpResponseMessage <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> httpClient<span class="token punctuation">.</span><span class="token function">SendAsync</span><span class="token punctuation">(</span>httpRequestMessage<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>httpResponseMessage<span class="token punctuation">.</span>IsSuccessStatusCode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> jsonResponse <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> httpResponseMessage<span class="token punctuation">.</span>Content<span class="token punctuation">.</span><span class="token function">ReadAsStringAsync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> JsonConvert<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">DeserializeObject</span><span class="token generic class-name"><span class="token punctuation"><</span>JObject<span class="token punctuation">></span></span></span><span class="token punctuation">(</span>jsonResponse<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Error: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">httpResponseMessage<span class="token punctuation">.</span>ReasonPhrase</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword keyword-private">private</span> <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task<span class="token punctuation"><</span>JObject<span class="token punctuation">></span></span> <span class="token function">SendGetRequestAsync</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span></span> url<span class="token punctuation">,</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-var">var</span></span> httpRequestMessage <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token constructor-invocation class-name">HttpRequestMessage</span><span class="token punctuation">(</span>HttpMethod<span class="token punctuation">.</span>Get<span class="token punctuation">,</span> url<span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Headers<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"Authorization"</span><span class="token punctuation">,</span> <span class="token interpolation-string"><span class="token string">$"Bearer </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">apiKey</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> httpRequestMessage<span class="token punctuation">.</span>Headers<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"OpenAI-Beta"</span><span class="token punctuation">,</span> <span class="token string">"assistants=v2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-using">using</span> <span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-var">var</span></span> httpResponseMessage <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> httpClient<span class="token punctuation">.</span><span class="token function">SendAsync</span><span class="token punctuation">(</span>httpRequestMessage<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>httpResponseMessage<span class="token punctuation">.</span>IsSuccessStatusCode<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> jsonResponse <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> httpResponseMessage<span class="token punctuation">.</span>Content<span class="token punctuation">.</span><span class="token function">ReadAsStringAsync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> JsonConvert<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">DeserializeObject</span><span class="token generic class-name"><span class="token punctuation"><</span>JObject<span class="token punctuation">></span></span></span><span class="token punctuation">(</span>jsonResponse<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Error: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">httpResponseMessage<span class="token punctuation">.</span>ReasonPhrase</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-return">return</span> <span class="token keyword keyword-null">null</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Key Components
- CreateThreadAsync: Initiates a new conversation thread with OpenAI’s Assistant API.
- CallAssistantAsync: Sends a user prompt to the assistant and retrieves the response.
- AddMessageToThreadAsync: Adds a user message to an existing conversation thread.
- RunMessageThreadAsync: Starts the assistant on the specified thread to process messages.
- GetAssistantResponseAsync: Retrieves the assistant’s response to the user’s message from the thread.
- SendPostRequestAsync and SendGetRequestAsync: Helper methods for making HTTP POST and GET requests to the OpenAI API.
Example Usage in .NET Application
Here’s an example of how you can utilize the OpenAiAssistantHandler class in your .NET application to interact with the OpenAI Assistant API.
1 2 3 4 5 6 7 8 9 10 | <span class="token keyword keyword-static">static</span> <span class="token keyword keyword-async">async</span> <span class="token return-type class-name">Task</span> <span class="token function">Main</span><span class="token punctuation">(</span><span class="token class-name"><span class="token keyword keyword-string">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span></span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey <span class="token operator">=</span> <span class="token string">"sk-rJ8BuIrPaXUff0tHM3RnT3BlbkFUF2IFuzQuGYGKTkfm4UsHTA"</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> threadId <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> OpenAiAssistantHandler<span class="token punctuation">.</span><span class="token function">CreateThreadAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">)</span><span class="token punctuation">;</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Thread Id: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">threadId</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantId <span class="token operator">=</span> <span class="token string">"asst_QTYWvU4cQnom29CbLof21gP5"</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> prompt <span class="token operator">=</span> <span class="token string">"Hey! what is the weather condition in Hyderabad"</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantResponse <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> OpenAiAssistantHandler<span class="token punctuation">.</span><span class="token function">CallAssistantAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">,</span> assistantId<span class="token punctuation">,</span> threadId<span class="token punctuation">,</span> prompt<span class="token punctuation">)</span><span class="token punctuation">;</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span>assistantResponse<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Code explanation
API Key Declaration
1 | <span class="token class-name"><span class="token keyword keyword-string">string</span></span> apiKey <span class="token operator">=</span> <span class="token string">"sk-rJ8BuIrPaXUff0tHM3RnT3BlbkFUF2IFuzQuGYGKTkfm4UsHTA"</span><span class="token punctuation">;</span> |
Initializes the API key required to authenticate requests to the OpenAI API.
Creating a Thread
1 2 | <span class="token class-name"><span class="token keyword keyword-string">string</span></span> threadId <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> OpenAiAssistantHandler<span class="token punctuation">.</span><span class="token function">CreateThreadAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">)</span><span class="token punctuation">;</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">$"Thread Id: </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token expression language-csharp">threadId</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Creates a new conversation thread using CreateThreadAsync and prints the returned thread ID to the console.
Assistant ID and Prompt Declaration
1 2 | <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantId <span class="token operator">=</span> <span class="token string">"asst_QTYWvU4cQnom29CbLof21gP5"</span><span class="token punctuation">;</span> <span class="token class-name"><span class="token keyword keyword-string">string</span></span> prompt <span class="token operator">=</span> <span class="token string">"Hey! what is the weather condition in Hyderabad"</span><span class="token punctuation">;</span> |
Defines the assistant ID and the message prompt to be sent to the assistant.
Calling the Assistant
1 2 | <span class="token class-name"><span class="token keyword keyword-string">string</span></span> assistantResponse <span class="token operator">=</span> <span class="token keyword keyword-await">await</span> OpenAiAssistantHandler<span class="token punctuation">.</span><span class="token function">CallAssistantAsync</span><span class="token punctuation">(</span>apiKey<span class="token punctuation">,</span> assistantId<span class="token punctuation">,</span> threadId<span class="token punctuation">,</span> prompt<span class="token punctuation">)</span><span class="token punctuation">;</span> Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span>assistantResponse<span class="token punctuation">)</span><span class="token punctuation">;</span> |
Sends the prompt to the assistant within the context of the created thread using CallAssistantAsync and prints the assistant’s response to the console.
Conclusion
Integrating OpenAI’s Assistant API into .NET applications provides a powerful toolset for implementing natural language understanding and generation. By following the steps outlined in this article, you can harness the capabilities of OpenAI’s AI models within your own .NET projects effectively.
ASP.NET 8.0.7 Hosting Recommendation
ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution. ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.