HollowVoice commited on
Commit
52237c6
·
1 Parent(s): ddba08c

Added wiki search

Browse files
.gitignore CHANGED
@@ -171,4 +171,6 @@ cython_debug/
171
  .ruff_cache/
172
 
173
  # PyPI configuration file
174
- .pypirc
 
 
 
171
  .ruff_cache/
172
 
173
  # PyPI configuration file
174
+ .pypirc
175
+
176
+ .langgraph_api/
.langgraph_api/.langgraph_checkpoint.1.pckl DELETED
Binary file (24.6 kB)
 
.langgraph_api/.langgraph_checkpoint.2.pckl DELETED
Binary file (5.23 kB)
 
.langgraph_api/.langgraph_checkpoint.3.pckl DELETED
Binary file (13.9 kB)
 
.langgraph_api/.langgraph_ops.pckl DELETED
Binary file (14.1 kB)
 
.langgraph_api/.langgraph_retry_counter.pckl DELETED
Binary file (117 Bytes)
 
.langgraph_api/store.pckl DELETED
Binary file (6 Bytes)
 
.langgraph_api/store.vectors.pckl DELETED
Binary file (6 Bytes)
 
agent.py CHANGED
@@ -7,32 +7,56 @@ from langgraph.graph import START, StateGraph, MessagesState
7
  from langgraph.prebuilt import tools_condition, ToolNode
8
  from langchain_core.runnables import RunnableConfig
9
 
 
 
 
 
 
10
 
11
  load_dotenv()
12
 
13
 
14
  # --- TOOLS ---
15
- def add(a: int, b: int) -> int:
16
- """Adds a and b.
 
17
 
18
  Args:
19
  a: first int
20
  b: second int
21
  """
22
- return a + b
23
 
24
 
25
- def multiply(a: int, b: int) -> int:
26
- """Multiplies a and b.
 
 
27
 
28
  Args:
29
- a: first int
30
- b: second int
31
  """
32
- return a * b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
 
34
 
35
- tools = [add, multiply]
 
36
 
37
 
38
  # --- GRAPH ---
@@ -98,13 +122,25 @@ if __name__ == "__main__":
98
  # Build the graph
99
  graph = create_graph()
100
  # Run the graph
 
 
101
  question = "What is an elephant? "
102
  messages = [HumanMessage(content=question)]
103
  messages = graph.invoke({"messages": messages})
104
  for m in messages["messages"]:
105
  m.pretty_print()
106
 
107
- question = "What is 10+10?"
 
 
 
 
 
 
 
 
 
 
108
  messages = [HumanMessage(content=question)]
109
  messages = graph.invoke({"messages": messages})
110
  for m in messages["messages"]:
 
7
  from langgraph.prebuilt import tools_condition, ToolNode
8
  from langchain_core.runnables import RunnableConfig
9
 
10
+ # Wikepedia tool
11
+ from langchain_community.tools import WikipediaQueryRun
12
+ from langchain_community.utilities import WikipediaAPIWrapper
13
+ from langchain_community.document_loaders import WikipediaLoader
14
+ from langchain_community.retrievers import WikipediaRetriever
15
 
16
  load_dotenv()
17
 
18
 
19
  # --- TOOLS ---
20
+ # Simple tool to test a tool call
21
+ def meaning_of_life(a: int, b: int) -> int:
22
+ """Returns meaning of life
23
 
24
  Args:
25
  a: first int
26
  b: second int
27
  """
28
+ return 42
29
 
30
 
31
+ # https://www.restack.io/docs/langchain-knowledge-wikipedia-loader-cat-ai
32
+ # https://api.python.langchain.com/en/latest/community/document_loaders/langchain_community.document_loaders.wikipedia.WikipediaLoader.html#
33
+ def wikipedia_search(query: str) -> str:
34
+ """Searches Wikipedia for a given query and fetches full document
35
 
36
  Args:
37
+ query: the query to search for
 
38
  """
39
+ loader = loader = WikipediaLoader(
40
+ query=query,
41
+ load_max_docs=1,
42
+ doc_content_chars_max=4000,
43
+ load_all_available_meta=False,
44
+ )
45
+ documents = loader.load()
46
+
47
+ formatted_search_docs = "\n\n---\n\n".join(
48
+ [
49
+ f'<Document source="{next_doc.metadata["source"]}" title="{next_doc.metadata.get("title", "")}"/>\n{next_doc.page_content}\n</Document>'
50
+ for next_doc in documents
51
+ ]
52
+ )
53
+
54
+ result = f"{{wiki_results: {formatted_search_docs}}}"
55
 
56
+ return result
57
 
58
+
59
+ tools = [meaning_of_life, wikipedia_search]
60
 
61
 
62
  # --- GRAPH ---
 
122
  # Build the graph
123
  graph = create_graph()
124
  # Run the graph
125
+ """
126
+ print(f"******** TEST NORMAL LLM CALL ********")
127
  question = "What is an elephant? "
128
  messages = [HumanMessage(content=question)]
129
  messages = graph.invoke({"messages": messages})
130
  for m in messages["messages"]:
131
  m.pretty_print()
132
 
133
+ print(f"******** TESTING MEANING OF LIFE TOOL ********")
134
+ question = "What is meaning of life 10+10?"
135
+ messages = [HumanMessage(content=question)]
136
+ messages = graph.invoke({"messages": messages})
137
+ for m in messages["messages"]:
138
+ m.pretty_print()
139
+
140
+ """
141
+ print("******** TESTING WIKEPEDIA TOOL ********")
142
+ # expected answer is "Samuel"
143
+ question = "Search Wikipedia and find out who is the recipient of the malko competition in 2024"
144
  messages = [HumanMessage(content=question)]
145
  messages = graph.invoke({"messages": messages})
146
  for m in messages["messages"]:
requirements.txt CHANGED
@@ -7,3 +7,4 @@ langchain-core
7
  langchain-community
8
  langchain-openai
9
  langgraph-cli[inmem]
 
 
7
  langchain-community
8
  langchain-openai
9
  langgraph-cli[inmem]
10
+ wikipedia