-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroqApiUnity.cs
More file actions
46 lines (39 loc) · 1.2 KB
/
GroqApiUnity.cs
File metadata and controls
46 lines (39 loc) · 1.2 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
using UnityEngine;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using GroqApiLibrary;
public class GroqApiUnity : MonoBehaviour
{
private string apiKey = "YOUR_API_KEY";
private GroqApiClient groqApi;
void Start()
{
groqApi = new GroqApiClient(apiKey);
string exampleMessage = "Hello, how are you?";
OnSubmit(exampleMessage);
}
public async void OnSubmit(string userMessage)
{
if (!string.IsNullOrEmpty(userMessage))
{
var request = new JObject
{
["model"] = "llama3-8b-8192",
["temperature"] = 0.5,
["max_tokens"] = 100,
["top_p"] = 1,
["messages"] = new JArray
{
new JObject { ["role"] = "user", ["content"] = userMessage }
}
};
await GetChatCompletion(request);
}
}
private async Task GetChatCompletion(JObject request)
{
var result = await groqApi.CreateChatCompletionAsync(request);
var response = result?["choices"]?[0]?["message"]?["content"]?.ToString() ?? "No response found";
Debug.Log(response);
}
}