nagesh5 commited on
Commit
5752744
·
verified ·
1 Parent(s): b824969

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +14 -29
app.py CHANGED
@@ -94,7 +94,6 @@ Settings.llm = llm # Complete the code to define the LLM model
94
  Settings.embedding = embedding_model # Complete the code to define the embedding model
95
 
96
  #================================Creating Langgraph agent======================#
97
-
98
  class AgentState(TypedDict):
99
  query: str # The current user query
100
  expanded_query: str # The expanded version of the user query
@@ -109,9 +108,11 @@ class AgentState(TypedDict):
109
  groundedness_check: bool
110
  loop_max_iter: int
111
 
112
- def expand_query(state):
 
 
113
  """
114
- Expands the user query to improve retrieval of nutrition disorder-related information.
115
 
116
  Args:
117
  state (Dict): The current state of the workflow, containing the user query.
@@ -119,7 +120,6 @@ def expand_query(state):
119
  Returns:
120
  Dict: The updated state with the expanded query.
121
  """
122
- print("---------Expanding Query---------")
123
  system_message = """
124
  You are a domain expert assisting in answering questions related to Nutritional Disorders.
125
  Perform query expansion on the question received. If there are multiple common ways of phrasing a user question \
@@ -140,31 +140,18 @@ def expand_query(state):
140
  expand_prompt = ChatPromptTemplate.from_messages([
141
  ("system", system_message),
142
  ("user", "Expand this query: {query} using the feedback: {query_feedback}")
143
-
144
  ])
145
 
146
  chain = expand_prompt | llm | StrOutputParser()
147
- expanded_query = chain.invoke({"query": state['query'], "query_feedback":state["query_feedback"]})
148
- print("expanded_query", expanded_query)
149
- state["expanded_query"] = expanded_query
 
150
  return state
151
 
152
 
153
- # Initialize the Chroma vector store for retrieving documents
154
- vector_store = Chroma(
155
- collection_name="nutritional_hypotheticals",
156
- persist_directory="./nutritional_db",
157
- embedding_function=embedding_model
158
 
159
- )
160
-
161
- # Create a retriever from the vector store
162
- retriever = vector_store.as_retriever(
163
- search_type='similarity',
164
- search_kwargs={'k': 3}
165
- )
166
-
167
- def retrieve_context(state):
168
  """
169
  Retrieves context from the vector store using the expanded or original query.
170
 
@@ -174,25 +161,23 @@ def retrieve_context(state):
174
  Returns:
175
  Dict: The updated state with the retrieved context.
176
  """
177
- print("---------retrieve_context---------")
178
- query = state['expanded_query'] # Complete the code to define the key for the expanded query
179
- #print("Query used for retrieval:", query) # Debugging: Print the query
180
 
181
  # Retrieve documents from the vector store
182
  docs = retriever.invoke(query)
183
  print("Retrieved documents:", docs) # Debugging: Print the raw docs object
184
 
185
  # Extract both page_content and metadata from each document
186
- context= [
187
  {
188
  "content": doc.page_content, # The actual content of the document
189
  "metadata": doc.metadata # The metadata (e.g., source, page number, etc.)
190
  }
191
  for doc in docs
192
  ]
193
- state['context'] = context # Complete the code to define the key for storing the context
194
- print("Extracted context with metadata:", context) # Debugging: Print the extracted context
195
- #print(f"Groundedness loop count: {state['groundedness_loop_count']}")
196
  return state
197
 
198
 
 
94
  Settings.embedding = embedding_model # Complete the code to define the embedding model
95
 
96
  #================================Creating Langgraph agent======================#
 
97
  class AgentState(TypedDict):
98
  query: str # The current user query
99
  expanded_query: str # The expanded version of the user query
 
108
  groundedness_check: bool
109
  loop_max_iter: int
110
 
111
+
112
+
113
+ def expand_query(state: AgentState) -> AgentState:
114
  """
115
+ Expands the user query to improve retrieval of nutrition disorder-related information using few-shot prompting.
116
 
117
  Args:
118
  state (Dict): The current state of the workflow, containing the user query.
 
120
  Returns:
121
  Dict: The updated state with the expanded query.
122
  """
 
123
  system_message = """
124
  You are a domain expert assisting in answering questions related to Nutritional Disorders.
125
  Perform query expansion on the question received. If there are multiple common ways of phrasing a user question \
 
140
  expand_prompt = ChatPromptTemplate.from_messages([
141
  ("system", system_message),
142
  ("user", "Expand this query: {query} using the feedback: {query_feedback}")
 
143
  ])
144
 
145
  chain = expand_prompt | llm | StrOutputParser()
146
+ #expanded_query = chain.invoke({"query": state['query'], "query_feedback":state["query_feedback"]})
147
+ #print("expanded_query", expanded_query)
148
+ #state["expanded_query"] = expanded_query
149
+ state['expanded_query'] = chain.invoke({"query": state['query'], "question": state['query'], "query_feedback": state.get('query_feedback', '')}) # Pass all required variables
150
  return state
151
 
152
 
 
 
 
 
 
153
 
154
+ def retrieve_context(state: AgentState) -> AgentState:
 
 
 
 
 
 
 
 
155
  """
156
  Retrieves context from the vector store using the expanded or original query.
157
 
 
161
  Returns:
162
  Dict: The updated state with the retrieved context.
163
  """
164
+ query = state['expanded_query']
165
+ print("Query used for retrieval:", query) # Debugging: Print the query
 
166
 
167
  # Retrieve documents from the vector store
168
  docs = retriever.invoke(query)
169
  print("Retrieved documents:", docs) # Debugging: Print the raw docs object
170
 
171
  # Extract both page_content and metadata from each document
172
+ state['context'] = [
173
  {
174
  "content": doc.page_content, # The actual content of the document
175
  "metadata": doc.metadata # The metadata (e.g., source, page number, etc.)
176
  }
177
  for doc in docs
178
  ]
179
+
180
+ print("Extracted context with metadata:", state['context']) # Debugging: Print the extracted context
 
181
  return state
182
 
183