Update chatcode/runtime.py
Browse files- chatcode/runtime.py +15 -4
chatcode/runtime.py
CHANGED
|
@@ -61,8 +61,6 @@ def load_engine(adapter: str | None = None):
|
|
| 61 |
|
| 62 |
def answer(tokenizer, model, prompt: str, history: list[dict] | None = None,
|
| 63 |
max_new_tokens: int = CONFIG["max_new_tokens"]) -> str:
|
| 64 |
-
import torch
|
| 65 |
-
|
| 66 |
if not 1 <= max_new_tokens <= 2048:
|
| 67 |
raise ValueError("Response length must be between 1 and 2,048 tokens.")
|
| 68 |
messages = conversation(prompt, history)
|
|
@@ -75,11 +73,24 @@ def answer(tokenizer, model, prompt: str, history: list[dict] | None = None,
|
|
| 75 |
if len(messages) <= 2:
|
| 76 |
raise ValueError("This message is too long. Please send a smaller code example.")
|
| 77 |
del messages[1:3]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
inputs = inputs.to(model.device)
|
|
|
|
| 79 |
with torch.inference_mode():
|
| 80 |
output = model.generate(
|
| 81 |
-
**inputs, max_new_tokens=max_new_tokens, do_sample=
|
| 82 |
-
temperature=0.7, top_p=0.8, top_k=20,
|
| 83 |
pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.eos_token_id)
|
| 84 |
reply = tokenizer.decode(output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 85 |
if not reply:
|
|
|
|
| 61 |
|
| 62 |
def answer(tokenizer, model, prompt: str, history: list[dict] | None = None,
|
| 63 |
max_new_tokens: int = CONFIG["max_new_tokens"]) -> str:
|
|
|
|
|
|
|
| 64 |
if not 1 <= max_new_tokens <= 2048:
|
| 65 |
raise ValueError("Response length must be between 1 and 2,048 tokens.")
|
| 66 |
messages = conversation(prompt, history)
|
|
|
|
| 73 |
if len(messages) <= 2:
|
| 74 |
raise ValueError("This message is too long. Please send a smaller code example.")
|
| 75 |
del messages[1:3]
|
| 76 |
+
return generate_messages(tokenizer, model, messages, max_new_tokens=max_new_tokens)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def generate_messages(tokenizer, model, messages, max_new_tokens=768,
|
| 80 |
+
context_limit=4096, do_sample=True):
|
| 81 |
+
import torch
|
| 82 |
+
if not 1 <= max_new_tokens <= 2048 or not 256 <= context_limit <= 8192:
|
| 83 |
+
raise ValueError("Invalid response or context length.")
|
| 84 |
+
inputs = tokenizer.apply_chat_template(
|
| 85 |
+
messages, tokenize=True, add_generation_prompt=True,
|
| 86 |
+
return_dict=True, return_tensors="pt")
|
| 87 |
+
if inputs["input_ids"].shape[1] + max_new_tokens > context_limit:
|
| 88 |
+
raise ValueError("Source packet is too long. Shorten the excerpts; no sources were silently removed.")
|
| 89 |
inputs = inputs.to(model.device)
|
| 90 |
+
sampling = {"temperature": 0.7, "top_p": 0.8, "top_k": 20} if do_sample else {}
|
| 91 |
with torch.inference_mode():
|
| 92 |
output = model.generate(
|
| 93 |
+
**inputs, max_new_tokens=max_new_tokens, do_sample=do_sample, **sampling,
|
|
|
|
| 94 |
pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.eos_token_id)
|
| 95 |
reply = tokenizer.decode(output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 96 |
if not reply:
|