sarath2803 commited on
Commit
89bcab2
·
verified ·
1 Parent(s): 06d87ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -23
app.py CHANGED
@@ -11,7 +11,7 @@ from typing import TypedDict, List, Dict, Any, Optional,Callable
11
  from langgraph.graph import StateGraph, END
12
  from langchain_openai import ChatOpenAI
13
  from langchain_core.messages import HumanMessage
14
- import wikipedia
15
 
16
  # (Keep Constants as is)
17
  # --- Constants ---
@@ -101,29 +101,17 @@ def web_search(query: str) -> str:
101
  return f"Search results for '{query}' : {result}."
102
 
103
  @tool
104
- def wikipedia_tool(query: str, llm: Optional[Callable[[str], str]] =openrouter_inference) -> str:
105
- """
106
- Searches Wikipedia for the given query and uses the LLM to extract a direct answer.
107
- Args:
108
- query (str): The search term or question.
109
- llm (callable, optional): The language model to extract the answer.
110
- Returns:
111
- str: A direct answer or summary from Wikipedia.
112
- """
113
  try:
114
- summary = wikipedia.summary(query, sentences=5)
115
- if llm:
116
- # Ask the LLM to extract a direct answer from the summary
117
- prompt = (
118
- f"Given the following Wikipedia summary, answer the question as directly as possible:\n"
119
- f"Question: {query}\n"
120
- f"Summary: {summary}\n"
121
- f"Answer:"
122
- )
123
- answer = llm(prompt)
124
- return answer.strip()
125
- else:
126
- return summary
127
  except Exception as e:
128
  return f"Wikipedia search error: {e}"
129
 
 
11
  from langgraph.graph import StateGraph, END
12
  from langchain_openai import ChatOpenAI
13
  from langchain_core.messages import HumanMessage
14
+ from langchain_community.document_loaders import WikipediaLoader
15
 
16
  # (Keep Constants as is)
17
  # --- Constants ---
 
101
  return f"Search results for '{query}' : {result}."
102
 
103
  @tool
104
+ def wikipedia_tool(query: str) -> str:
105
+ """Search Wikipedia for a query and return up to 2 results as formatted text."""
 
 
 
 
 
 
 
106
  try:
107
+ search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
108
+ formatted_search_docs = "\n\n---\n\n".join(
109
+ [
110
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
111
+ for doc in search_docs
112
+ ]
113
+ )
114
+ return formatted_search_docs
 
 
 
 
 
115
  except Exception as e:
116
  return f"Wikipedia search error: {e}"
117