-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13a61cb
commit f15e838
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import chainlit as cl | ||
from langchain.callbacks.base import BaseCallbackHandler | ||
from langchain.chains import LLMChain | ||
from langchain.memory import ConversationBufferMemory | ||
from langchain_community.llms import CTransformers | ||
from langchain_core.prompts import PromptTemplate | ||
|
||
|
||
class StreamHandler(BaseCallbackHandler): | ||
def __init__(self): | ||
self.msg = cl.Message(content="") | ||
|
||
async def on_llm_new_token(self, token: str, **kwargs): | ||
await self.msg.stream_token(token) | ||
|
||
async def on_llm_end(self, response: str, **kwargs): | ||
await self.msg.send() | ||
self.msg = cl.Message(content="") | ||
|
||
|
||
# Load quantized Llama 2 | ||
llm = CTransformers( | ||
model="TheBloke/Llama-2-7B-Chat-GGUF", | ||
model_file="llama-2-7b-chat.Q2_K.gguf", | ||
model_type="llama2", | ||
max_new_tokens=20, | ||
) | ||
|
||
template = """ | ||
[INST] <<SYS>> | ||
You are a helpful, respectful and honest assistant. | ||
Always provide a concise answer and use the following Context: | ||
{context} | ||
<</SYS>> | ||
User: | ||
{instruction}[/INST]""" | ||
|
||
prompt = PromptTemplate(template=template, input_variables=["context", "instruction"]) | ||
|
||
|
||
@cl.on_chat_start | ||
def on_chat_start(): | ||
memory = ConversationBufferMemory(memory_key="context") | ||
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=False, memory=memory) | ||
cl.user_session.set("llm_chain", llm_chain) | ||
|
||
|
||
@cl.on_message | ||
async def on_message(message: cl.Message): | ||
llm_chain = cl.user_session.get("llm_chain") | ||
|
||
await llm_chain.ainvoke( | ||
message.content, | ||
config={"callbacks": [cl.AsyncLangchainCallbackHandler(), StreamHandler()]}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from langchain.chains import LLMChain | ||
from langchain.memory import ConversationBufferMemory | ||
from langchain_community.llms import CTransformers | ||
from langchain_core.prompts import PromptTemplate | ||
|
||
llm = CTransformers( | ||
model="zoltanctoth/orca_mini_3B-GGUF", | ||
model_file="orca-mini-3b.q4_0.gguf", | ||
model_type="llama2", | ||
max_new_tokens=20, | ||
) | ||
|
||
prompt_template = """### System:\nYou are an AI assistant that gives helpful answers. | ||
You answer the question in a short and concise way. Take this context into account when answering the question: {context}\n | ||
\n\n### User:\n{instruction}\n\n### Response:\n""" | ||
|
||
prompt = PromptTemplate(template=prompt_template, input_variables=["instruction"]) | ||
memory = ConversationBufferMemory(memory_key="context") | ||
|
||
chain = LLMChain(llm=llm, prompt=prompt, verbose=True, memory=memory) | ||
|
||
print(chain.invoke({"instruction": "Which city is the capital of India?"})) | ||
print( | ||
chain.invoke({"instruction": "Which city is has the same functionality in the US?"}) | ||
) |