Update app.py
Browse files
app.py
CHANGED
@@ -8,12 +8,10 @@ from langgraph.graph import StateGraph, MessagesState, START
|
|
8 |
from langchain_core.messages import SystemMessage, HumanMessage
|
9 |
from langchain_community.document_loaders import WikipediaLoader
|
10 |
from langchain_community.tools import TavilySearchResults, DuckDuckGoSearchRun
|
11 |
-
from dotenv import load_dotenv
|
12 |
import operator
|
13 |
from typing import Annotated
|
14 |
from typing_extensions import TypedDict
|
15 |
|
16 |
-
load_dotenv()
|
17 |
# (Keep Constants as is)
|
18 |
# --- Constants ---
|
19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -44,75 +42,76 @@ class State(TypedDict):
|
|
44 |
answer: str
|
45 |
context: Annotated[list, operator.add]
|
46 |
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
|
65 |
-
|
66 |
|
67 |
-
|
68 |
|
69 |
-
|
70 |
-
|
71 |
load_max_docs=2).load()
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
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 |
-
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
|
115 |
-
|
116 |
|
117 |
|
118 |
builder = StateGraph(State)
|
|
|
8 |
from langchain_core.messages import SystemMessage, HumanMessage
|
9 |
from langchain_community.document_loaders import WikipediaLoader
|
10 |
from langchain_community.tools import TavilySearchResults, DuckDuckGoSearchRun
|
|
|
11 |
import operator
|
12 |
from typing import Annotated
|
13 |
from typing_extensions import TypedDict
|
14 |
|
|
|
15 |
# (Keep Constants as is)
|
16 |
# --- Constants ---
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
42 |
answer: str
|
43 |
context: Annotated[list, operator.add]
|
44 |
|
45 |
+
def search_tavily(state):
|
46 |
|
47 |
+
""" Retrieve docs from web search """
|
48 |
+
|
49 |
+
# Search
|
50 |
+
tavily_search = TavilySearchResults(max_results=2)
|
51 |
+
|
52 |
+
search_docs = tavily_search.invoke(state['question'])
|
53 |
+
|
54 |
+
# Format
|
55 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
56 |
+
[
|
57 |
+
f'<Document href="{doc["url"]}"/>\n{doc["content"]}\n</Document>'
|
58 |
+
for doc in search_docs
|
59 |
+
]
|
60 |
+
)
|
61 |
|
62 |
+
return {"context": [formatted_search_docs]}
|
63 |
|
64 |
+
def search_wikipedia(state):
|
65 |
|
66 |
+
""" Retrieve docs from wikipedia """
|
67 |
|
68 |
+
# Search
|
69 |
+
search_docs = WikipediaLoader(query=state['question'],
|
70 |
load_max_docs=2).load()
|
71 |
|
72 |
+
# Format
|
73 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
74 |
+
[
|
75 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
76 |
+
for doc in search_docs
|
77 |
+
]
|
78 |
+
)
|
79 |
|
80 |
+
return {"context": [formatted_search_docs]}
|
81 |
|
82 |
+
def search_DuckDuckGo(state):
|
83 |
|
84 |
+
""" Retrive answer from DuckDuckGoSearch."""
|
85 |
|
86 |
+
ddg_search = DuckDuckGoSearchRun(max_results=2)
|
87 |
+
search_docs = ddg_search.invoke(state['question'])
|
88 |
|
89 |
+
# Format
|
90 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
91 |
+
[
|
92 |
+
f'<Document href="{doc["url"]}"/>\n{doc["content"]}\n</Document>'
|
93 |
+
for doc in search_docs
|
94 |
+
]
|
95 |
+
)
|
96 |
|
97 |
+
return {"context": [formatted_search_docs]}
|
98 |
|
99 |
+
def generate_answer(state):
|
100 |
|
101 |
+
"""Node to give answer to the question"""
|
102 |
|
103 |
+
context = state["context"]
|
104 |
+
question = state["question"]
|
105 |
|
106 |
+
additional_context_template = """Here are some contexts about the question you can use if you find it helpful: {context}"""
|
107 |
+
additional_context = additional_context_template.format(context=context)
|
108 |
+
final_instruction = SYSTEM_MESSAGE + additional_context
|
109 |
|
110 |
+
#answer
|
111 |
+
answer = llm.invoke([SystemMessage(content=final_instruction)] + [HumanMessage(content=f"Answer the question: {question}")])
|
112 |
|
113 |
+
# Append it to state
|
114 |
+
return {"answer": answer}
|
115 |
|
116 |
|
117 |
builder = StateGraph(State)
|