Tanmay09516 commited on
Commit
1924a16
·
verified ·
1 Parent(s): ebd67e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -103
app.py CHANGED
@@ -1,125 +1,81 @@
 
1
  import os
2
- import warnings
3
  from dotenv import load_dotenv
4
- import gradio as gr
5
-
6
-
7
  from qdrant_search import QdrantSearch
8
  from langchain_groq import ChatGroq
9
  from nomic_embeddings import EmbeddingsModel
 
10
 
11
-
12
-
13
-
14
- # Load environment variables from .env file
15
  load_dotenv()
16
 
17
- # Suppress FutureWarnings
18
  warnings.filterwarnings("ignore", category=FutureWarning)
19
 
20
- # Disable tokenizers parallelism to avoid potential issues
21
  os.environ["TOKENIZERS_PARALLELISM"] = "FALSE"
22
 
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
  # Initialize global variables
36
  collection_names = ["docs_v1_2", "docs_v2_2", "docs_v3_2"]
37
  limit = 5
 
 
 
 
 
38
  embeddings=embeddings
39
  )
40
 
41
- def chat_endpoint(question: str):
42
- """
43
- Handles the chat functionality by processing the user's question,
44
- retrieving relevant documents, generating an answer, and returning sources.
45
-
46
- Args:
47
- question (str): The user's question.
48
-
49
-
50
- Returns:
51
- Tuple[str, str]: The generated answer and the sources used.
52
- """
53
- query = question.strip()
54
- if not query:
55
- return "❌ **Error:** Query cannot be empty.", "No sources available."
56
-
57
- # Step 1: Retrieve relevant documents from Qdrant
58
  retrieved_docs = search.query_multiple_collections(query, collection_names, limit)
59
-
60
- if not retrieved_docs:
61
- return "⚠️ **No relevant documents found** for your query.", "No sources available."
62
-
63
-
64
-
65
-
66
- # Step 2: Prepare the context from retrieved documents
67
- context = "\n\n".join([doc['text'] for doc in retrieved_docs])
 
 
 
 
 
68
  try:
69
- answer = llm.invoke(prompt)
70
  except Exception as e:
71
- return f"⚠️ **Error generating answer:** {str(e)}", "No sources available."
72
-
73
  # Prepare sources
74
- sources_md = "\n\n".join([
75
- f"**Source:** {src['source']}\n**Excerpt:** {src['text']}"
76
- for src in retrieved_docs
77
- ])
78
-
79
- return answer.content.strip(), sources_md
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
- # Create Gradio Interface
106
- interface = gr.Interface(
107
- fn=chat_endpoint,
108
- inputs=gr.Textbox(
109
- lines=2,
110
- placeholder="Type your question here...",
111
- description="Ask questions about the LangChain Python Library and get answers based on the latest documentation."
112
- )
113
-
114
- # If running locally, uncomment the following lines:
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
- # if __name__ == "__main__":
125
- # interface.launch()
 
1
+ # app.py
2
  import os
 
3
  from dotenv import load_dotenv
4
+ from pydantic import BaseModel
 
 
5
  from qdrant_search import QdrantSearch
6
  from langchain_groq import ChatGroq
7
  from nomic_embeddings import EmbeddingsModel
8
+ import gradio as gr
9
 
 
 
 
 
10
  load_dotenv()
11
 
12
+ import warnings
13
  warnings.filterwarnings("ignore", category=FutureWarning)
14
 
 
15
  os.environ["TOKENIZERS_PARALLELISM"] = "FALSE"
16
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Initialize global variables
18
  collection_names = ["docs_v1_2", "docs_v2_2", "docs_v3_2"]
19
  limit = 5
20
+ llm = ChatGroq(model="mixtral-8x7b-32768")
21
+ embeddings = EmbeddingsModel()
22
+ search = QdrantSearch(
23
+ qdrant_url=os.environ["QDRANT_CLOUD_URL"],
24
+ api_key=os.environ["QDRANT_API_KEY"],
25
  embeddings=embeddings
26
  )
27
 
28
+ # Define the query processing function
29
+ def chat_with_langassist(query: str):
30
+ if not query.strip():
31
+ return "Query cannot be empty.", []
32
+
33
+ # Retrieve relevant documents from Qdrant
 
 
 
 
 
 
 
 
 
 
 
34
  retrieved_docs = search.query_multiple_collections(query, collection_names, limit)
35
+
36
+ # Prepare the context from retrieved documents
37
+ context = "\n".join([doc['text'] for doc in retrieved_docs])
38
+
39
+ # Construct the prompt with context and question
40
+ prompt = (
41
+ "You are LangAssist, a knowledgeable assistant for the LangChain Python Library. "
42
+ "Given the following context from the documentation, provide a helpful answer to the user's question.\n\n"
43
+ "Context:\n{context}\n\n"
44
+ "Question: {question}\n\n"
45
+ "Answer:"
46
+ ).format(context=context, question=query)
47
+
48
+ # Generate an answer using the language model
49
  try:
50
+ answer = llm.invoke(prompt).content.strip()
51
  except Exception as e:
52
+ return f"Error: {str(e)}", []
53
+
54
  # Prepare sources
55
+ sources = [
56
+ {
57
+ "source": doc['source'],
58
+ "text": doc['text']
59
+ } for doc in retrieved_docs
60
+ ]
61
+
62
+ return answer, sources
63
+
64
+ # Define Gradio interface
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("<h1>LangAssist Chat</h1>")
67
+ chatbot = gr.Chatbot()
68
+ msg = gr.Textbox()
69
+ clear = gr.Button("Clear")
70
+
71
+ def respond(message, chat_history):
72
+ answer, sources = chat_with_langassist(message)
73
+ chat_history.append((message, answer))
74
+ return chat_history, gr.update(value=''), sources
75
+
76
+ msg.submit(respond, [msg, chatbot], [chatbot, msg])
77
+ clear.click(lambda: None, None, chatbot)
78
+
79
+ # Run the Gradio app
80
+ if __name__ == "__main__":
81
+ demo.launch()