· last year · Aug 01, 2024, 04:15 AM
1# You need an API key, you can get one for free at GROQ quite easily if you register as a user
2# https://console.groq.com/keys
3# I don't know for how long GROQ will leave this free to use, but here it is.
4
5# Nodes for the scene
6# %prompt is a TextEdit
7# %opt_models is an OptionButton
8# %lb_md is a Label (or a MarkdownLabel if you want to download the addon from the AssetLib)
9# %req is an HTTPRequest
10
11# https://console.groq.com/docs/api-reference
12
13extends Control
14
15var GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
16const API_KEY = "your_api_key_here" # Get your own I can't just share mine :P
17
18const MODELS_URL = "https://api.groq.com/openai/v1/models"
19var models: Array[String] = []
20var headers = [
21 "Content-Type: application/json",
22 "Authorization: Bearer %s"%API_KEY,
23 ]
24
25class GroqResult:
26 var choices: Array[GroqChoice]
27 var created: int
28 var id: String
29 var model: String
30 static func from_data(data: String) -> GroqResult:
31 var dict : Dictionary = JSON.parse_string(data)
32 var gr := GroqResult.new()
33 gr.choices = []
34 for d in dict.choices:
35 gr.choices.append(GroqChoice.from_dict(d))
36 gr.created = int(dict.created)
37 gr.id = dict.id
38 gr.model = dict.model
39 return gr
40
41class GroqChoice:
42 var finish_reason: String
43 var index: int
44 var logprobs: String
45 var message: Dictionary
46 var content: String
47 var role: String
48 static func from_dict(dict: Dictionary) -> GroqChoice:
49 var ch = GroqChoice.new()
50 ch.finish_reason = str(dict.finish_reason)
51 ch.index = int(dict.index)
52 ch.logprobs = str(dict.logprobs)
53 ch.message = dict.message
54 ch.content = dict.message.content
55 ch.role = dict.message.role
56 return ch
57
58
59func _ready() -> void:
60 get_models()
61
62func get_models() -> void:
63 models.clear()
64 %opt_models.clear()
65 %req.request(MODELS_URL, headers)
66 var response = await %req.request_completed
67 var _status = response[0]
68 var _response_code = response[1]
69 var _res_headers = JSON.stringify(response[2])
70 var raw: String = PackedByteArray(response[3]).get_string_from_utf8()
71 var data = JSON.parse_string(raw)
72 for d in data.data:
73 models.append(d.id)
74 %opt_models.add_item(d.id)
75
76func groq_completions(prompt: String) -> GroqChoice:
77 var model = %opt_models.get_item_text(%opt_models.selected)
78 if !model: model = "llama3-8b-8192"
79 var body = JSON.stringify({
80 "messages": [
81 {"role": "user",
82 "content": prompt}
83 ],
84 "model": model})
85
86 %req.request(GROQ_ENDPOINT, PackedStringArray(headers), HTTPClient.METHOD_POST, body)
87 var response = await %req.request_completed
88 var _status = response[0]
89 var _response_code = response[1]
90 var _res_headers = JSON.stringify(response[2])
91 var data: String = PackedByteArray(response[3]).get_string_from_utf8()
92 var gr = GroqResult.from_data(data)
93 return gr.choices[0]
94
95# connect those
96func _on_btn_set_pressed() -> void:
97 %prompt.text_set.emit()
98func _on_prompt_text_set() -> void:
99 if !%prompt.text: return
100 var ch := await groq_completions(%prompt.text)
101 %lb_md.text = ch.content