File size: 802 Bytes
8099938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afec314
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain 
from langchain.llms import HuggingFaceHub
from .config import config 
from prompts.prompt import prompts

class LLM_chain:
    def __init__(self):
        self.llm = HuggingFaceHub(
            repo_id=config["model"], 
            model_kwargs={"temperature": config["temperature"], "max_new_tokens": config["max_new_tokens"], "top_k": config["top_k"], "load_in_8bit": config["load_in_8bit"]})

    def __call__(self, entity: str, id: int = 0):
        template = prompts[id]["prompt_template"]
        prompt = PromptTemplate(template=template, input_variables=["entity"])
        llm_chain = LLMChain(prompt=prompt, llm=self.llm, verbose=True)
        output = llm_chain.invoke(entity)
        return output["text"]