StatusCode: 422, ReasonPhrase: 'Unprocessable Entity', when call APIs
#55
by
asmgx
- opened
I am doing some research and I have to use LLM models on different platforms APIs
1- OpenAI ChatGPT 4
2- Groq Llama 3.2
3- HuggingFace Llama 3.3
I built code that works on OpenAI ChatGPT 4 & Groq Llama 3.2
but when I tried to call HuggingFace Llama 3.3 it failed.
Here is the code
public async Task<string> GetChatGPTResponse(string apiUrl, string token, string model, string message)
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
// Prepare the request data
var requestData = new
{
model = model,
messages = new[]
{
new
{
role = "system",
content = "You are a helpful assistant."
},
new
{
role = "user",
content = message
}
}
};
// Convert the request data to JSON
var jsonRequest = JsonConvert.SerializeObject(requestData);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
// Send the request to ChatGPT API
var response = await httpClient.PostAsync(apiUrl, content);
// Check if the request was successful
if (response.IsSuccessStatusCode)
{
// Read and return the content value from the response
string jsonResponse = await response.Content.ReadAsStringAsync();
string Parse = ParseChatGPTResponse(jsonResponse);
return Parse;
}
else
{
// Handle the error, e.g., log it or throw an exception
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
return null;
}
}
}
I call OpenAI
GetChatGPTResponse("https://api.openai.com/v1/chat/completions", "sk-proj-*************", "gpt-4o-mini", "Pray for Gaza")
and Groq by using
GetChatGPTResponse("https://api.groq.com/openai/v1/chat/completions", "gsk_*************", "llama-3.2-90b-text-preview", "Pray for Gaza")
but when i call HuggingFace
GetChatGPTResponse("https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct", "hf_*************", "Llama-3.3-70B-Instruct", "Pray for Gaza")
when I call it for HuggingFace API resonse value is
{StatusCode: 422, ReasonPhrase: 'Unprocessable Entity', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Fri, 03 Jan 2025 03:04:47 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Vary: origin, access-control-request-method, access-control-request-headers, Origin, Access-Control-Request-Method, Access-Control-Request-Headers
x-sha: 6f6073b423013f9a7d2d9f29134061ffbfbc386b
Access-Control-Allow-Origin: *
X-Request-ID: _XIwrr6V30LZL-mFy9xBs
Access-Control-Allow-Credentials: true
Content-Type: text/plain; charset=utf-8
}}
Is my calling to the API wrong? if so how to fix it?
if not then How can fix the code so it works for all 3 platforms?