Blob


1 #!/usr/bin/env python
3 import json
4 import os
5 import sys
6 import urllib.request
8 url = "https://api.groq.com/openai/v1/chat/completions"
9 # small models include:
10 # llama-3.1-8b-instant
11 # llama-3.2-3b-preview
12 # llama-3.2-1b-preview
13 model = "llama-3.1-8b-instant"
14 big = "llama-3.3-70b-versatile"
16 def read_token(name):
17 with open(name) as f:
18 return f.read().strip()
20 tpath = os.path.join(os.getenv("HOME"), ".config/groq/token")
21 token = read_token(tpath)
23 if len(sys.argv) > 1 and sys.argv[1] == "-b":
24 model = big
25 prompt = sys.stdin.read()
26 message = {"messages": [{"role": "user","content": prompt}], "model": model}
28 req = urllib.request.Request(url, json.dumps(message).encode())
29 req.add_header("Content-Type", "application/json")
30 req.add_header("Authorization", "Bearer "+token)
31 # groq blocks urllib's user agent
32 req.add_header("User-Agent", "curl/8.9.0")
34 with urllib.request.urlopen(req) as resp:
35 reply = json.load(resp)
36 print(reply["choices"][0]["message"]["content"])