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