Last active
March 22, 2024 15:18
-
-
Save hustshawn/80810688b8c3b3ad5dd39da378f6ce7c to your computer and use it in GitHub Desktop.
LangChain agent with Google Search and Math Capability
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
from langchain import hub | |
from langchain.agents import AgentExecutor, load_tools | |
from langchain.agents.format_scratchpad import format_log_to_str | |
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser | |
from langchain.tools.render import render_text_description | |
from langchain_community.llms.bedrock import Bedrock | |
from langchain_community.chat_models.bedrock import BedrockChat | |
import dotenv | |
dotenv.load_dotenv() | |
llm = Bedrock( | |
credentials_profile_name='us-east-1', | |
model_id='anthropic.claude-v2:1', | |
) | |
chat_model = BedrockChat( | |
credentials_profile_name='us-east-1', | |
model_id='anthropic.claude-v2:1' | |
) | |
tools = load_tools(["serpapi", "llm-math"], llm=llm) | |
prompt = hub.pull("hwchase17/react-json") | |
prompt = prompt.partial( | |
tools=render_text_description(tools), | |
tool_names=", ".join([t.name for t in tools]), | |
) | |
chat_model_with_stop = chat_model.bind( | |
stop=["\n\nHuman:"] | |
) | |
agent = ( | |
{ | |
"input": lambda x: x["input"], | |
"agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]), | |
} | |
| prompt | |
| chat_model_with_stop | |
| ReActJsonSingleInputOutputParser() | |
) | |
agent_executor = AgentExecutor( | |
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True) | |
agent_executor.invoke( | |
{ | |
"input": "Which city has a larger population, Guiyang or Tacheng?" | |
} | |
# { | |
# "input": "what's the smallest prime number greater than 1000?" | |
# } | |
# { | |
# "input": "The Japanese military officer Kenji Hatanaka conspired to prevent the broadcast of the surrender of this emperor who passed away a few decades later and was succeeded by whom?" | |
# } | |
# { | |
# "input": "who is the F1 World Champion in 2023? what's his age raised to the 0.43 power?" | |
# } | |
) |
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
aiohttp==3.9.2 | |
aiosignal==1.3.1 | |
annotated-types==0.6.0 | |
anyio==4.2.0 | |
appnope==0.1.3 | |
asttokens==2.4.1 | |
attrs==23.2.0 | |
boto3==1.34.34 | |
botocore==1.34.34 | |
certifi==2023.11.17 | |
charset-normalizer==3.3.2 | |
click==8.1.7 | |
comm==0.2.1 | |
dataclasses-json==0.6.3 | |
debugpy==1.8.0 | |
decorator==5.1.1 | |
Deprecated==1.2.14 | |
distro==1.9.0 | |
executing==2.0.1 | |
filelock==3.13.1 | |
frozenlist==1.4.1 | |
fsspec==2023.12.2 | |
google_search_results==2.4.2 | |
greenlet==3.0.3 | |
h11==0.14.0 | |
httpcore==1.0.2 | |
httpx==0.26.0 | |
huggingface-hub==0.20.3 | |
idna==3.6 | |
ipykernel==6.29.0 | |
ipython==8.20.0 | |
jedi==0.19.1 | |
Jinja2==3.1.3 | |
jmespath==1.0.1 | |
joblib==1.3.2 | |
jsonpatch==1.33 | |
jsonpointer==2.4 | |
jupyter_client==8.6.0 | |
jupyter_core==5.7.1 | |
langchain==0.1.5 | |
langchain-community==0.0.17 | |
langchain-core==0.1.18 | |
langchain-openai==0.0.5 | |
langchainhub==0.1.14 | |
langgraph==0.0.21 | |
langsmith==0.0.86 | |
llama-index==0.9.39 | |
MarkupSafe==2.1.5 | |
marshmallow==3.20.2 | |
matplotlib-inline==0.1.6 | |
multidict==6.0.4 | |
mypy-extensions==1.0.0 | |
nest-asyncio==1.6.0 | |
networkx==3.2.1 | |
nltk==3.8.1 | |
numexpr==2.9.0 | |
numpy==1.26.3 | |
openai==1.10.0 | |
packaging==23.2 | |
pandas==2.2.0 | |
parso==0.8.3 | |
pexpect==4.9.0 | |
platformdirs==4.1.0 | |
prompt-toolkit==3.0.43 | |
psutil==5.9.8 | |
ptyprocess==0.7.0 | |
pure-eval==0.2.2 | |
pydantic==2.5.3 | |
pydantic_core==2.14.6 | |
Pygments==2.17.2 | |
python-dateutil==2.8.2 | |
python-dotenv==1.0.1 | |
pytz==2023.4 | |
PyYAML==6.0.1 | |
pyzmq==25.1.2 | |
regex==2023.12.25 | |
requests==2.31.0 | |
s3transfer==0.10.0 | |
safetensors==0.4.2 | |
six==1.16.0 | |
sniffio==1.3.0 | |
SQLAlchemy==2.0.25 | |
stack-data==0.6.3 | |
tavily-python==0.3.1 | |
tenacity==8.2.3 | |
tiktoken==0.5.2 | |
tokenizers==0.15.1 | |
tornado==6.4 | |
tqdm==4.66.1 | |
traitlets==5.14.1 | |
transformers==4.37.2 | |
types-requests==2.31.0.20240125 | |
typing-inspect==0.9.0 | |
typing_extensions==4.9.0 | |
tzdata==2023.4 | |
urllib3==2.0.7 | |
wcwidth==0.2.13 | |
wrapt==1.16.0 | |
yarl==1.9.4 |
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
from langchain import hub | |
from langchain.agents import AgentExecutor, load_tools | |
from langchain.agents.format_scratchpad import format_log_to_str | |
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser | |
from langchain.tools.render import render_text_description | |
from langchain_community.llms.bedrock import Bedrock | |
from langchain_community.chat_models.bedrock import BedrockChat | |
import dotenv | |
dotenv.load_dotenv() | |
llm = Bedrock( | |
credentials_profile_name='us-east-1', | |
model_id='anthropic.claude-v2:1', | |
) | |
chat_model = BedrockChat( | |
credentials_profile_name='us-east-1', | |
model_id='anthropic.claude-v2:1' | |
) | |
tools = load_tools(["serpapi", "llm-math"], llm=llm) | |
prompt = hub.pull("hwchase17/react-json") | |
prompt = prompt.partial( | |
tools=render_text_description(tools), | |
tool_names=", ".join([t.name for t in tools]), | |
) | |
chat_model_with_stop = chat_model.bind( | |
stop=["\n\nHuman:"] | |
) | |
agent = ( | |
{ | |
"input": lambda x: x["input"], | |
"agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]), | |
} | |
| prompt | |
| chat_model_with_stop | |
| ReActJsonSingleInputOutputParser() | |
) | |
agent_executor = AgentExecutor( | |
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True) | |
agent_executor.invoke( | |
{ | |
"input": "how much is 27*313*289?" | |
} | |
# { | |
# "input": "Which city has a larger population, Guiyang or Tacheng?" | |
# } | |
# { | |
# "input": "what's the smallest prime number greater than 1000?" | |
# } | |
# { | |
# "input": "The Japanese military officer Kenji Hatanaka conspired to prevent the broadcast of the surrender of this emperor who passed away a few decades later and was succeeded by whom?" | |
# } | |
# { | |
# "input": "who is the F1 World Champion in 2023? what's his age raised to the 0.43 power?" | |
# } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output