{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import utils\n", "\n", "utils.load_env()\n", "os.environ['LANGCHAIN_TRACING_V2'] = \"true\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n", " warn_deprecated(\n", "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The function `format_tool_to_openai_function` was deprecated in LangChain 0.1.16 and will be removed in 1.0. Use langchain_core.utils.function_calling.convert_to_openai_function() instead.\n", " warn_deprecated(\n" ] } ], "source": [ "from langchain_core.messages import HumanMessage\n", "\n", "# for llm model\n", "from langchain_openai import ChatOpenAI\n", "from langchain.agents.format_scratchpad import format_to_openai_function_messages\n", "from tools import find_place_from_text, nearby_search\n", "from typing import Dict, List, Tuple\n", "from langchain.agents import (\n", " AgentExecutor,\n", ")\n", "from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser\n", "from langchain_community.chat_models import ChatOpenAI\n", "from langchain_community.tools.convert_to_openai import format_tool_to_openai_function\n", "from langchain_core.messages import AIMessage, HumanMessage\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "\n", "# Bind the tools to the model\n", "tools = [find_place_from_text, nearby_search] # Include both tools if needed\n", "\n", "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n", "\n", "llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])\n", "\n", "def _format_chat_history(chat_history: List[Tuple[str, str]]):\n", " buffer = []\n", " for human, ai in chat_history:\n", " buffer.append(HumanMessage(content=human))\n", " buffer.append(AIMessage(content=ai))\n", " return buffer\n", "\n", "meta = utils.load_agent_meta()[0]\n", "\n", "prompt = ChatPromptTemplate.from_messages(\n", " [\n", " (\"system\", meta['prompt']),\n", " MessagesPlaceholder(variable_name=\"chat_history\"),\n", " (\"user\", \"{input}\"),\n", " MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n", " ]\n", ")\n", "\n", "agent = (\n", " {\n", " \"input\": lambda x: x[\"input\"],\n", " \"chat_history\": lambda x: _format_chat_history(x[\"chat_history\"]),\n", " \"agent_scratchpad\": lambda x: format_to_openai_function_messages(\n", " x[\"intermediate_steps\"]\n", " ),\n", " }\n", " | prompt\n", " | llm_with_tools\n", " | OpenAIFunctionsAgentOutputParser()\n", ")\n", "\n", "\n", "class AgentInput(BaseModel):\n", " input: str\n", " chat_history: List[Tuple[str, str]] = Field(\n", " ..., extra={\"widget\": {\"type\": \"chat\", \"input\": \"input\", \"output\": \"output\"}}\n", " )\n", "\n", "\n", "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).with_types(\n", " input_type=AgentInput\n", ")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32;1m\u001b[1;3m\n", "Invoking: `nearby_search` with `{'input_dict': {'keyword': 'ร้านกาแฟ', 'location_name': 'มาบุญครอง', 'radius': 1000, 'place_type': 'cafe'}}`\n", "\n", "\n", "\u001b[0m" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n", "Unable to load requested LangChainTracer. To disable this warning, unset the LANGCHAIN_TRACING_V2 environment variables.\n", "LangSmithUserError('API key must be provided when using hosted LangSmith API')\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33;1m\u001b[1;3mnumber of results more than 20\n", "\n", " address: อาคารหอศิลปวัฒนธรรมแห่งกรุงเทพมหานคร(BACC) 939 Rama I Rd, Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7466896, 'lng': 100.5304572}\n", "\n", " name: Gallery Drip Coffee\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.6\n", "\n", " plus code: 7P52PGWJ+M5\n", "\n", "\n", " \n", " address: 445 Rama I Rd, Rong Muang, Pathum Wan\n", "\n", " location: {'lat': 13.7476317, 'lng': 100.5232939}\n", "\n", " name: D Coffee House\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52PGXF+38\n", "\n", "\n", " \n", " address: 43/4 Rama VI Rd, Rong Muang, Pathum Wan\n", "\n", " location: {'lat': 13.7497223, 'lng': 100.521221}\n", "\n", " name: Olympic Coffee\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.8\n", "\n", " plus code: 7P52PGXC+VF\n", "\n", "\n", " \n", " address: 36, 6 Kasem San 1 Alley, Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7477989, 'lng': 100.5296324}\n", "\n", " name: Sip 'n Drip\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52PGXH+4V\n", "\n", "\n", " \n", " address: 27 ซอย จุฬาลงกรณ์ 20 Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7408514, 'lng': 100.5230725}\n", "\n", " name: mm : MAKE MERRY\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.8\n", "\n", " plus code: 7P52PGRF+86\n", "\n", "\n", " \n", " address: 2nd Floor Siam Square One 388 Rama I Rd, Pathum Wan\n", "\n", " location: {'lat': 13.7446247, 'lng': 100.5340785}\n", "\n", " name: GATTA CAFé\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52PGVM+RJ\n", "\n", "\n", " \n", " address: MINT TOWER Bantadthong Phetchaburi Rd, Subdistrict Pathum Wan\n", "\n", " location: {'lat': 13.7450255, 'lng': 100.5235735}\n", "\n", " name: Treasure specialty coffee\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52PGWF+2C\n", "\n", "\n", " \n", " address: 991/1 Rama I Rd, Pathum Wan\n", "\n", " location: {'lat': 13.7458781, 'lng': 100.5341476}\n", "\n", " name: BEANS Coffee Roaster Paragon\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 5\n", "\n", " plus code: 7P52PGWM+9M\n", "\n", "\n", " \n", " address: 226 Siam Square Soi 7, Pathum Wan\n", "\n", " location: {'lat': 13.7448544, 'lng': 100.5317337}\n", "\n", " name: MONGKOL COFFEE X WARMBATCH ROASTERS\n", "\n", " opening hours: N/A\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52PGVJ+WM\n", "\n", "\n", " \n", " address: B106 เลขที่ 353, 355, Charoen Mueang Rd, Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7402072, 'lng': 100.5244642}\n", "\n", " name: Trentotto, Home of Coffee I'm Park\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.8\n", "\n", " plus code: 7P52PGRF+3Q\n", "\n", "\n", " \n", " address: 83 85 ซอยจุฬา 16 Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7419898, 'lng': 100.5236568}\n", "\n", " name: Chawarin\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52PGRF+QF\n", "\n", "\n", " \n", " address: 1/11 Rong Muang 5 Alley, Rong Muang, Pathum Wan\n", "\n", " location: {'lat': 13.7466928, 'lng': 100.5227848}\n", "\n", " name: Greatercafe Specialty Coffee Bangkok\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.7\n", "\n", " plus code: 7P52PGWF+M4\n", "\n", "\n", " \n", " address: 266/15 Rama I Rd, Pathum Wan\n", "\n", " location: {'lat': 13.7454978, 'lng': 100.5330408}\n", "\n", " name: Mongkol COFFEE\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.6\n", "\n", " plus code: 7P52PGWM+66\n", "\n", "\n", " \n", " address: 645, 47 Phetchaburi Rd, Thanon Phaya Thai, Ratchathewi\n", "\n", " location: {'lat': 13.7518404, 'lng': 100.5362577}\n", "\n", " name: 26y coffee\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52QG2P+PG\n", "\n", "\n", " \n", " address: 85 1 Akkharanithi Alley, Thanon Phaya Thai, Ratchathewi\n", "\n", " location: {'lat': 13.7537094, 'lng': 100.5328794}\n", "\n", " name: Bullet Thai Craft Beer\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 5\n", "\n", " plus code: 7P52QG3M+F5\n", "\n", "\n", " \n", " address: ชั้น จี, 999/9 Rama I Rd, Pathum Wan\n", "\n", " location: {'lat': 13.745689, 'lng': 100.5383407}\n", "\n", " name: Pacamara Coffee Roasters (The Offices at CentralwOrld)\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.4\n", "\n", " plus code: 7P52PGWQ+78\n", "\n", "\n", " \n", " address: Dragon Town ชั้น 2 เลขที่ 188/66 Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7407797, 'lng': 100.5245334}\n", "\n", " name: OKA x LOMNUA cafe & coffee roaster\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.7\n", "\n", " plus code: 7P52PGRF+8R\n", "\n", "\n", " \n", " address: 1800 ถนน บรรทัดทอง Wang Mai, Pathum Wan\n", "\n", " location: {'lat': 13.7389399, 'lng': 100.5221088}\n", "\n", " name: Bantatthong Cafe\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.8\n", "\n", " plus code: 7P52PGQC+HR\n", "\n", "\n", " \n", " address: 535 31 ตรอก Wat Phraya Yang Alley, Khwaeng Thanon Phetchaburi, Ratchathewi\n", "\n", " location: {'lat': 13.7513197, 'lng': 100.5264651}\n", "\n", " name: Piccolo Vicolo Café\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.7\n", "\n", " plus code: 7P52QG2G+GH\n", "\n", "\n", " \n", " address: 1, 50 Phetchaburi Rd, Thanon Phaya Thai, Ratchathewi\n", "\n", " location: {'lat': 13.7520417, 'lng': 100.5386537}\n", "\n", " name: MIND SLOWBAR (ประตูน้ำ เพชรบุรีซอย19)\n", "\n", " opening hours: {'open_now': True}\n", "\n", " rating: 4.9\n", "\n", " plus code: 7P52QG2Q+RF\n", "\n", "\n", " \u001b[0m" ] } ], "source": [ "def submitUserMessage(user_input: str) -> str:\n", " responce = agent_executor.invoke({\"input\": user_input, \"chat_history\": []})\n", " return responce[\"output\"]\n", "\n", "submitUserMessage(\"ค้นหาร้านกาแฟใกล้มาบุญครอง\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9" } }, "nbformat": 4, "nbformat_minor": 2 }