forked from jgravelle/GroqApiLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroqLlmProvider.cs
48 lines (41 loc) · 1.2 KB
/
GroqLlmProvider.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json;
using System.Threading.Tasks;
namespace GroqApiLibrary
{
public interface ILlmProvider
{
Task<string> GenerateAsync(string prompt);
}
public class GroqLlmProvider : ILlmProvider, IDisposable
{
private readonly GroqApiClient _client;
private readonly string _model;
public GroqLlmProvider(string apiKey, string model)
{
_client = new GroqApiClient(apiKey);
_model = model;
}
public async Task<string> GenerateAsync(string prompt)
{
var request = new JsonObject
{
["model"] = _model,
["messages"] = JsonSerializer.SerializeToNode(new[]
{
new { role = "user", content = prompt }
})
};
var response = await _client.CreateChatCompletionAsync(request);
return response?["choices"]?[0]?["message"]?["content"]?.GetValue<string>() ?? string.Empty;
}
public void Dispose()
{
_client.Dispose();
}
}
}