Upload handler.py
Browse files- handler.py +24 -7
handler.py
CHANGED
|
@@ -79,13 +79,14 @@ def generate(
|
|
| 79 |
top_p: float = 0.9,
|
| 80 |
top_k: int = 50,
|
| 81 |
repetition_penalty: float = 1.2,
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
# Check if the system_prompt is provided, else construct it from instruction, conclusions, and context
|
| 85 |
if system_prompt is None and instruction is not None and conclusions is not None and context is not None:
|
| 86 |
system_prompt = "Instruction: {}\nConclusions:\n".format(instruction)
|
| 87 |
for idx, (conclusion, conclusion_key) in enumerate(conclusions):
|
| 88 |
-
system_prompt += "{}: {}\n".format(
|
| 89 |
system_prompt += "\nContext:\n"
|
| 90 |
for idx, ctx in enumerate(context):
|
| 91 |
system_prompt += "{}: [{}]\n".format(ctx, idx + 1)
|
|
@@ -126,8 +127,24 @@ def generate(
|
|
| 126 |
outputs = []
|
| 127 |
for text in streamer:
|
| 128 |
outputs.append(text)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
top_p: float = 0.9,
|
| 80 |
top_k: int = 50,
|
| 81 |
repetition_penalty: float = 1.2,
|
| 82 |
+
end_sequences: list[str] = ["[INST]", "[/INST]", "\n"]
|
| 83 |
+
) -> dict:
|
| 84 |
|
| 85 |
# Check if the system_prompt is provided, else construct it from instruction, conclusions, and context
|
| 86 |
if system_prompt is None and instruction is not None and conclusions is not None and context is not None:
|
| 87 |
system_prompt = "Instruction: {}\nConclusions:\n".format(instruction)
|
| 88 |
for idx, (conclusion, conclusion_key) in enumerate(conclusions):
|
| 89 |
+
system_prompt += "{}: {}\n".format(conclusion, conclusion_key)
|
| 90 |
system_prompt += "\nContext:\n"
|
| 91 |
for idx, ctx in enumerate(context):
|
| 92 |
system_prompt += "{}: [{}]\n".format(ctx, idx + 1)
|
|
|
|
| 127 |
outputs = []
|
| 128 |
for text in streamer:
|
| 129 |
outputs.append(text)
|
| 130 |
+
generated_text = "".join(outputs)
|
| 131 |
+
conclusion_found = None
|
| 132 |
+
context_numbers = []
|
| 133 |
+
|
| 134 |
+
# Check for conclusion keys in the generated text
|
| 135 |
+
if conclusions:
|
| 136 |
+
for conclusion_key, _ in conclusions:
|
| 137 |
+
if conclusion_key in generated_text:
|
| 138 |
+
conclusion_found = conclusion_key
|
| 139 |
+
break
|
| 140 |
+
|
| 141 |
+
# Extract context numbers from the generated text
|
| 142 |
+
context_pattern = r"\[\d+\]"
|
| 143 |
+
context_matches = re.findall(context_pattern, generated_text)
|
| 144 |
+
context_numbers = [int(match.strip("[]")) for match in context_matches]
|
| 145 |
+
|
| 146 |
+
return {
|
| 147 |
+
"generated_text": generated_text.strip(),
|
| 148 |
+
"conclusion": conclusion_found,
|
| 149 |
+
"context": context_numbers
|
| 150 |
+
}
|