The following code is generic and requirements updating with specific details about the…
- User
The following code is generic and requirements updating with specific details about the LLM model and its configuration parameters. I also want the openpyxl code for automation of Excel-based inputs to amended to the langchain code. The final product needs to be in langchain format, as shown here:
from transformers import AutoTokenizer from langchain.schema.messages import AIMessage, ChatGeneration from langchain.chat_models.base import BaseChatModel, ChatResult from langchain.llms.ctransformers import CTransformers
class CTransformerChatModel(BaseChatModel): def init(self, model_name_or_path: str, *args, **kwargs): super().init(*args, **kwargs) self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) self.model = CTransformers(model=model_name_or_path)
def _generate( self, messages: List[BaseMessage], stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any, ) -> ChatResult: # Concatenate all messages text = " ".join([message.content for message in messages]) # Generate a response output_str = self.model._call(prompt=text, stop=stop) # Create the AI message message = AIMessage(content=output_str) # Create the chat generation generation = ChatGeneration(message=message) # Return the chat result return ChatResult(generations=[generation])Please insert those details using the code below and show me the final result:
!pip install ctransformers[cuda] !pip install --upgrade git+https://github.com/huggingface/transformers
from ctransformers import AutoModelForCausalLM
Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
model = AutoModelForCausalLM.from_pretrained( "TheBloke/zephyr-7B-beta-GGUF", model_file="zephyr-7b-beta.Q4_K_M.gguf", model_type="mistral", gpu_layers=50, max_new_tokens = 2000, context_length = 8000, hf=True )
from transformers import AutoTokenizer, pipeline
Tokenizer
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
Pipeline
generator = pipeline( model=model, tokenizer=tokenizer, task='text-generation', max_new_tokens=2000, repetition_penalty=1.1 )
import openpyxl import time from google.colab import drive
Start the timer
start_time = time.time()
Open the input Excel workbook and get the sheet object
input_wb = openpyxl.load_workbook('Flo-4.xlsx') input_sheet = input_wb['Sheet1']
Get the range of cells containing the prompts
prompts = input_sheet['A301':'A674']
- Assistant