Spaces:
Sleeping
Sleeping
File size: 2,371 Bytes
f420e0c 8b78e2a f420e0c 8b78e2a f420e0c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_community.tools import WikipediaQueryRun\n",
"from langchain_community.utilities import WikipediaAPIWrapper\n",
"from langchain_community.tools import DuckDuckGoSearchResults\n",
"from langchain_community.tools.yahoo_finance_news import YahooFinanceNewsTool\n",
"from langchain_core.messages import HumanMessage\n",
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.prebuilt import create_react_agent\n",
"from langchain_core.tools import tool"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create the agent\n",
"memory = MemorySaver()\n",
"llm = ChatOpenAI(model_name=\"gpt-4o-mini\", temperature=0)\n",
"tool_search = DuckDuckGoSearchResults(max_results=2)\n",
"tool_finance = YahooFinanceNewsTool()\n",
"tool_wiki = WikipediaQueryRun(\n",
" api_wrapper=WikipediaAPIWrapper(\n",
" top_k_results=2,\n",
" doc_content_chars_max=500))\n",
"@tool\n",
"def tool_mult(a: int, b: int) -> int:\n",
" 'Multiplies two given numbers'\n",
" return a * b\n",
"tools = [tool_search, tool_finance, tool_wiki, tool_mult]\n",
"agent_executor = create_react_agent(\n",
" llm,\n",
" tools,\n",
" checkpointer = memory\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# use the agent\n",
"config = {'configurable': {'thread_id': '1'}}\n",
"user_input = input(\"User: \")\n",
"for chunk in agent_executor.stream(\n",
" {\"messages\": [HumanMessage(content=user_input)]},\n",
" config):\n",
" print(chunk)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "langchain_311",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|