homen_testing_merged6 / handler.py
fletch1300's picture
Update handler.py
7fb554f
raw
history blame
3.13 kB
from typing import Any, Dict, List
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
class EndpointHandler:
def __init__(self, path=""):
self.tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
self.model = AutoModelForCausalLM.from_pretrained(
path,
return_dict=True,
device_map="auto",
load_in_8bit=True,
torch_dtype=dtype,
trust_remote_code=True,
)
generation_config = self.model.generation_config
generation_config.max_new_tokens = 200
generation_config.temperature = 0.8
generation_config.top_p = 0.8
generation_config.num_return_sequences = 1
generation_config.pad_token_id = self.tokenizer.eos_token_id
generation_config.eos_token_id = self.tokenizer.eos_token_id
generation_config.early_stopping = True
self.generate_config = generation_config
self.pipeline = transformers.pipeline(
"text-generation", model=self.model, tokenizer=self.tokenizer
)
def _ensure_token_limit(self, text):
"""Ensure text is within the model's token limit."""
tokens = self.tokenizer.tokenize(text)
if len(tokens) > 2048:
# Remove tokens from the beginning until the text fits
tokens = tokens[-2048:]
return self.tokenizer.decode(tokens)
return text
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
user_prompt = data.pop("inputs", data)
# Add the user's message to the conversation history
self.conversation_history += f"<user>: {user_prompt}\n"
# Ensure the conversation history is within token limit
self.conversation_history = self._ensure_token_limit(self.conversation_history)
# Add the permanent context, user's prompt, and conversation history
permanent_context = "<context>: You are a life coaching bot with the goal of providing guidance, improving understanding, reducing suffering and improving life. Gain as much understanding of the user before providing guidance."
structured_prompt = f"{permanent_context}\n{self.conversation_history}<bot> response:"
result = self.pipeline(structured_prompt, generation_config=self.generate_config)
# Extract only the bot's response without the structuring text
response_text = self._extract_response(result[0]['generated_text'])
# Remove the last "<bot>" from the response_text
response_text = response_text.rsplit("[END", 1)[0].strip()
# Add the bot's response to the conversation history
self.conversation_history += f"<bot>: {response_text}\n"
self.conversation_history = self._ensure_token_limit(self.conversation_history)
return [{"generated_text": response_text}]
return {"response": response_text}