Update handler.py
Browse files- handler.py +6 -31
handler.py
CHANGED
|
@@ -16,36 +16,7 @@ class EndpointHandler():
|
|
| 16 |
self.tokenizer = AutoTokenizer.from_pretrained(cfg['repo'])
|
| 17 |
|
| 18 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 19 |
-
inputs = data.pop("inputs", "")
|
| 20 |
-
temperature = data.pop("temperature", None)
|
| 21 |
-
if not temperature:
|
| 22 |
-
temperature = data.pop("temp", 0.33)
|
| 23 |
-
if temperature > 3 or temperature < 0:
|
| 24 |
-
return json.dumps({
|
| 25 |
-
"status": "error",
|
| 26 |
-
"reason": "invalid temperature ( 0.01 - 1.00 )"
|
| 27 |
-
})
|
| 28 |
-
top_p = data.pop("top-p", 0.85)
|
| 29 |
-
if top_p > 3 or top_p < 0:
|
| 30 |
-
return json.dumps({
|
| 31 |
-
"status": "error",
|
| 32 |
-
"reason": "invalid top percentage ( 0.01 - 1.00 )"
|
| 33 |
-
})
|
| 34 |
-
top_k = data.pop("top-k", 42)
|
| 35 |
-
if top_k > 100 or top_k < 0:
|
| 36 |
-
return json.dumps({
|
| 37 |
-
"status": "error",
|
| 38 |
-
"reason": "invalid top k ( 1 - 99 )"
|
| 39 |
-
})
|
| 40 |
-
system_prompt = data.pop("system-prompt", "You are a helpful assistant.")
|
| 41 |
-
fmat = data.pop("format", f"<|system|>\n{system_prompt} <|end|>\n<|user|>\n{inputs} <|end|>\n<|assistant|>")
|
| 42 |
-
try:
|
| 43 |
-
fmat = fmat.format(system_prompt = system_prompt, prompt = inputs)
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return json.dumps({
|
| 46 |
-
"status": "error",
|
| 47 |
-
"reason": "invalid format"
|
| 48 |
-
})
|
| 49 |
max_length = data.pop("max_length", 1024)
|
| 50 |
try:
|
| 51 |
max_length = int(max_length)
|
|
@@ -55,6 +26,10 @@ class EndpointHandler():
|
|
| 55 |
"reason": "max_length was passed as something that was absolutely not a plain old int"
|
| 56 |
})
|
| 57 |
|
| 58 |
-
res = self.model(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
return res
|
|
|
|
| 16 |
self.tokenizer = AutoTokenizer.from_pretrained(cfg['repo'])
|
| 17 |
|
| 18 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 19 |
+
inputs = data.pop("inputs", "Q: What is the chemical composition of common concrete in 2024?\nA: ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
max_length = data.pop("max_length", 1024)
|
| 21 |
try:
|
| 22 |
max_length = int(max_length)
|
|
|
|
| 26 |
"reason": "max_length was passed as something that was absolutely not a plain old int"
|
| 27 |
})
|
| 28 |
|
| 29 |
+
res = self.model(f"""
|
| 30 |
+
<|user|>
|
| 31 |
+
{inputs} <|end|>
|
| 32 |
+
<|assistant|>
|
| 33 |
+
""", max_new_tokens=max_new_tokens, do_sample=False)
|
| 34 |
|
| 35 |
return res
|