kostasang commited on
Commit
0e2dfdd
·
verified ·
1 Parent(s): 0855646

Upload tools.py

Browse files
Files changed (1) hide show
  1. src/tools.py +25 -0
src/tools.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  from langchain_core.tools import tool
2
 
3
 
@@ -60,3 +63,25 @@ def modulus(a: int, b: int) -> int:
60
  b: second int
61
  """
62
  return a % b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ from langchain_community.document_loaders import WikipediaLoader
4
  from langchain_core.tools import tool
5
 
6
 
 
63
  b: second int
64
  """
65
  return a % b
66
+
67
+
68
+ @tool
69
+ def wiki_search(query: str) -> str:
70
+ """
71
+ Search Wikipedia for a query and return maximum 2 results. The results
72
+ are formatted as a JSON string with the source and content of each
73
+ document.
74
+
75
+ Args:
76
+ query: The search query."""
77
+ search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
78
+ formatted_search_docs = "\n\n---\n\n".join(
79
+ [
80
+ {
81
+ "source": doc.metadata["source"],
82
+ "page": doc.metadata.get("page", ""),
83
+ "content": doc.page_content,
84
+ }
85
+ for doc in search_docs
86
+ ])
87
+ return json.dumps({"wiki_results": formatted_search_docs})