Mayank2027 commited on
Commit
95c4a1a
·
verified ·
1 Parent(s): 62b5eb1

Update backend/model_router.py

Browse files
Files changed (1) hide show
  1. backend/model_router.py +22 -20
backend/model_router.py CHANGED
@@ -1,8 +1,5 @@
1
  import os, random, aiohttp
2
  from typing import List, Dict, AsyncGenerator
3
-
4
- # Correct imports for groq 0.3.0+
5
- from groq import Client as Groq
6
  from openai import AsyncOpenAI
7
 
8
  groq_keys = os.environ.get("GROQ_API_KEYS", "").split(",")
@@ -10,25 +7,29 @@ openrouter_keys = os.environ.get("OPENROUTER_KEYS", "").split(",")
10
 
11
  POLLINATIONS_URL = "https://text.pollinations.ai/"
12
 
 
 
 
 
 
 
 
 
 
13
  async def route_chat(model: str, messages: List[Dict]) -> AsyncGenerator[str, None]:
14
  if model.startswith("groq-"):
15
- key = random.choice(groq_keys)
16
- client = Groq(api_key=key)
17
  model_id = model[5:] # strip "groq-"
18
- stream = client.chat.completions.create(
19
  messages=messages,
20
  model=model_id,
21
  stream=True,
22
  )
23
- for chunk in stream:
24
  if chunk.choices[0].delta.content:
25
  yield chunk.choices[0].delta.content
26
  elif model.startswith("openrouter-"):
27
- key = random.choice(openrouter_keys)
28
- client = AsyncOpenAI(
29
- base_url="https://openrouter.ai/api/v1",
30
- api_key=key,
31
- )
32
  model_id = model[11:] # strip "openrouter-"
33
  stream = await client.chat.completions.create(
34
  messages=messages,
@@ -49,17 +50,18 @@ async def route_chat(model: str, messages: List[Dict]) -> AsyncGenerator[str, No
49
  raise ValueError(f"Unknown model: {model}")
50
 
51
  async def route_autocomplete(model: str, prefix: str, suffix: str, max_tokens: int) -> str:
52
- key = random.choice(groq_keys)
53
- client = Groq(api_key=key)
54
- prompt = f"<fim_prefix>{prefix}<fim_suffix>{suffix}<fim_middle>"
55
- response = client.completions.create(
56
- model="codellama-70b",
57
- prompt=prompt,
 
 
58
  max_tokens=max_tokens,
59
  temperature=0.1,
60
- stop=["<fim_middle>"],
61
  )
62
- return response.choices[0].text
63
 
64
  def get_available_models():
65
  return [
 
1
  import os, random, aiohttp
2
  from typing import List, Dict, AsyncGenerator
 
 
 
3
  from openai import AsyncOpenAI
4
 
5
  groq_keys = os.environ.get("GROQ_API_KEYS", "").split(",")
 
7
 
8
  POLLINATIONS_URL = "https://text.pollinations.ai/"
9
 
10
+ # Groq provides an OpenAI-compatible API – we use AsyncOpenAI directly.
11
+ def get_groq_client():
12
+ key = random.choice(groq_keys)
13
+ return AsyncOpenAI(base_url="https://api.groq.com/openai/v1", api_key=key)
14
+
15
+ def get_openrouter_client():
16
+ key = random.choice(openrouter_keys)
17
+ return AsyncOpenAI(base_url="https://openrouter.ai/api/v1", api_key=key)
18
+
19
  async def route_chat(model: str, messages: List[Dict]) -> AsyncGenerator[str, None]:
20
  if model.startswith("groq-"):
21
+ client = get_groq_client()
 
22
  model_id = model[5:] # strip "groq-"
23
+ stream = await client.chat.completions.create(
24
  messages=messages,
25
  model=model_id,
26
  stream=True,
27
  )
28
+ async for chunk in stream:
29
  if chunk.choices[0].delta.content:
30
  yield chunk.choices[0].delta.content
31
  elif model.startswith("openrouter-"):
32
+ client = get_openrouter_client()
 
 
 
 
33
  model_id = model[11:] # strip "openrouter-"
34
  stream = await client.chat.completions.create(
35
  messages=messages,
 
50
  raise ValueError(f"Unknown model: {model}")
51
 
52
  async def route_autocomplete(model: str, prefix: str, suffix: str, max_tokens: int) -> str:
53
+ # Use Groq's chat completion for autocomplete
54
+ client = get_groq_client()
55
+ prompt = f"Complete the following code:\n```\n{prefix}\n```"
56
+ if suffix:
57
+ prompt += f"\nThe code should continue until before: {suffix}"
58
+ response = await client.chat.completions.create(
59
+ model="codellama-70b-instruct", # or any other model you prefer
60
+ messages=[{"role": "user", "content": prompt}],
61
  max_tokens=max_tokens,
62
  temperature=0.1,
 
63
  )
64
+ return response.choices[0].message.content
65
 
66
  def get_available_models():
67
  return [