sunnyday910 commited on
Commit
e7844c5
·
verified ·
1 Parent(s): b1becf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -52
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
- def search_tavily(state):
48
 
49
- """ Retrieve docs from web search """
50
-
51
- # Search
52
- tavily_search = TavilySearchResults(max_results=2)
53
- search_docs = tavily_search.invoke(state['question'])
54
-
55
- # Format
56
- formatted_search_docs = "\n\n---\n\n".join(
57
- [
58
- f'<Document href="{doc["url"]}"/>\n{doc["content"]}\n</Document>'
59
- for doc in search_docs
60
- ]
61
- )
 
62
 
63
- return {"context": [formatted_search_docs]}
64
 
65
- def search_wikipedia(state):
66
 
67
- """ Retrieve docs from wikipedia """
68
 
69
- # Search
70
- search_docs = WikipediaLoader(query=state['question'],
71
  load_max_docs=2).load()
72
 
73
- # Format
74
- formatted_search_docs = "\n\n---\n\n".join(
75
- [
76
- f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
77
- for doc in search_docs
78
- ]
79
- )
80
 
81
- return {"context": [formatted_search_docs]}
82
 
83
- def search_DuckDuckGo(state):
84
 
85
- """ Retrive answer from DuckDuckGoSearch."""
86
 
87
- ddg_search = DuckDuckGoSearchRun(max_results=2)
88
- search_docs = ddg_search.invoke(state['question'])
89
 
90
- # Format
91
- formatted_search_docs = "\n\n---\n\n".join(
92
- [
93
- f'<Document href="{doc["url"]}"/>\n{doc["content"]}\n</Document>'
94
- for doc in search_docs
95
- ]
96
- )
97
 
98
- return {"context": [formatted_search_docs]}
99
 
100
- def generate_answer(state):
101
 
102
- """Node to give answer to the question"""
103
 
104
- context = state["context"]
105
- question = state["question"]
106
 
107
- additional_context_template = """Here are some contexts about the question you can use if you find it helpful: {context}"""
108
- additional_context = additional_context_template.format(context=context)
109
- final_instruction = SYSTEM_MESSAGE + additional_context
110
 
111
- #answer
112
- answer = llm.invoke([SystemMessage(content=final_instruction)] + [HumanMessage(content=f"Answer the question: {question}")])
113
 
114
- # Append it to state
115
- return {"answer": answer}
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)