How do I use groq in godot

Godot Version

4.3

Question

I want to use GD script to make requests to the groq api. I tried to figure it out but the JSON section on the quickstart guide didn’t provide enough info

What have you tried so far in Godot, share code if possible.

What didn’t work specifically, share any error messages you have encountered.

The quickstart guide says to use an http request with JSON:

{
  "messages": [
    {
      "role": "user",
      "content": "Explain the importance of fast language models"
    }
  ],
  "model": "mixtral-8x7b-32768"
}

I understand the JSON but I noticed that it has no parameter for an auth key. The curl example does it like this

curl -X POST "https://api.groq.com/openai/v1/chat/completions" \
     -H "Authorization: Bearer $GROQ_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"messages": [{"role": "user", "content": "Explain the importance of fast language models"}], "model": "llama3-8b-8192"}'

I don’t know how to translate this to a godot http request.

Did you follow examples here? Share what you’ve tried so far.

I tried it with groq’s example JSON. It complained about an invalid auth key

Look at the curl request, it is passing “Authorization: Bearer $GROQ_API_KEY” as a custom header where “$GROQ_API_KEY” needs to be replaced by your api key.

I’m not at my computer, but looking at the docs it’s probably something like:

var url = “https://api.groq.com/openai/v1/chat/completions
var MyAPI = “Your API Key”
var json=‘{“messages”: [{“role”: “user”, “content”: “Explain the importance of fast language models”}], “model”: “llama3-8b-8192”}’

var headers = [
“Content-Type: application/json”,
"Authorization: Bearer " + MyAPI,
]
$HTTPRequest.request(url, headers, HTTPClient.METHOD_POST, json)

YMMV with if the JSON.stringify is required and presumably requires a child node named $HTTPRequest for this example.

1 Like

I’ll try that tomorrow morning

Based on your suggestions I wrote this code

extends Node2D

func _ready():
	var url = "https://api.groq.com/openai/v1/chat/completions"
	var MyAPI = "redacted"
	var json = '{“messages”: [{“role”: “user”, “content”: “Explain the importance of fast language models”}], “model”: “llama3-8b-8192”}'
	var headers = [
	"Content-Type: application/json",
	"Authorization: Bearer " + MyAPI,
	]
	$HTTPRequest.request(url, headers, HTTPClient.METHOD_POST, json)

func _on_request_completed(result, response_code, headers, body):
	var parse = JSON.parse_string(body.get_string_from_utf8())
	print(parse)
	pass

However, when I run it, it prints this:

{ "error": { "message": "failed to unmarshal JSON: invalid character \'â\' looking for beginning of object key string", "type": "invalid_request_error" } }