{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from typing import Any, Dict\n", "\n", "from langchain.agents import AgentType, initialize_agent\n", "from langchain.llms import OpenAI\n", "from langchain.tools.requests.tool import RequestsGetTool, TextRequestsWrapper\n", "from pydantic import BaseModel, Field, root_validator" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(temperature=0)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import tldextract\n", "\n", "_APPROVED_DOMAINS = {\n", " \"langchain\",\n", " \"wikipedia\",\n", "}\n", "\n", "class ToolInputSchema(BaseModel):\n", "\n", " url: str = Field(...)\n", " \n", " @root_validator\n", " def validate_query(cls, values: Dict[str, Any]) -> Dict:\n", " url = values[\"url\"]\n", " domain = tldextract.extract(url).domain\n", " if domain not in _APPROVED_DOMAINS:\n", " raise ValueError(f\"Domain {domain} is not on the approved list:\"\n", " f\" {sorted(_APPROVED_DOMAINS)}\")\n", " return values\n", " \n", "tool = RequestsGetTool(args_schema=ToolInputSchema, requests_wrapper=TextRequestsWrapper())\n", "# tool = RequestsGetTool( requests_wrapper=TextRequestsWrapper())" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "agent = initialize_agent([tool], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=False)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The main title on langchain.com is \"LANG CHAIN 🦜️🔗 Official Home Page\".\n" ] } ], "source": [ "# This will succeed, since there aren't any arguments that will be triggered during validation\n", "answer = agent.run(\"What's the main title on langchain.com?\")\n", "print(answer)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }