Spaces:
Build error
Build error
Update llm_models.py
Browse files- llm_models.py +77 -77
llm_models.py
CHANGED
|
@@ -1,78 +1,78 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
-
from langchain.llms.base import LLM
|
| 4 |
-
from typing import Optional, List
|
| 5 |
-
import os
|
| 6 |
-
from execution_programs import *
|
| 7 |
-
|
| 8 |
-
# API key for llm model
|
| 9 |
-
GROQ_API_KEY = os.environ.get("GROQ_TOKENS")
|
| 10 |
-
class GroqLLM(LLM):
|
| 11 |
-
model: str = "
|
| 12 |
-
temperature: float = 0.3
|
| 13 |
-
api_key: str = GROQ_API_KEY
|
| 14 |
-
|
| 15 |
-
@property
|
| 16 |
-
def _llm_type(self) -> str:
|
| 17 |
-
return "groq-llm"
|
| 18 |
-
|
| 19 |
-
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 20 |
-
headers = {
|
| 21 |
-
"Authorization": f"Bearer {self.api_key}",
|
| 22 |
-
"Content-Type": "application/json",
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
payload = {
|
| 26 |
-
"messages": [{"role": "user", "content": prompt}],
|
| 27 |
-
"model": self.model,
|
| 28 |
-
"temperature": self.temperature,
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
try:
|
| 32 |
-
response = requests.post(
|
| 33 |
-
"https://api.groq.com/openai/v1/chat/completions",
|
| 34 |
-
headers=headers,
|
| 35 |
-
json=payload
|
| 36 |
-
)
|
| 37 |
-
response.raise_for_status()
|
| 38 |
-
return response.json()["choices"][0]["message"]["content"]
|
| 39 |
-
except Exception as e:
|
| 40 |
-
st.error(f"Error calling Groq API: {str(e)}")
|
| 41 |
-
return "Sorry, there was an error processing your request."
|
| 42 |
-
|
| 43 |
-
class GroqTextLLM(LLM):
|
| 44 |
-
model: str = "deepseek-r1-distill-llama-70b"
|
| 45 |
-
temperature: float = 0.7
|
| 46 |
-
api_key: str = GROQ_API_KEY
|
| 47 |
-
|
| 48 |
-
@property
|
| 49 |
-
def _llm_type(self) -> str:
|
| 50 |
-
return "groq-text-llm"
|
| 51 |
-
|
| 52 |
-
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 53 |
-
headers = {
|
| 54 |
-
"Authorization": f"Bearer {self.api_key}",
|
| 55 |
-
"Content-Type": "application/json",
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
payload = {
|
| 59 |
-
"messages": [{"role": "user", "content": prompt}],
|
| 60 |
-
"model": self.model,
|
| 61 |
-
"temperature": self.temperature,
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
try:
|
| 65 |
-
response = requests.post(
|
| 66 |
-
"https://api.groq.com/openai/v1/chat/completions",
|
| 67 |
-
headers=headers,
|
| 68 |
-
json=payload
|
| 69 |
-
)
|
| 70 |
-
response.raise_for_status()
|
| 71 |
-
return response.json()["choices"][0]["message"]["content"]
|
| 72 |
-
except Exception as e:
|
| 73 |
-
st.error(f"Error calling Groq API: {str(e)}")
|
| 74 |
-
return "Sorry, there was an error processing your request."
|
| 75 |
-
|
| 76 |
-
# Initialize LLMs
|
| 77 |
-
code_llm = GroqLLM()
|
| 78 |
text_llm = GroqTextLLM()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from langchain.llms.base import LLM
|
| 4 |
+
from typing import Optional, List
|
| 5 |
+
import os
|
| 6 |
+
from execution_programs import *
|
| 7 |
+
|
| 8 |
+
# API key for llm model
|
| 9 |
+
GROQ_API_KEY = os.environ.get("GROQ_TOKENS")
|
| 10 |
+
class GroqLLM(LLM):
|
| 11 |
+
model: str = "llama-3.3-70b-versatile"
|
| 12 |
+
temperature: float = 0.3
|
| 13 |
+
api_key: str = GROQ_API_KEY
|
| 14 |
+
|
| 15 |
+
@property
|
| 16 |
+
def _llm_type(self) -> str:
|
| 17 |
+
return "groq-llm"
|
| 18 |
+
|
| 19 |
+
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 20 |
+
headers = {
|
| 21 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 22 |
+
"Content-Type": "application/json",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
payload = {
|
| 26 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 27 |
+
"model": self.model,
|
| 28 |
+
"temperature": self.temperature,
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(
|
| 33 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
| 34 |
+
headers=headers,
|
| 35 |
+
json=payload
|
| 36 |
+
)
|
| 37 |
+
response.raise_for_status()
|
| 38 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"Error calling Groq API: {str(e)}")
|
| 41 |
+
return "Sorry, there was an error processing your request."
|
| 42 |
+
|
| 43 |
+
class GroqTextLLM(LLM):
|
| 44 |
+
model: str = "deepseek-r1-distill-llama-70b"
|
| 45 |
+
temperature: float = 0.7
|
| 46 |
+
api_key: str = GROQ_API_KEY
|
| 47 |
+
|
| 48 |
+
@property
|
| 49 |
+
def _llm_type(self) -> str:
|
| 50 |
+
return "groq-text-llm"
|
| 51 |
+
|
| 52 |
+
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 53 |
+
headers = {
|
| 54 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 55 |
+
"Content-Type": "application/json",
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
payload = {
|
| 59 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 60 |
+
"model": self.model,
|
| 61 |
+
"temperature": self.temperature,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
response = requests.post(
|
| 66 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
| 67 |
+
headers=headers,
|
| 68 |
+
json=payload
|
| 69 |
+
)
|
| 70 |
+
response.raise_for_status()
|
| 71 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"Error calling Groq API: {str(e)}")
|
| 74 |
+
return "Sorry, there was an error processing your request."
|
| 75 |
+
|
| 76 |
+
# Initialize LLMs
|
| 77 |
+
code_llm = GroqLLM()
|
| 78 |
text_llm = GroqTextLLM()
|