Spaces:
Building
Building
Update generator.py
Browse files- generator.py +15 -11
generator.py
CHANGED
|
@@ -1,26 +1,30 @@
|
|
| 1 |
import os
|
| 2 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 3 |
-
|
| 4 |
-
# Load the API token from environment variable
|
| 5 |
-
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 6 |
|
| 7 |
def load_llm(repo_id="mistralai/Mistral-7B-Instruct-v0.2"):
|
| 8 |
'''
|
| 9 |
Load the LLM from the HuggingFace model hub
|
| 10 |
-
|
| 11 |
Args:
|
| 12 |
repo_id (str): The HuggingFace model ID
|
| 13 |
-
|
| 14 |
Returns:
|
| 15 |
llm (HuggingFaceEndpoint): The LLM model
|
| 16 |
'''
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def guardrails():
|
| 26 |
return None
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 3 |
+
from pydantic import ValidationError
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def load_llm(repo_id="mistralai/Mistral-7B-Instruct-v0.2"):
|
| 6 |
'''
|
| 7 |
Load the LLM from the HuggingFace model hub
|
|
|
|
| 8 |
Args:
|
| 9 |
repo_id (str): The HuggingFace model ID
|
|
|
|
| 10 |
Returns:
|
| 11 |
llm (HuggingFaceEndpoint): The LLM model
|
| 12 |
'''
|
| 13 |
|
| 14 |
+
try:
|
| 15 |
+
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 16 |
+
llm = HuggingFaceEndpoint(
|
| 17 |
+
repo_id=repo_id, max_length=128, temperature=0.2, api_token=api_token)
|
| 18 |
+
return llm
|
| 19 |
+
except ValidationError as e:
|
| 20 |
+
print("Validation Error:", e)
|
| 21 |
+
# Log or handle the validation error appropriately
|
| 22 |
+
return None
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print("Error:", e)
|
| 25 |
+
# Log or handle other exceptions
|
| 26 |
+
return None
|
| 27 |
|
| 28 |
def guardrails():
|
| 29 |
return None
|
| 30 |
+
|