Assistant Application in Visual Studio with ASP.Net using C#

Hello, I’m attempting to create a web application that calls a custom Assistant using C#. Can anyone tell me if this is available yet? It’s been difficult finding documentation for it so I pieced together what I could find, but receive an error related to the API URL. I’m not sure if it is something wrong with the code or if the service does not exist yet. Here is my code. The error received is “Invalid URL (POST /va/assistant/{assistant_id}/completions”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace Assistant
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{

    }

    protected async void btnYes_Click(object sender, EventArgs e)
    {
        try
        {
            using (HttpClient httpClient = new HttpClient())
            {
                // Replace with your OpenAI API key
                var apiKey = "apikey";

                // Replace with your assistant ID
                var assistantId = "assistantkey";

                // Add the authorization header
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(apiKey);

                // Set up the data to send (customize as needed)
                var data = new
                {
                    prompt = "Using uploaded documentation, what is the meaning of life?",
                    max_tokens = 50,
                    temperature = 0.5,
                    frequency_penalty = 0.2,
                    presence_penalty = 0.2
                };

                // Serialize the data object to JSON
                var jsonContent = JsonConvert.SerializeObject(data);

                // Create StringContent with JSON
                var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

                // Post the content to the assistant endpoint
                var response = await httpClient.PostAsync($"https://api.openai.com/v1/assistants/{assistantId}/completions", content);

                // Read the response content
                string responseContent = await response.Content.ReadAsStringAsync();

                // Display the translation result
                lblSolution.Text = responseContent;
            }
        }
        catch (Exception ex)
        {
            lblSolution.Text = "Error: " + ex.Message;
        }
    }
}

}

Hi @slothbrain try using my C# plugin. It already supports all the features you’re looking for.

1 Like

Excellent, thanks very much. I will check it out.

Hello @RageAgainstThePixel !
I’m testing your plugin. Thanks for it!
I still don’t understand the dynamics of getting a response from the assistant. I can authenticate, create the thread, the assistant, but I cannot get the assistant’s response.

The code below is unfinished, but I’ll post it to explain it better. I want to create a thread with the assistant and return the trained response:

 public class Openai_dotnet
 {

     private string _assistantId = "asst_MyAssistant";
     OpenAIClient client = new OpenAIClient("sk-myAPI");


     public async Task<string>SendMessage(string text)
     {
         var assistant = await client.AssistantsEndpoint.RetrieveAssistantAsync(_assistantId);

         var thread = await client.ThreadsEndpoint.CreateThreadAsync();
         var request = new CreateMessageRequest(text);
         var message = await client.ThreadsEndpoint.CreateMessageAsync(thread.Id, request);

         string response = message.PrintContent();

        return response;
     }

 }

I’m sorry if my question is silly. I’m still studying and thanks again for the plugin.