-
Notifications
You must be signed in to change notification settings - Fork 2
/
langchain_app.py
61 lines (54 loc) · 2.21 KB
/
langchain_app.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
#@ Building document reader fully local
from langchain_community.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.prompts import PromptTemplate
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_community.llms import LlamaCpp
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
#@ Loading hugging face embeddings
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
embeddings = HuggingFaceEmbeddings()
def file_to_vdb(FILE_DIRECTORY,embeddings):
#@ loading files to vector stores
loader = DirectoryLoader(FILE_DIRECTORY)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=16)
splits = text_splitter.split_documents(docs)
vector_stores = Chroma.from_documents(splits , embeddings)
return vector_stores
def generate_resonse(llm,vdb,query):
#@ creating template and loading llm
template = """Question: {query}\n
Answer : """
prompt = PromptTemplate(template=template, input_variables=["query"])
retriever = vdb.as_retriever()
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
#@ LLM
llm = llm
#@ retriever chain
rag_chain = (
{"context": retriever | format_docs, "query": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
response = rag_chain.invoke(query)
return response
def main_runner(vdb,query):
# Callbacks support token-wise streaming
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
llm = LlamaCpp(
model_path="C:/Users/ASUS/.cache/lm-studio/models/TheBloke/phi-2-GGUF/phi-2.Q4_K_S.gguf",
temperature=0.2,
max_tokens=2000,
top_p=1,
callback_manager=callback_manager,
verbose=True, # Verbose is required to pass to the callback manager
)
output = generate_resonse(llm,vdb,query)
return output