ubaid
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,62 @@
|
|
1 |
-
import langchain
|
2 |
-
from langchain.chat_models import ChatOpenAI
|
3 |
-
from langchain.chains import RetrievalQA
|
4 |
-
from langchain_groq import ChatGroq
|
5 |
-
from langchain.prompts import PromptTemplate
|
6 |
-
from langchain.vectorstores import Chroma
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
from langchain.
|
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 |
-
|
|
|
|
1 |
+
import langchain
|
2 |
+
from langchain.chat_models import ChatOpenAI
|
3 |
+
from langchain.chains import RetrievalQA
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
from langchain.vectorstores import Chroma
|
7 |
+
import os
|
8 |
+
from langchain.embeddings import TensorflowHubEmbeddings,HuggingFaceEmbeddings
|
9 |
+
import gradio as gr
|
10 |
+
from langchain.memory import ConversationBufferMemory
|
11 |
+
from langchain.chains import ConversationalRetrievalChain
|
12 |
+
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
13 |
+
llm=ChatGroq(api_key=os.getenv('groq_api'))
|
14 |
+
vector_db = Chroma(persist_directory='./db', embedding_function=embeddings)
|
15 |
+
# Define a custom prompt template (Modify as needed)
|
16 |
+
prompt_template = PromptTemplate.from_template(
|
17 |
+
"Use the following context to answer the question you have no answer tell me i dont know:\n\n{context}\n\nQuestion: {question} "
|
18 |
+
)
|
19 |
+
memory=ConversationBufferMemory(
|
20 |
+
memory_key='chat_history',
|
21 |
+
return_messages=True
|
22 |
+
)
|
23 |
+
|
24 |
+
prompt=PromptTemplate.from_template(
|
25 |
+
"""You are an AI assistant that provides accurate and concise answers.
|
26 |
+
Use the following retrieved documents to answer the question. If you don't know, say "I don't know."
|
27 |
+
|
28 |
+
Context:
|
29 |
+
{context}
|
30 |
+
|
31 |
+
Question:
|
32 |
+
{question}
|
33 |
+
|
34 |
+
Answer:
|
35 |
+
""")
|
36 |
+
|
37 |
+
retriever=vector_db.as_retriever(
|
38 |
+
search_type='similarity',
|
39 |
+
search_kwargs={'k':4}
|
40 |
+
)
|
41 |
+
|
42 |
+
|
43 |
+
con=ConversationalRetrievalChain.from_llm(
|
44 |
+
llm=llm,
|
45 |
+
retriever=retriever,
|
46 |
+
memory=memory,
|
47 |
+
combine_docs_chain_kwargs={'prompt':prompt})
|
48 |
+
|
49 |
+
# Use the new RetrievalQA structure
|
50 |
+
retriever=vector_db.as_retriever(search_kwargs={"k": 3})
|
51 |
+
|
52 |
+
|
53 |
+
def chat(query,history):
|
54 |
+
try:
|
55 |
+
response=con.run({'question':query,'chat_history':history})
|
56 |
+
return str(response)
|
57 |
+
except Exception as e:
|
58 |
+
return 'slow connection'
|
59 |
+
|
60 |
+
app=gr.ChatInterface(chat,theme=gr.themes.Soft(),title='Chat With Nasir Hussain'
|
61 |
+
,description='I have provide llm Nasir Husssain Youtube Playlist Data Playlist Link Here:https://youtube.com/playlist?list=PLuYWhEqu9a9A7s21UXlZ1yYNPk5ZLfhpH&si=qg8iuts2csW3P4bQ')
|
62 |
+
app.launch(share=True)
|