-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
59 lines (47 loc) · 1.81 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from dotenv import load_dotenv
import chainlit as cl
from langchain import HuggingFaceHub, PromptTemplate, LLMChain
from getpass import getpass
from huggingface_hub import login
from langchain.prompts import ChatPromptTemplate
from langchain_core.messages import HumanMessage, SystemMessage
#loading the API key
load_dotenv(override=True)
model_id = "meta-llama/Meta-Llama-3-8B-Instruct" # "meta-llama/Meta-Llama-3-8B-Instruct" , "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "openai-community/gpt2-medium", "google/flan-t5-base"
conv_model = HuggingFaceHub(huggingfacehub_api_token=
os.environ['HUGGINGFACEHUB_API_TOKEN'],
repo_id=model_id,
model_kwargs={"temperature":0.8,
"max_new_tokens":250,
"max_length": 64})
# template = """
# Answer in a very concise manner
# {question}
# """
# prompt = PromptTemplate(template=template, input_variables=['question'])
prompt = ChatPromptTemplate.from_messages(
[
("system",
"""
Answer very shortly and to the point.
The answer should not exceed more than a few lines.
Answer only 1 question.
"""
),
("user", "{question}\n"),
]
)
@cl.on_chat_start
def main():
conv_chain = LLMChain(llm=conv_model,
prompt=prompt,
verbose=False)
cl.user_session.set("llm_chain", conv_chain)
@cl.on_message
async def main(message:str):
llm_chain = cl.user_session.get("llm_chain")
res = await llm_chain.acall(message.content, callbacks=[cl.AsyncLangchainCallbackHandler()])
#perform post processing on the received response here
#res is a dict and the response text is stored under the key "text"
await cl.Message(content=res["text"]).send()