File size: 1,225 Bytes
831257c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList
import torch
import json
import json
import re
import numpy as np


def create_prompt(text, template, examples):
  template = json.dumps(json.loads(template),indent = 4)

  prompt = "<|input|>\n### Template:\n"+template+"\n"

  if examples[0]:
    example1 = json.dumps(json.loads(examples[0]),indent = 4)
    prompt+= "### Example:\n"+example1+"\n"
  if examples[1]:
    example2 = json.dumps(json.loads(examples[1]),indent = 4)
    prompt+= "### Example:\n"+example1+"\n"
  if examples[2]:
    example3 = json.dumps(json.loads(examples[1]),indent = 4)
    prompt+= "### Example:\n"+example3+"\n"

  prompt += "### Text:\n"+text+'''\n<|output|>'''

  return prompt


def generate_answer_short(prompt,model, tokenizer):
    model_input = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=3000).to("cuda")
    with torch.no_grad():
        gen = tokenizer.decode(model.generate(**model_input, max_new_tokens=1500)[0], skip_special_tokens=True)
    print(gen.split("<|output|>")[1].split("<|end-output|>")[0])
    return gen.split("<|output|>")[1].split("<|end-output|>")[0]