Hi there
im sorry if this has answered before but im new to this and just trying to get a simple C# Console app to query my custom Assistant
if i query the assistant it works ok
im connecting to the assistant ( dos uploaded and gives me correct answers when i query it on the site)
Creating a thread and posting my question to that
but im not getting assistant answers
can someone see where i am going wrong
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CustomGPTAssistant
{
class Program
{
static void Main(string[] args)
{
// Entry point for the program
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
string apiKey = "REDACTED for this post";
string AssistantID = "REDACTED for this post";
String MyQuestion = "tell me about Donations ";
// START create Thread
var requestData = new
{
assistant_id = AssistantID
};
// create Thread
var _httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
_httpClient.DefaultRequestHeaders.Add("OpenAI-Beta", "assistants=v1");
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("https://api.openai.com/v1/threads", null);
var responseContent = await response.Content.ReadAsStringAsync();
JObject result = (JObject)JsonConvert.DeserializeObject(responseContent);
string MyThreadID = result["id"].ToString();
//ask quetion
var messageData = new
{
role = "user",
content = MyQuestion
};
var _httpClientask = new HttpClient();
_httpClientask.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
_httpClientask.DefaultRequestHeaders.Add("OpenAI-Beta", "assistants=v1");
_httpClientask.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var contentAsk = new StringContent(JsonConvert.SerializeObject(messageData), Encoding.UTF8, "application/json");
var responseAsk = await _httpClient.PostAsync("https://api.openai.com/v1/threads", null);
var responseContentAsk = await responseAsk.Content.ReadAsStringAsync();
JObject resultAsk = (JObject)JsonConvert.DeserializeObject(responseContentAsk);
var runData = new
{
assistant_id = AssistantID
};
content = new StringContent(JsonConvert.SerializeObject(runData), Encoding.UTF8, "application/json");
response = await _httpClient.PostAsync($"https://api.openai.com/v1/threads/{MyThreadID}/runs", content);
responseContent = await response.Content.ReadAsStringAsync();
result = (JObject)JsonConvert.DeserializeObject(responseContent);
string RunID = result["id"].ToString();
// get result
var _httpClient4 = new HttpClient();
_httpClient4.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
_httpClient4.DefaultRequestHeaders.Add("OpenAI-Beta", "assistants=v1");
_httpClient4.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response4 = await _httpClient.GetAsync($"https://api.openai.com/v1/threads/{MyThreadID}/messages");
var responseContent4 = await response4.Content.ReadAsStringAsync();
JObject result4 = (JObject)JsonConvert.DeserializeObject(responseContent4);
string messageValue = result4["data"][0]["content"][0]["text"]["value"].ToString();
Console.Write(messageValue);
}
}
}