-
Notifications
You must be signed in to change notification settings - Fork 0
/
rag_chat.py
executable file
·73 lines (65 loc) · 2.14 KB
/
rag_chat.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# Sample code to implement a Llama2 chatbot with RAG using a Chroma vector
# database with Langchain and LlamaCpp
from langchain_community.llms import LlamaCpp
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_community.embeddings.sentence_transformer import (
SentenceTransformerEmbeddings,
)
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
from sys import argv, exit
from os import path
# Get the path to the LLM model we are using, and the path to chromadb for RAG.
if (len(argv) != 3):
print("Usage:", argv[0], "/path/to/llm /path/to/chromadb/dir")
exit(1)
llama_path = argv[1]
chromadbdir = argv[2]
chromadbdir = chromadbdir + "/chromadb"
if not path.exists(llama_path):
print("Invalid llama model path!")
exit(1)
if not path.exists(chromadbdir):
print("Invalid chroma database path!")
exit(1)
# Define the prompt template
# A context must be passed in to the RetrievalQA even if it is empty.
template = """
Context: {context}
Question: {question}
Answer:
"""
prompt = PromptTemplate(
template=template,
input_variables=['context', 'question']
)
# Load Chroma vector database from disk
embedding_function = SentenceTransformerEmbeddings(
model_name="all-MiniLM-L6-v2"
)
db = Chroma(persist_directory=chromadbdir,
embedding_function=embedding_function
)
# Load the LlamaCpp language model and adjust GPU usage
llm = LlamaCpp(
model_path=llama_path,
n_ctx=2048,
n_gpu_layers=-1,
n_batch=512,
verbose=False,
)
# Setup the RAG chain with our LLM
retriever = db.as_retriever(search_kwargs={'k': 2})
chain = RetrievalQA.from_chain_type(llm=llm,
chain_type='stuff',
retriever=retriever,
return_source_documents=False,
chain_type_kwargs={'prompt': prompt})
llm_chain = LLMChain(prompt=prompt, llm=llm)
print("Chatbot initialized, ready to chat...")
while True:
question = input("> ")
answer = chain({'query':question})['result']
print(answer, '\n')