Sarel-Esterhuizen / GroqSharp

GroqSharp is a C# client library that makes it easy to interact with GroqCloud. It's designed to provide a simple and flexible interface, allowing you to seamlessly integrate the Groq service into your C# applications.
MIT License
10 stars 1 forks source link

How to set the "response_format" type to "Json_object"? #7

Open FractalMind opened 1 month ago

FractalMind commented 1 month ago

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

string jsonStructure = @"
        {
          ""companyName"" : ""string | The name of the company"",
          ""offerSummary"" : ""string | The main offer and services of the website""
        }
      ";
...
var groqClient = new global::GroqClient(apiKey, apiModel).SetTemperature(0.5)
        .SetMaxTokens(512)
        .SetTopP(1)
        .SetStop("NONE")
        .SetStructuredRetryPolicy(5);
FractalMind commented 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;
    }