|
a |
|
b/src/utils.py |
|
|
1 |
import requests |
|
|
2 |
import os |
|
|
3 |
from typing import Optional |
|
|
4 |
|
|
|
5 |
HUGGING_FACE_TOKEN = os.environ["HUGGING_FACE_TOKEN"] |
|
|
6 |
MODEL_API_URL = ( |
|
|
7 |
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1" |
|
|
8 |
) |
|
|
9 |
|
|
|
10 |
headers = {"Authorization": f"Bearer {HUGGING_FACE_TOKEN}"} |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
def query(payload: Optional[dict] = None, inputs: Optional[str] = None): |
|
|
14 |
if payload is None: |
|
|
15 |
payload = { |
|
|
16 |
"inputs": inputs, |
|
|
17 |
"parameters": { |
|
|
18 |
"max_new_tokens": 240, |
|
|
19 |
"return_full_text": False, |
|
|
20 |
}, |
|
|
21 |
"options": {"use_cache": True, "wait_for_model": True}, |
|
|
22 |
} |
|
|
23 |
response = requests.post(MODEL_API_URL, headers=headers, json=payload) |
|
|
24 |
return response.json()[0]["generated_text"] |