-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_groq_fullswitch.py
More file actions
86 lines (77 loc) · 3.08 KB
/
Copy pathpatch_groq_fullswitch.py
File metadata and controls
86 lines (77 loc) · 3.08 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Completes the switch to Groq llama-3.3-70b.
# Fixes API URL, headers, key variable, and response parsing
# which are still pointing to Anthropic after Session 5.
import sys
from pathlib import Path
APP = Path(r"C:\Users\Ezeking\hf_space\app.py")
def patch(old, new, label):
content = APP.read_text(encoding="utf-8")
count = content.count(old)
if count == 0:
print(f"ERROR [{label}]: string not found"); sys.exit(1)
if count > 1:
print(f"ERROR [{label}]: found {count} times"); sys.exit(1)
APP.write_text(content.replace(old, new, 1), encoding="utf-8")
print(f"OK [{label}]")
# Fix 1: API constants back to Groq
patch(
'ANTHROPIC_API_URL: str = "https://api.anthropic.com/v1/messages"\n'
'ANTHROPIC_MODEL: str = "llama-3.3-70b-versatile"',
'GROQ_API_URL: str = "https://api.groq.com/openai/v1/chat/completions"\n'
'GROQ_MODEL: str = "llama-3.3-70b-versatile"',
"Fix 1 API constants"
)
# Fix 2: Key variable back to Groq
patch(
' anthropic_key = "".join(os.environ.get("ANTHROPIC_API_KEY", "").split())\n'
' if not anthropic_key:\n'
' return "ANTHROPIC_API_KEY not set in Space secrets."',
' groq_key = "".join(os.environ.get("GROQ_API_KEY", "").split())\n'
' if not groq_key:\n'
' return "GROQ_API_KEY not set in Space secrets."',
"Fix 2 key variable"
)
# Fix 3: API call back to Groq/OpenAI format
patch(
' response = requests.post(\n'
' ANTHROPIC_API_URL,\n'
' headers={\n'
' "x-api-key": anthropic_key,\n'
' "anthropic-version": "2023-06-01",\n'
' "content-type": "application/json",\n'
' },\n'
' json={\n'
' "model": ANTHROPIC_MODEL,\n'
' "max_tokens": 1500,\n'
' "temperature": 0.1,\n'
' "system": SYSTEM_PROMPT,\n'
' "messages": [\n'
' {"role": "user", "content": user_message},\n'
' ],\n'
' },\n'
' timeout=60,\n'
' )\n'
' response.raise_for_status()\n'
' return response.json()["content"][0]["text"]',
' response = requests.post(\n'
' GROQ_API_URL,\n'
' headers={\n'
' "Authorization": f"Bearer {groq_key}",\n'
' "Content-Type": "application/json",\n'
' },\n'
' json={\n'
' "model": GROQ_MODEL,\n'
' "messages": [\n'
' {"role": "system", "content": SYSTEM_PROMPT},\n'
' {"role": "user", "content": user_message},\n'
' ],\n'
' "max_tokens": 1500,\n'
' "temperature": 0.1,\n'
' },\n'
' timeout=60,\n'
' )\n'
' response.raise_for_status()\n'
' return response.json()["choices"][0]["message"]["content"]',
"Fix 3 API call format"
)
print("\nGroq llama-3.3-70b fully wired.")