-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
35 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import os | ||
import pprint | ||
|
||
from langchain_core.tools import Tool | ||
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper | ||
from dotenv import load_dotenv, find_dotenv | ||
|
||
_ = load_dotenv(find_dotenv()) | ||
from langchain_core.tools import tool | ||
from langchain.pydantic_v1 import BaseModel, Field | ||
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper | ||
from dotenv import load_dotenv | ||
|
||
serper_api_key = os.getenv('SERPER_API_KEY') | ||
load_dotenv() | ||
os.environ["SERPER_API_KEY"] = os.getenv('SERPER_API_KEY') | ||
|
||
search = GoogleSerperAPIWrapper(serper_api_key = serper_api_key) | ||
class SerperTool(BaseModel): | ||
query: str = Field(description = "This should be a search query") | ||
|
||
serper_tool = Tool( | ||
name = "Intermediate Answer", | ||
func = search.run, | ||
description="useful for when you need to ask with search" | ||
) | ||
@tool("serper_tool_main", args_schema=SerperTool, return_direct=False) | ||
def serper_tool(query:str) -> str: | ||
"""A useful for when you need to ask with search. Very useful when recent or specific information is needed from the web | ||
""" | ||
search = GoogleSerperAPIWrapper(k=4, type="search") | ||
initial_result = search.results(query) | ||
result = initial_result['organic'] | ||
results = "" | ||
for r in result: | ||
data = f"'Title':{r['title']}\n 'content':{r['snippet']}" | ||
results += f"{data}\n\n" | ||
return results |