Open FractalMind opened 1 month ago
I had to use the classic "System.Net.Http.HttpClient()" While GroqSharp fixes their "GetStructuredChatCompletionAsync"
public class GroqResponseDto
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("object")]
public string Object { get; set; }
[JsonPropertyName("created")]
public long Created { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("choices")]
public List<Choice> Choices { get; set; }
[JsonPropertyName("usage")]
public Usage Usage { get; set; }
}
public class Choice
{
[JsonPropertyName("index")]
public int Index { get; set; }
[JsonPropertyName("message")]
public Message Message { get; set; }
[JsonPropertyName("logprobs")]
public object LogProbs { get; set; }
[JsonPropertyName("finish_reason")]
public string FinishReason { get; set; }
}
public class Message
{
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}
public class Usage
{
[JsonPropertyName("queue_time")]
public double QueueTime { get; set; }
[JsonPropertyName("prompt_tokens")]
public int PromptTokens { get; set; }
[JsonPropertyName("prompt_time")]
public double PromptTime { get; set; }
[JsonPropertyName("completion_tokens")]
public int CompletionTokens { get; set; }
[JsonPropertyName("completion_time")]
public double CompletionTime { get; set; }
[JsonPropertyName("total_tokens")]
public int TotalTokens { get; set; }
[JsonPropertyName("total_time")]
public double TotalTime { get; set; }
}
public async Task<string> sendHttpRequest(GroqRequestDto groqRequestDto)
{
var query = JsonConvert.SerializeObject(groqRequestDto);
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.groq.com/openai/v1/chat/completions");
request.Headers.Add("Authorization", $"Bearer {_groqSettings.Value.ApiKey}");
request.Content = new StringContent(query, Encoding.UTF8, "application/json");
var response = await new System.Net.Http.HttpClient().SendAsync(request);
response.EnsureSuccessStatusCode();
var contentString = await response.Content.ReadAsStringAsync();
var content = JsonConvert.DeserializeObject<GroqResponseDto>(contentString);
if (!content.Choices.Any()){
throw new Exception("Groq API did not generated any content for this request");
}
return content.Choices[0].Message.Content;
}
https://console.groq.com/docs/text-chat#json-mode-object-object
How can I use "response_format": {"type": "json_object"} with GroqSharp?
The GetStructuredChatCompletionAsync does not yield JSON