jonathanjordan21 commited on
Commit
b4cb4dd
1 Parent(s): 6222108

Update custom_llm.py

Browse files
Files changed (1) hide show
  1. custom_llm.py +19 -2
custom_llm.py CHANGED
@@ -7,13 +7,30 @@ import requests
7
  from langchain.prompts import PromptTemplate, ChatPromptTemplate
8
  from operator import itemgetter
9
 
10
- # from langchain.memory import ConversationBufferMemory
11
  from langchain.memory import ChatMessageHistory, ConversationBufferMemory
12
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
13
  from langchain_community.chat_models import ChatOpenAI
14
  from langchain_core.runnables import RunnableLambda, RunnablePassthrough
15
  from langchain_core.messages import AIMessage, HumanMessage
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def custom_chain_with_history(llm, memory):
19
 
@@ -50,7 +67,7 @@ def custom_chain_with_history(llm, memory):
50
  # ]
51
  # )
52
 
53
- return {"chat_history":prompt_memory, "context":db.as_retriever(search_type="similarity", search_kwargs={"k": 8}) | format_docs, "question": RunnablePassthrough()} | prompt | llm
54
 
55
  class CustomLLM(LLM):
56
  repo_id : str
 
7
  from langchain.prompts import PromptTemplate, ChatPromptTemplate
8
  from operator import itemgetter
9
 
 
10
  from langchain.memory import ChatMessageHistory, ConversationBufferMemory
11
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
12
  from langchain_community.chat_models import ChatOpenAI
13
  from langchain_core.runnables import RunnableLambda, RunnablePassthrough
14
  from langchain_core.messages import AIMessage, HumanMessage
15
 
16
+ from langchain_community.document_loaders import DirectoryLoader
17
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
18
+ from langchain_community.document_loaders import PyMuPDFLoader
19
+ import os
20
+ from langchain.embeddings import HuggingFaceEmbeddings
21
+ from langchain.vectorstores import FAISS
22
+
23
+
24
+ def create_vectorstore():
25
+ loader = os.getenv('knowledge_base')
26
+
27
+ splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=20)
28
+
29
+ docs = splitter.create_documents([loader])
30
+
31
+ emb_model = HuggingFaceEmbeddings(model_name='sentence-transformers/paraphrase-multilingual-mpnet-base-v2', encode_kwargs={'normalize_embeddings': True})
32
+ db = FAISS.from_documents(docs, emb_model)
33
+ return db
34
 
35
  def custom_chain_with_history(llm, memory):
36
 
 
67
  # ]
68
  # )
69
 
70
+ return {"chat_history":prompt_memory, "context":create_vectorstore().as_retriever(search_type="similarity", search_kwargs={"k": 8}) | format_docs, "question": RunnablePassthrough()} | prompt | llm
71
 
72
  class CustomLLM(LLM):
73
  repo_id : str