C# Integration with OpenAI Assistants

ihsan güç
3 min readMar 8, 2024

--

Yazının Türkçe versiyonuna bu link aracılığıyla ulaşabilirsiniz.

Artificial intelligence techologies are gaining more and more ground in the software development world. OpenAI’s Assistants API opens the doors to smart integrations in the C# programming language. Using OpenAI Assistants, we will take a step-by-step look at how to develop an application that acts like a math tutor. To start with, we will see how to obtain the necessary keys from the OpenAI platform how to create and test an Assistant and finally how we can use this AI service with C#.
1-) Creating keys on the OpenAI side.
2-) Creating and testing Assistants on OpenAI side.
3-) Using the created key and assistants via c#.

Preview Form
Preview Form

1-) Creating keys on the OpenAI side.

After credit loading by entering the OpenAI platform section the key is created according to which areas will be accessed in the API keys section.

Key Creation Page
Key Creation Page

2-) Creating and testing Assistants on the OpenAI side.

We click on the OpenAI Assistants section and when we click the create button the following screen welcomes us;

Model Creation
Model Creation
  • Instructions: Where we train the model.
  • Model: Where we select the language model.
  • Functions: Allows you to define your application’s custom functions or external API’s to the assistant.
  • Code Interpreter: Allows the assistant to write and run code. This tool can handle files with different data and formatting and create files such as charts.
    Retrieval: Allows the assistant to retrieve information from files that you or your users upload. Once a file is uploaded the assistant automatically decides.
  • Files: By uploading files you enable the assistant to use the content in these files for retrieval and code interpreter if these tools are enabled.

After creating our model on the screen above we can get the assistant Id by going to the assistant section from the screen below.

Assistant Detail
Assistant Detail

3-) Using the created key and assistants over c#

First of all we need to do before starting the code;

- Select Assistants
- Create Thread
- Message insertion and Run Method
- Status Check
- Get Answer

Created Thread

We create one threads by assigning one post request using Assistant id.

Created Thread
Created Thread

Message Insertion and Run Method

When Adding the message we use thread Id but when running the run we use thread Id and assistant id.

Message Insertion and Run Method
Message Insertion and Run Method

Status Check

With the thread id and run id we can see the status of the message we are running and take action accordingly.

Status Check
Status Check

Get Answer

We can retrieve the message using the thread id if the message run is finished.

Get Answer
Get Answer

References

Thanks you for reading. Hope to see you in the next article…

--

--