C#C
C#2y ago
Rem

How do I do a HTTP request in C#?

 public static async Task<string> MessageAI(string user, string system)
 {
     HttpClient client = new();
     client.BaseAddress = new("http://localhost:1234");
     client.DefaultRequestHeaders
         .Accept
         .Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     var json = @"{
         ""messages"": [
             {
                 ""role"": ""user"",
                 ""content"": user
             },
             {
                 ""role"": ""system"",
                 ""content"": system
             }
         ],
         ""temperature"": 0.7,
         ""max_tokens"": 80,
         ""stream"": false
     }";

     var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
     
     var response = await client.PostAsync("http://localhost:1234/v1/chat/completions", content);
     var responseContent = await response.Content.ReadAsStringAsync();

     return responseContent;
 }

I get bad request?
Was this page helpful?