raghavNCI
commited on
Commit
Β·
e30a3df
1
Parent(s):
f0f712f
mistral connection with inference
Browse files
models_initialization/mistral_registry.py
CHANGED
@@ -1,29 +1,19 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
-
import
|
4 |
-
from
|
5 |
-
from botocore.exceptions import BotoCoreError, ClientError
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
10 |
-
# AWS_ACCESS_KEY_ID
|
11 |
-
# AWS_SECRET_ACCESS_KEY
|
12 |
-
# AWS_REGION β e.g. "us-east-1"
|
13 |
-
# SAGEMAKER_ENDPOINT_NAME β e.g. "mistral-endpoint"
|
14 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
15 |
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
|
26 |
-
sm_client = boto3.client("sagemaker-runtime", region_name=AWS_REGION, config=boto_cfg)
|
27 |
|
28 |
|
29 |
def mistral_generate(
|
@@ -32,8 +22,8 @@ def mistral_generate(
|
|
32 |
temperature: float = 0.7,
|
33 |
) -> str:
|
34 |
"""
|
35 |
-
Call the
|
36 |
-
Returns the generated text or an empty string on failure.
|
37 |
"""
|
38 |
payload = {
|
39 |
"inputs": prompt,
|
@@ -44,22 +34,26 @@ def mistral_generate(
|
|
44 |
}
|
45 |
|
46 |
try:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
)
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
if isinstance(
|
58 |
-
return
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
63 |
|
64 |
except Exception as e:
|
65 |
print("β Unknown error:", str(e))
|
|
|
1 |
import os
|
2 |
import json
|
3 |
+
import requests
|
4 |
+
from requests.exceptions import RequestException
|
|
|
5 |
|
6 |
+
HF_ENDPOINT_URL = os.getenv("HF_ENDPOINT_URL")
|
7 |
+
HF_ENDPOINT_TOKEN = os.getenv("HF_ENDPOINT_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
assert HF_ENDPOINT_URL, "β HF_ENDPOINT_URL is not set"
|
10 |
+
assert HF_ENDPOINT_TOKEN, "β HF_ENDPOINT_TOKEN is not set"
|
11 |
|
12 |
+
HEADERS = {
|
13 |
+
"Authorization": f"Bearer {HF_ENDPOINT_TOKEN}",
|
14 |
+
"Content-Type": "application/json",
|
15 |
+
"Accept": "application/json",
|
16 |
+
}
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
def mistral_generate(
|
|
|
22 |
temperature: float = 0.7,
|
23 |
) -> str:
|
24 |
"""
|
25 |
+
Call the Hugging Face Inference Endpoint that hosts Mistral-7B.
|
26 |
+
Returns the generated text, or an empty string on failure.
|
27 |
"""
|
28 |
payload = {
|
29 |
"inputs": prompt,
|
|
|
34 |
}
|
35 |
|
36 |
try:
|
37 |
+
r = requests.post(
|
38 |
+
HF_ENDPOINT_URL,
|
39 |
+
headers=HEADERS,
|
40 |
+
json=payload,
|
41 |
+
timeout=90, # HF spins up cold endpoints too
|
42 |
)
|
43 |
+
r.raise_for_status()
|
44 |
+
data = r.json()
|
45 |
+
|
46 |
+
# HF Endpoints usually return a *list* of dicts
|
47 |
+
if isinstance(data, list) and data:
|
48 |
+
return data[0].get("generated_text", "").strip()
|
49 |
+
# Some endpoints return a single dict
|
50 |
+
if isinstance(data, dict) and "generated_text" in data:
|
51 |
+
return data["generated_text"].strip()
|
52 |
+
|
53 |
+
except RequestException as e:
|
54 |
+
print("β HF Endpoint error:", str(e))
|
55 |
+
if e.response is not None:
|
56 |
+
print("Endpoint said:", e.response.text[:300])
|
57 |
|
58 |
except Exception as e:
|
59 |
print("β Unknown error:", str(e))
|