-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroqApiClient.cs
More file actions
74 lines (66 loc) · 2.56 KB
/
GroqApiClient.cs
File metadata and controls
74 lines (66 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace GroqApiLibrary
{
public class GroqApiClient : IDisposable
{
private readonly HttpClient _httpClient;
private const string BaseUrl = "https://api.groq.com/openai/v1/chat/completions";
public GroqApiClient(string apiKey)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
}
public async Task<JObject?> CreateChatCompletionAsync(JObject request)
{
try
{
var content = new StringContent(request.ToString(), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(BaseUrl, content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<JObject>(jsonResponse);
}
catch (Exception ex)
{
Debug.LogError($"Error in CreateChatCompletionAsync: {ex}");
return null;
}
}
public async IAsyncEnumerable<JObject?> CreateChatCompletionStreamAsync(JObject request)
{
request["stream"] = true;
var content = new StringContent(request.ToString(), Encoding.UTF8, "application/json");
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, BaseUrl) { Content = content };
using var response = await _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
string? line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (line.StartsWith("data: "))
{
var data = line.Substring("data: ".Length);
if (data != "[DONE]")
{
yield return JsonConvert.DeserializeObject<JObject>(data);
}
}
}
}
public void Dispose()
{
_httpClient.Dispose();
GC.SuppressFinalize(this);
}
}
}