Daxtra commited on
Commit
6ab9166
1 Parent(s): c2418d9

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +89 -0
handler.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from vllm import LLM, SamplingParams
2
+ from transformers import AutoTokenizer
3
+
4
+ max_seq_length = 2048
5
+ dtype = None
6
+ load_in_4bit = True
7
+
8
+ instruction_message = """You are tasked with extracting relevant information from a given text representing a candidate profile or job requirement. Your goal is to insert specific tags around relevant pieces of information without altering the original text's formatting, punctuation, or word order.
9
+ **Tags to use:**
10
+ - `<jobtitle>`: Job title of the candidate or job requirement.
11
+ - `<skill>`: Specific skills or competencies mentioned.
12
+ - `<minyear>`: Minimum number of years of experience.
13
+ - `<maxyear>`: Maximum number of years of experience.
14
+ - `<language>`: Languages spoken or required.
15
+ - `<location>`: Geographical location related to the candidate or job.
16
+ - `<degree>`: Academic qualifications or degrees.
17
+ - `<certification>`: Professional certifications or accreditations.
18
+ - `<institution_education>`: Names of educational institutions related to the degree.
19
+ - `<institution_company>`: Names of companies related to employment history.
20
+ - `<proximity>`: Distance or location-related preferences (e.g., "within 30 miles").
21
+ - `<industry>`: Specific industry or sector experience.
22
+ **Guidelines:**
23
+ 1. **Preserve Original Text**: Do not change the original text's formatting, punctuation, or word order. The output should be identical to the input except for the addition of tags.
24
+ 2. **Tagging**: Enclose each relevant piece of information with the appropriate tag. Do not include commas or other punctuation inside the tags unless they are part of the tagged item.
25
+ 3. **Experience Years**: Use `<minyear>` as default if only a single year is given, relating to the experience.
26
+ 4. **IMPORTANT** You **must not** include any tag that is not in the provided list. You can only tag using the ones provided.
27
+ Your role is to accurately tag the text while preserving its original appearance.
28
+ """
29
+
30
+ conversation_history = [
31
+ {
32
+ "role": "system",
33
+ "content": f"{instruction_message}"
34
+ },
35
+ {
36
+ "role": "user",
37
+ "content": "Financial Analysts located within 50 miles of London, with skills in planning, budgeting, and a Master's Degree in Finance. Previous employment at Goldman Sachs or degree from MIT is a bonus."
38
+ },
39
+ {
40
+ "role": "assistant",
41
+ "content": "<jobtitle>Financial Analysts</jobtitle> located within <proximity>50 miles</proximity> of <location>London</location>, with skills in <skill>planning</skill>, <skill>budgeting</skill>, and a <degree>Master's Degree in Finance</degree>. Previous employment at <institution_company>Goldman Sachs</institution_company> or degree from <institution_education>MIT</institution_education> is a bonus."
42
+ }
43
+ ]
44
+
45
+ class EndpointHandler:
46
+
47
+ def __init__(self, path=""):
48
+ """
49
+ Initializes the EndpointHandler with a specified model and tokenizer.
50
+
51
+ Args:
52
+ path (str): The local path or identifier for the model to load.
53
+ This path should contain both the model and tokenizer files.
54
+ """
55
+ self.llm = LLM(model=path, max_model_len=2048, quantization='awq', gpu_memory_utilization=0.8)
56
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
57
+
58
+
59
+ def __call__(self, data) -> str:
60
+ """
61
+ Processes the input data by generating a formatted conversation history string
62
+ and passing it to the language model for generation.
63
+
64
+ Args:
65
+ data (dict): A dictionary containing the input data with the key `inputs`,
66
+ which is a list representing a conversation history.
67
+ Each conversation history item should be a dictionary with 'role'
68
+ (e.g., "assistant" or "user") and 'content' (the message text).
69
+
70
+ Returns:
71
+ str: The generated output from the model after processing the conversation history.
72
+ """
73
+
74
+ # Get inputs and preprocess
75
+ user_string = data.pop("user_string")
76
+ user_example = {"role": "user", "content": user_string}
77
+
78
+ conversation_input = conversation_history.copy()
79
+ conversation_input.append(user_example)
80
+ model_input = self.tokenizer.apply_chat_template(conversation_input, tokenize=False, add_generation_prompt=True)
81
+
82
+ # Set sampling parameters
83
+ sampling_params = SamplingParams(temperature=0.1, min_p=0.6, max_tokens=1024)
84
+
85
+ # Generate output
86
+ output = self.llm.generate(model_input, sampling_params)
87
+ generated_text = output[0].outputs[0].text
88
+
89
+ return generated_text