rishisim commited on
Commit
62855fd
·
verified ·
1 Parent(s): 91a0713

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -11
app.py CHANGED
@@ -42,13 +42,12 @@ In the answer try to provide as much text as possible from "response" section in
42
  If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
43
  If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at rishi@aiotsmartlabs.com" Don't try to make up an answer.
44
  CONTEXT: {context}
45
- HISTORY: {history}
46
  QUESTION: {question}""")
47
 
48
  from langchain_core.runnables import RunnablePassthrough
49
 
50
  rag_chain = (
51
- {"context": retriever, "history": RunnablePassthrough(), "question": RunnablePassthrough()}
52
  | prompt
53
  | llm
54
  | StrOutputParser()
@@ -56,21 +55,92 @@ rag_chain = (
56
 
57
  # Define the chat response function
58
  def chatresponse(message, history):
59
- history_text = "\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in history])
60
-
61
- inputs = {
62
- "context": "", # context will be retrieved by the retriever
63
- "history": history_text,
64
- "question": message
65
- }
66
-
67
- output = rag_chain.invoke(inputs)
68
  response = output.split('ANSWER: ')[-1].strip()
69
  return response
70
 
71
  # Launch the Gradio chat interface
72
  gr.ChatInterface(chatresponse).launch()
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # import gradio as gr
75
  # from langchain.schema import AIMessage, HumanMessage
76
 
 
42
  If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
43
  If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at rishi@aiotsmartlabs.com" Don't try to make up an answer.
44
  CONTEXT: {context}
 
45
  QUESTION: {question}""")
46
 
47
  from langchain_core.runnables import RunnablePassthrough
48
 
49
  rag_chain = (
50
+ {"context": retriever, "question": RunnablePassthrough()}
51
  | prompt
52
  | llm
53
  | StrOutputParser()
 
55
 
56
  # Define the chat response function
57
  def chatresponse(message, history):
58
+ output = rag_chain.invoke(message)
 
 
 
 
 
 
 
 
59
  response = output.split('ANSWER: ')[-1].strip()
60
  return response
61
 
62
  # Launch the Gradio chat interface
63
  gr.ChatInterface(chatresponse).launch()
64
 
65
+ # import gradio as gr
66
+
67
+
68
+ # import os
69
+ # hftoken = os.environ["hftoken"]
70
+
71
+ # from langchain_huggingface import HuggingFaceEndpoint
72
+
73
+ # repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
74
+ # llm = HuggingFaceEndpoint(repo_id = repo_id, max_new_tokens = 128, temperature = 0.7, huggingfacehub_api_token = hftoken)
75
+
76
+ # from langchain_core.output_parsers import StrOutputParser
77
+ # from langchain_core.prompts import ChatPromptTemplate
78
+
79
+ # prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
80
+ # chain = prompt | llm | StrOutputParser()
81
+
82
+ # # from langchain.document_loaders.csv_loader import CSVLoader
83
+ # from langchain_community.document_loaders.csv_loader import CSVLoader
84
+
85
+
86
+ # loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
87
+ # data = loader.load()
88
+
89
+ # from langchain_huggingface import HuggingFaceEmbeddings
90
+ # from langchain_chroma import Chroma
91
+ # from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
92
+
93
+ # # CHECK MTEB LEADERBOARD & FIND BEST EMBEDDING MODEL
94
+ # model = "BAAI/bge-m3"
95
+ # embeddings = HuggingFaceEndpointEmbeddings(model = model)
96
+
97
+ # vectorstore = Chroma.from_documents(documents = data, embedding = embeddings)
98
+ # retriever = vectorstore.as_retriever()
99
+
100
+ # # from langchain.prompts import PromptTemplate
101
+
102
+ # from langchain_core.prompts import ChatPromptTemplate
103
+
104
+ # prompt = ChatPromptTemplate.from_template("""Given the following context and a question, generate an answer based on the context only.
105
+ # In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
106
+ # If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
107
+ # If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at rishi@aiotsmartlabs.com" Don't try to make up an answer.
108
+ # CONTEXT: {context}
109
+ # QUESTION: {question}""")
110
+
111
+ # from langchain_core.runnables import RunnablePassthrough
112
+
113
+ # # rag_chain = (
114
+ # # {"context": retriever, "history": RunnablePassthrough(), "question": RunnablePassthrough()}
115
+ # # | prompt
116
+ # # | llm
117
+ # # | StrOutputParser()
118
+ # # )
119
+
120
+ # rag_chain = (
121
+ # {"context": retriever, "question": RunnablePassthrough()}
122
+ # | prompt
123
+ # | llm
124
+ # | StrOutputParser()
125
+ # )
126
+
127
+ # # Define the chat response function
128
+ # def chatresponse(message, history):
129
+ # # history_text = "\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in history])
130
+
131
+ # # inputs = {
132
+ # # # "context": retriever, # context will be retrieved by the retriever
133
+ # # # "history": history_text,
134
+ # # "question": message
135
+ # # }
136
+
137
+ # output = rag_chain.invoke(message)
138
+ # response = output.split('ANSWER: ')[-1].strip()
139
+ # return response
140
+
141
+ # # Launch the Gradio chat interface
142
+ # gr.ChatInterface(chatresponse).launch()
143
+
144
  # import gradio as gr
145
  # from langchain.schema import AIMessage, HumanMessage
146