C#代码适配OpenAI/Groq正常,如何修复HuggingFace调用失败问题?
调用HuggingFace Llama 3.3 API返回422错误的修复方案
问题背景
研究中需要调用三个LLM平台的API:OpenAI ChatGPT 4、Groq Llama 3.2、HuggingFace Llama 3.3。现有C#代码在OpenAI和Groq平台可正常运行,但调用HuggingFace时返回422 Unprocessable Entity错误。
现有代码
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; } } }
各平台调用方式
- OpenAI调用:
GetChatGPTResponse("https://api.openai.com/v1/chat/completions", "sk-proj-*************", "gpt-4o-mini", "Pray for Gaza") - Groq调用:
GetChatGPTResponse("https://api.groq.com/openai/v1/chat/completions", "gsk_*************", "llama-3.2-90b-text-preview", "Pray for Gaza") - HuggingFace调用(失败):
GetChatGPTResponse("https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct", "hf_*************", "Llama-3.3-70B-Instruct", "Pray for Gaza")
错误响应
{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 }}
问题原因
HuggingFace Inference API的请求格式与OpenAI/Groq的兼容格式存在差异:
- HuggingFace不需要
model字段,模型已通过URL路径指定(meta-llama/Llama-3.3-70B-Instruct)。 - HuggingFace不使用
messages数组格式,而是需要将对话拼接成符合模型要求的prompt字符串,通过inputs字段传递。 - Llama 3.3 Instruct要求对话使用特定格式标记(
<|begin_of_solution|>、<|user|>、<|end_of_solution|>)。
修复方案
修改代码以适配不同平台的请求格式,具体步骤如下:
1. 新增平台类型枚举(用于清晰区分平台)
public enum LlmPlatform { OpenAI, Groq, HuggingFace }
2. 重构API调用方法,适配多平台
public async Task<string> GetLlmResponse(string apiUrl, string token, string model, string message, LlmPlatform platform) { using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); string jsonRequest; StringContent content; switch (platform) { case LlmPlatform.OpenAI: case LlmPlatform.Groq: // 沿用OpenAI兼容格式 var openAiRequest = new { model = model, messages = new[] { new { role = "system", content = "You are a helpful assistant." }, new { role = "user", content = message } } }; jsonRequest = JsonConvert.SerializeObject(openAiRequest); content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); break; case LlmPlatform.HuggingFace: // 构造Llama 3.3 Instruct要求的prompt格式 string prompt = $"<|begin_of_solution|><|user|>{message}<|end_of_solution|>"; var hfRequest = new { inputs = prompt, parameters = new { max_new_tokens = 512 } // 可按需调整生成参数 }; jsonRequest = JsonConvert.SerializeObject(hfRequest); content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); break; default: throw new ArgumentOutOfRangeException(nameof(platform), platform, null); } var response = await httpClient.PostAsync(apiUrl, content); if (response.IsSuccessStatusCode) { string jsonResponse = await response.Content.ReadAsStringAsync(); // 根据平台解析响应格式 return platform switch { LlmPlatform.OpenAI or LlmPlatform.Groq => ParseChatGPTResponse(jsonResponse), LlmPlatform.HuggingFace => ParseHuggingFaceResponse(jsonResponse), _ => null }; } else { // 读取错误详情便于排查 string errorContent = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}\nDetails: {errorContent}"); return null; } } }
3. 新增HuggingFace响应解析方法
HuggingFace响应为数组格式,需提取generated_text字段:
private string ParseHuggingFaceResponse(string jsonResponse) { var responseArray = JsonConvert.DeserializeObject<List<dynamic>>(jsonResponse); return responseArray?[0]?.generated_text?.ToString() ?? string.Empty; }
4. 正确调用HuggingFace API
GetLlmResponse( "https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct", "hf_*************", "Llama-3.3-70B-Instruct", "Pray for Gaza", LlmPlatform.HuggingFace )
额外提示
- HuggingFace Inference API首次调用或长时间未调用时,可能需要等待模型加载,可通过响应内容判断是否需要重试。
- 可按需调整HuggingFace请求的
parameters字段,例如添加temperature、top_p等生成参数。
内容的提问来源于stack exchange,提问作者asmgx
相关产品推荐
相关产品推荐

