chainlit-work / app.py
themanas021's picture
Create app.py
0a44d42
Raw
History Blame Contribute Delete
1.25 kB
from langchain import PromptTemplate, LLMChain
import chainlit as cl
from langchain import HuggingFaceHub
repo_id = "tiiuae/falcon-7b-instruct"
llm = HuggingFaceHub(
huggingfacehub_api_token='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',
repo_id=repo_id,
model_kwargs={"temperature":0.3, "max_new_tokens":500}
)
template = """Question: {question}
Answer: Let's think step by step."""
@cl.on_chat_start
def main():
# Instantiate the chain for that user session
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True)
# Store the chain in the user session
cl.user_session.set("llm_chain", llm_chain)
@cl.on_message
async def main(message: cl.Message):
# Retrieve the chain from the user session
llm_chain = cl.user_session.get("llm_chain") # type: LLMChain
# Call the chain asynchronously
res = await llm_chain.acall(message.content, callbacks=[cl.AsyncLangchainCallbackHandler()])
# Do any post processing here
# "res" is a Dict. For this chain, we get the response by reading the "text" key.
# This varies from chain to chain, you should check which key to read.
await cl.Message(content=res["text"]).send()