h3110Fr13nd commited on
Commit
d48537f
1 Parent(s): 79340f2

RAG using Chroma Langchain

Browse files
Files changed (3) hide show
  1. README.md +5 -1
  2. main.py +49 -12
  3. setup.py +1 -1
README.md CHANGED
@@ -18,4 +18,8 @@
18
  HF_PASS=your-password
19
  ```
20
 
21
- Now you can run the chatbot and interact with it.
 
 
 
 
 
18
  HF_PASS=your-password
19
  ```
20
 
21
+ Now you can run the chatbot and interact with it.
22
+
23
+
24
+
25
+ https://github.com/langchain-ai/langchain/issues/6628#issuecomment-1935374689
main.py CHANGED
@@ -10,39 +10,75 @@ from langchain_core.runnables import RunnablePassthrough
10
  from langchain_core.documents import Document
11
  from langchain_core.prompts import ChatPromptTemplate
12
  from langchain_core.output_parsers import StrOutputParser
 
13
  # from langchain_community.chains import
14
  from langchain_community.chat_models import ChatOllama
15
  from langchain_chroma import Chroma
16
  from hugchat import hugchat
 
17
  from hugchat.login import Login
18
  import dotenv
19
  from utils import HuggingChat
20
- from langchain import PromptTemplate
 
 
 
21
 
22
  dotenv.load_dotenv()
23
 
24
 
25
  class GradioApp:
26
  def __init__(self):
 
 
27
  # self.llm = ChatOllama(model="phi3:3.8b", base_url="http://localhost:11434", num_gpu=32)
28
 
29
 
30
- template = """
31
- You are a helpful health assistant. These Human will ask you a questions about their pregnancy health.
32
- Use following piece of context to answer the question.
33
- If you don't know the answer, just say you don't know.
34
- Keep the answer within 2 sentences and concise.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- Context: {context}
37
- Question: {question}
38
- Answer:
39
 
40
- """
41
 
 
 
 
 
 
 
 
 
42
  self.llm = HuggingChat(email = os.getenv("HF_EMAIL") , psw = os.getenv("HF_PASS") )
43
- self.chain = (self.llm | StrOutputParser())
 
 
 
 
 
 
 
 
44
 
45
  def user(self,user_message, history):
 
46
  return "", history + [[user_message, None]]
47
 
48
  def bot(self,history):
@@ -53,7 +89,8 @@ class GradioApp:
53
  history[-1][1] += chunks
54
  yield history
55
  history[-1][1] = history[-1][1] or ""
56
- history[-1][1] += self.chain.invoke(prompt)
 
57
  print(history[-1][1])
58
  print(history)
59
  return history
 
10
  from langchain_core.documents import Document
11
  from langchain_core.prompts import ChatPromptTemplate
12
  from langchain_core.output_parsers import StrOutputParser
13
+
14
  # from langchain_community.chains import
15
  from langchain_community.chat_models import ChatOllama
16
  from langchain_chroma import Chroma
17
  from hugchat import hugchat
18
+ # from langchain.callbacks import SystemMessage
19
  from hugchat.login import Login
20
  import dotenv
21
  from utils import HuggingChat
22
+ from langchain_core.prompts import PromptTemplate
23
+ from langchain_community.embeddings import HuggingFaceEmbeddings
24
+ import langchain
25
+ langchain.debug = True
26
 
27
  dotenv.load_dotenv()
28
 
29
 
30
  class GradioApp:
31
  def __init__(self):
32
+
33
+ self.history = []
34
  # self.llm = ChatOllama(model="phi3:3.8b", base_url="http://localhost:11434", num_gpu=32)
35
 
36
 
37
+ # template = """
38
+ # You are a helpful health assistant. These Human will ask you a questions about their pregnancy health.
39
+ # Use following piece of context to answer the question.
40
+ # If you don't know the answer, just say you don't know.
41
+ # Keep the answer within 2 sentences and concise.
42
+
43
+ # Context: {context}
44
+ # Question: {question}
45
+ # Answer: """
46
+
47
+
48
+ self.template = """
49
+ You are a helpful AI bot that guides the customer or user through the website content and provides the user with exact details they want.
50
+ You help everyone by answering questions, and improve your answers from previous answers in History.
51
+ Don't try to make up an answer, if you don't know, just say that you don't know.
52
+ Answer in the same language the question was asked.
53
+ Answer in a way that is easy to understand.
54
+ Try to limit the answer to 3-4 sentences.
55
+ Do not say "Based on the information you provided, ..." or "I think the answer is...". Just answer the question directly in detail.
56
 
57
+ History: {chat_history}
 
 
58
 
59
+ Context: {context}
60
 
61
+ Question: {question}
62
+ Answer:
63
+ """
64
+ self.prompt = PromptTemplate(
65
+ template=self.template,
66
+ input_variables=["chat_history","context", "question"]
67
+ )
68
+ self.db = Chroma(persist_directory="./pragetx_chroma", embedding_function=HuggingFaceEmbeddings())
69
  self.llm = HuggingChat(email = os.getenv("HF_EMAIL") , psw = os.getenv("HF_PASS") )
70
+ self.chain = (
71
+ {"chat_history": self.chat_history, "context": self.db.as_retriever(k=1), "question": RunnablePassthrough()} |
72
+ self.prompt |
73
+ self.llm |
74
+ StrOutputParser())
75
+ def chat_history(self, history):
76
+ print(self.history)
77
+ print("\n".join(f"##Human: {x[0]}\n{'##Bot: '+x[1] if x[1] else ''}" for x in self.history))
78
+ return "\n".join(f"##Human: {x[0]}\n{'##Bot: '+x[1] if x[1] else ''}" for x in self.history)
79
 
80
  def user(self,user_message, history):
81
+ self.history = history + [[user_message, None]]
82
  return "", history + [[user_message, None]]
83
 
84
  def bot(self,history):
 
89
  history[-1][1] += chunks
90
  yield history
91
  history[-1][1] = history[-1][1] or ""
92
+ self.history = history
93
+ # history[-1][1] += self.chain.invoke(prompt)
94
  print(history[-1][1])
95
  print(history)
96
  return history
setup.py CHANGED
@@ -6,7 +6,7 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
6
 
7
  loader = TextLoader('./pragetx.md')
8
  documents = loader.load()
9
- text_splitter = CharacterTextSplitter(chunk_size=4000, chunk_overlap=4)
10
  docs = text_splitter.split_documents(documents)
11
 
12
  embeddings = HuggingFaceEmbeddings()
 
6
 
7
  loader = TextLoader('./pragetx.md')
8
  documents = loader.load()
9
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=4)
10
  docs = text_splitter.split_documents(documents)
11
 
12
  embeddings = HuggingFaceEmbeddings()