Spaces:
Runtime error
Runtime error
File size: 18,356 Bytes
115169a 82df0a3 115169a 82df0a3 115169a 7118dfb 82df0a3 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 7118dfb 115169a 82df0a3 115169a 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 82df0a3 7118dfb 115169a 7118dfb 115169a |
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
{
"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
}
|