Untitled
!pip install langchain from langchain import PromptTemplate, LLMChain from langchain.llms import CTransformers from collections import deque from ctransformers import AutoModelForCausalLM #Configuring the AI's "Stats" config = { 'max_new_tokens': 1024, #Important aspect of how much AI can handle, 1 token = about 4 char 'context_length': 1024, 'temperature': 0, #Neglectable (Even idk what temp really is for smh) 'gpu_layers': 64 } model = CTransformers(model='TheBloke/Mistral-7B-Instruct-v0.1-GGUF',model_path="mistral-7b-instruct-v0.1.Q4_K_M.gguf", config=config) #Take model from package (very hip and cool) llm = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GGUF", model_file="mistral-7b-instruct-v0.1.Q4_K_M.gguf", model_type="mistral", gpu_layers=50) #History context for the AI to consume class chat_instance: def __init__(self, template, history=None): self.template = template self.history = deque([], maxlen=5) # Determines the amount of history the model will remember in the span of the chat with user def query_model(self, user_prompt): if len(self.history) == 0: prompt = PromptTemplate(template=template + "Answer the question below:\n{question} [/INST] </s>", input_variables = ["question"]) llm_chain = LLMChain(prompt = prompt, llm=model) response = llm_chain.run({"question": user_prompt}) else: prompt = PromptTemplate(template = template + "Answer the question below, with the chat history as context:\n{context}\n{question}[/INST]</s>") llm_chain = LLMChain(prompt = prompt, llm=model) response = llm_chain.run({"question": user_prompt, "context": "".join(list(self.history))}) if len(self.history) == self.history.maxlen: self.history.popleft() self.history.append(f"User: {user_prompt}\nYou: {response}\n") return response template = """<s>[INST] Act as an optometrist who is currently teaching their student on how to do history taking. Provide concise answers for basic concepts or subtopics relating to the subject, the answers have to give some examples. Elaborate in extreme detail when discussing advanced topics. Don't address incorrect outputs by apologizing, just provide the correct response. Emphasize the importance of factual accuracy; if you cannot provide an accurate answer with high confidence, you state this to the user, rather than risk providing incorrect information. Also note that if an answer is already provided by the user, that it is already fact-checked and you simply need to reach it. You are NOT to provide a more 'correct' answer to the one provided by the user, you only need to respond with the methodology and workings to reach it. You will not condone any acts of violence or hatred, and will avoid answering such questions. Furthermore, you will not assist anyone in the manufacturing of illegal or morally questionable things, such as bombs, drugs or chemical weapons.[/INST] """ chat = chat_instance(template) #Answer the question below: Non context/history response setting #{question} [/INST] </s> #<s>[INST] {prompt} [/INST]
Leave a Comment