PageSourceSearch

https://innovationlab.fetch.ai/resources/assets/js/c05a7a01.f58d5995.js

js fetch.ai collected 2026-09-24 09:38:30 UTC 17,429 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkinnovation_labs=self.webpackChunkinnovation_labs||[]).push([[693],{57751:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>d,frontMatter:()=>a,metadata:()=>s,toc:()=>c});const s=JSON.parse('{"id":"examples/asione/asi-langchain-tavily","title":"ASI1-mini LangChain & Tavily Search Integration Guide","description":"This guide demonstrates how to integrate the ASI1-mini API with LangChain and leverage the Tavily Search tool to process search queries in a streamlined manner. The project is encapsulated in a single file that implements a custom LangChain LLM and integrates it with an agent chain to combine API responses with dynamic search results.","source":"@site/versioned_docs/version-1.0.4/examples/asione/asi1-langchain-tavily.md","sourceDirName":"examples/asione","slug":"/examples/asione/asi-langchain-tavily","permalink":"/resources/docs/1.0.4/examples/asione/asi-langchain-tavily","draft":false,"unlisted":false,"tags":[],"version":"1.0.4","frontMatter":{"id":"asi-langchain-tavily","title":"ASI1-mini LangChain & Tavily Search Integration Guide"},"sidebar":"tutorialSidebar","previous":{"title":"DeFi AI Agent Starter Guide","permalink":"/resources/docs/1.0.4/examples/asione/asi-defi-ai-agent"},"next":{"title":"Creating ASI1 Compatible uAgent","permalink":"/resources/docs/1.0.4/examples/chat-protocol/asi-compatible-uagents"}}');var t=i(74848),r=i(28453);const a={id:"asi-langchain-tavily",title:"ASI1-mini LangChain & Tavily Search Integration Guide"},o="ASI1-mini LangChain & Tavily Search Integration Guide",l={},c=[{value:"Overview",id:"overview",level:2},{value:"Prerequisites",id:"prerequisites",level:2},{value:"Project Structure",id:"project-structure",level:2},{value:"Script Breakdown",id:"script-breakdown",level:2},{value:"GitHub Repository",id:"github-repository",level:2}];function h(e){const n={a:"a",admonition:"admonition",br:"br",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.header,{children:(0,t.jsx)(n.h1,{id:"asi1-mini-langchain--tavily-search-integration-guide",children:"ASI1-mini LangChain & Tavily Search Integration Guide"})}),"\n",(0,t.jsxs)(n.p,{children:["This guide demonstrates how to integrate the ASI1-mini API with LangChain and leverage the Tavily Search tool to process search queries in a streamlined manner. The project is encapsulated in a single file that implements a custom LangChain ",(0,t.jsx)(n.code,{children:"LLM"})," and integrates it with an agent chain to combine API responses with dynamic search results."]}),"\n",(0,t.jsx)(n.h2,{id:"overview",children:"Overview"}),"\n",(0,t.jsx)(n.p,{children:"This project showcases an integration system built on the following key components:"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Custom LLM Integration:"}),(0,t.jsx)(n.br,{}),"\n","Implements a custom LangChain ",(0,t.jsx)(n.code,{children:"LLM"})," that sends user prompts to the ASI1-mini API using a defined JSON payload."]}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Tavily Search Tool:"}),(0,t.jsx)(n.br,{}),"\n","Uses the Tavily Search API to fetch search results, which are then incorporated into the agent chain to enhance the response."]}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Agent Chain Execution:"}),(0,t.jsx)(n.br,{}),"\n","Sets up an agent chain that processes search queries, calls the ASI1-mini API, and returns a combined result."]}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Environment-Based Configuration:"}),(0,t.jsx)(n.br,{}),"\n","Manages API keys and sensitive configurations through environment variables loaded from a ",(0,t.jsx)(n.code,{children:".env"})," file."]}),"\n"]}),"\n"]}),"\n",(0,t.jsx)(n.h2,{id:"prerequisites",children:"Prerequisites"}),"\n",(0,t.jsx)(n.p,{children:"Before running this project, ensure you have the following:"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Python:"})," Version 3.8 or higher."]}
1),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Required Python Packages:"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"pip install requests pydantic python-dotenv langchain\n"})}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Environment Variables:"})}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["A valid API key for ASI1. Obtain your API Key ",(0,t.jsx)(n.a,{href:"https://asi1.ai/dashboard/api-keys",children:"here"}),"."]}),"\n",(0,t.jsxs)(n.li,{children:["A valid API key for Tavily. Obtain your API Key ",(0,t.jsx)(n.a,{href:"https://app.tavily.com/home#",children:"here"}),"."]}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["Create a ",(0,t.jsx)(n.code,{children:".env"})," file in the project directory with the following keys:\n",(0,t.jsx)(n.code,{children:"ASI_LLM_KEY=<asi1-api_key>"}),"\n",(0,t.jsx)(n.code,{children:"TAVILY_API_KEY=<tavily_api_key>"})]}),"\n"]}),"\n"]}),"\n",(0,t.jsx)(n.h2,{id:"project-structure",children:"Project Structure"}),"\n",(0,t.jsx)(n.p,{children:"The entire integration is contained within a single file:"}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"ASI_Langchain.py"}),"  # Contains the custom LLM class and the search handler integration"]}),"\n",(0,t.jsx)(n.h2,{id:"script-breakdown",children:"Script Breakdown"}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"1. Importing Required Libraries"})}),"\n",(0,t.jsx)(n.p,{children:"The script begins by importing the necessary modules:"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"os"}),": To get environment variables."]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"requests:"})," To perform HTTP requests to the ASI-1 Mini API."]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"typing:"})," For getting Python types."]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"pydantic:"})," To define pydantic data models required by Langchain."]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"langchain:"})," Imports required by Langchain."]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"langchain_community:"})," Imports required to use the TavilySearch tool."]}),"\n"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-python",children:"import os\nimport requests\nfrom typing import Optional, List\nfrom pydantic import Field\nfrom langchain.llms.base import LLM\nfrom langchain_community.utilities.tavily_search import TavilySearchAPIWrapper\nfrom langchain.agents import initialize_agent, AgentType\nfrom langchain_community.tools.tavily_search.tool import TavilySearchResults\nfrom dotenv import load_dotenv\n"})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"2. Defining the ASI-1 Mini LLM Class"})}),"\n",(0,t.jsx)(n.p,{children:"Defines a custom LangChain LLM that sends prompts to the ASI1-mini API. It supports parameters such as temperature, fun mode, and web search, and handles API responses by extracting the relevant message content."}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-python",children:'class ASI1MINI(LLM):\n    api_key: str = Field(...)\n    api_url: str = Field(...)\n    model: str = Field(default="asi1-mini")\n    temperature: float = Field(default=0.7)\n    fun_mode: bool = Field(default=False)\n    web_search: bool = Field(default=False)\n    enable_stream: bool = Field(default=False)\n    max_tokens: int = Field(default=1024)\n\n    @property\n    def _llm_type(self) -> str:\n        return "custom_llm"\n\n    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:\n        headers = {\n            "Authorization": f"Bearer {self.api_key}",\n            "Content-Type": "application/json",\n        }\n        payload = {\n            "model": self.model,\n            "messages": [{"role": "user", "content": prompt}],\n            "temperature": self.temperature,\n            "fun_mode": self.fun_mode,\n            "web_search": self.web_search,\n            "stream": self.enable_stream,\n            "max_tokens": self.max_tokens,\n        }\n        if stop:\n            payload["stop"] = stop\n\n        response = requests.post(self.api_url, headers=headers, json=payload)\n        response.raise_for_status()\n        response_data = response.json()\n        return (\n            response_data.get("choices", [{}])[0].get("message", {}).get("content", "")\n        )\n\n\n'})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"3. Initializing the Agent"})}),"\n",(0,t.jsxs)(n.p,{children:["The agent is defined in the ",(0,t.jsx)(n.code,{children:"custom_search_handler"})," function."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-python",children:'def custom_search_handler(data):\n    """\n    Uses LangChain to process a search query with the custom LLM.\n    Expects a JSON payload with the key "search_query" and returns the result.\n    """\n    search_query = data.get("search_query")\n    if not search_query:\n        return {"error": "Missing search query"}\n\n    custom_api_key = os.getenv("ASI_LLM_KEY")\n    custom_api_url = "https://api.asi1.ai/v1/chat/completions"\n    tavily_api_key = os.getenv("TAVILY_API_KEY")\n    print("1: ", custom_api_key)\n    print("2: ", custom_api_url)\n    print("3: ", tavily_api_key)\n\n    if not custom_api_key or not custom_api_url or not tavily_api_key:\n        return {"error": "Missing API keys"}\n\n    try:\n        # Initialize your custom LLM\n        llm = ASI1MINI(api_key=custom_api_key, api_url=custom_api_url, temperature=0.7)\n        # Initialize the Tavily search tool\n        search = TavilySearchAPIWrapper()\n        tavily_tool = TavilySearchResults(\n            api_wrapper=search, tavily_api_key=tavily_api_key\n        )\n\n        # Initialize the agent with your custom LLM and Tavily search tool\n        agent_chain = initialize_agent
1(\n            [tavily_tool],\n            llm,\n            agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n            verbose=True,\n        )\n        # Run the agent chain with the search query\n        result = agent_chain.invoke({"input": search_query})\n        return {"result": result}\n    except Exception as e:\n        return {"error": str(e)}\n'})}),"\n",(0,t.jsx)(n.h1,{id:"running-the-system",children:"Running the System"}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"1. Activate Your Virtual Environment:"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"source venv/bin/activate   # On Windows: venv\\Scripts\\activate\n"})}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"2. Run the Script:"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"python ASI_Langchain.py\n"})}),"\n",(0,t.jsx)(n.h1,{id:"sample-outputs",children:"Sample Outputs"}),"\n",(0,t.jsxs)(n.ol,{children:["\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Factual question known by the LLM(Does not use the Tavily tool)"})}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Input:"})," ",(0,t.jsx)(n.code,{children:"How tall is the Eiffel tower?"})]}),"\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Final Output:"})}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"{'result': 'The Eiffel Tower is approximately 330 meters (1,083 feet) tall, including its antennas.'}\n"})}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Current news not known by the LLM (Uses the Tavily tool)"})}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Input:"})," ",(0,t.jsx)(n.code,{children:"Nvidia company news?"})]}),"\n",(0,t.jsx)(n.p,{children:"** Final Output:**"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"{'result': \"Here are some recent updates on NVIDIA:\\n1. **GTC 2025 Announcement**: NVIDIA\u2019s premier AI conference will take place from March 17-21, 2025, in San Jose, California, featuring advancements in agentic AI and RTX AI tools.\\n2. **New Product Launch**: The NVIDIA GeForce RTX 5070 Ti, built on the Blackwell architecture, is now available, boosting generative AI content creation and creative workflows.\\n3. **AI Platform Advancements**: NVIDIA has unveiled the Rubin AI platform, set for 2026, and introduced the largest publicly available AI model for genomic data using DGX Cloud.\\n4. **Stock Performance**: After a 27% decline over three weeks, Nvidia stock is attempting a rebound, supported by positive analyst reports.\\nFor more details, you can visit NVIDIA's official newsroom or recent financial updates.\"}\n"})}),"\n"]}),"\n"]}),"\n",(0,t.jsx)(n.h1,{id:"troubleshooting",children:"Troubleshooting"}),"\n",(0,t.jsxs)(n.ol,{children:["\n",(0,t.jsx)(n.li,{children:(0,t.jsx)(n.strong,{children:"Environment Variables"})}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["Ensure that both ",(0,t.jsx)(n.code,{children:"ASI_LLM_KEY"})," and ",(0,t.jsx)(n.code,{children:"TAVILY_API_KEY"})," are correctly defined in your ",(0,t.jsx)(n.code,{children:".env"})," file."]}),"\n",(0,t.jsx)(n.p,{children:"Missing or incorrect API keys will lead to errors.\nAPI Connectivity"}),"\n",(0,t.jsxs)(n.p,{children:["Verify that the ASI1-mini API endpoint (",(0,t.jsx)(n.a,{href:"https://api.asi1.ai/v1/chat/completions",children:"https://api.asi1.ai/v1/chat/completions"}),") is accessible."]}),"\n",(0,t.jsx)(n.p,{children:"Confirm that the Tavily Search API is operational and that your API key is valid."}),"\n",(0,t.jsx)(n.h1,{id:"debugging",children:"Debugging"}),"\n",(0,t.jsx)(n.p,{children:"The code includes debug print statements (e.g., printing API responses) to help trace issues with API calls or response handling.\nReview the console output to diagnose any problems during execution."}),"\n",(0,t.jsx)(n.h1,{id:"benefits-of-this-integration",children:"Benefits of This Integration"}),"\n",(0,t.jsxs)(n.ol,{children:["\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Seamless API Communication:"})}),"\n",(0,t.jsx)(n.p,{children:"Directly integrates with the ASI1-mini API via a custom LangChain LLM."}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Enhanced Search Capabilities:"})}),"\n",(0,t.jsx)(n.p,{children:"Enriches responses by combining LLM outputs with real-time search results using Tavily Search."}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Configurable Parameters:"})}),"\n",(0,t.jsx)(n.p,{children:"Offers flexibility through parameters like temperature, fun mode, and maximum tokens."}),"\n"]}),"\n",(0,t.jsxs)(n.li,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.strong,{children:"Simplified Deployment:"})}),"\n",(0,t.jsx)(n.p,{children:"The single-file integration simplifies setup and deployment, making it easy to incorporate into larger projects."}),"\n"]}),"\n"]}),"\n",(0,t.jsx)(n.h1,{id:"additional-resources",children:"Additional Resources"}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"ASI1-mini API Documentation"}),"(",(0,t.jsx)(n.a,{href:"https://docs.asi1.ai",children:"https://docs.asi1.ai"}),")"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"LangChain GitHub Repository"}),"(",(0,t.jsx)(n.a,{href:"https://python.langchain.com/docs/introduction/",children:"https://python.langchain.com/docs/introduction/"}),")"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.strong,{children:"Tavily Search Tool Documentation"}),"(",(0,t.jsx)(n.a,{href:"https://docs.tavily.com/welcome",children:"https://docs.tavily.com/welcome"}),")"]}),"\n"]}),"\n",(0,t.jsx)(n.h2,{id:"github-repository",children:"GitHub Repository"}),"\n",(0,t.jsxs)(n.p,{children:["For the complete code, visit the ",(0,t.jsx)(n.a,{href:"https://github.com/abhifetch/ASI-1_mini_Langchain",children:"ASI1 Chat System Repository"}),"."]}),"\n",(0,t.jsx)(n.admonition,{type:"note",children:(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.strong,{children:"Note:"})," You can learn more about ASI1 Mini APIs ",(0,t.jsx)(n.a,{href:"https://docs.asi1.ai/docs/",children:(0,t.jsx)(n.strong,{children:"here"})}),"."]})})]})}function d(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(h,{...e})}):h(e)}},28453:(e,n,i)=>{i.d(n,{R:()=>a,x:()=>o});var s=i(96540);const t={},r=s.createContext(t);function a(e){const n=s.useContext(r);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:a(e.components),s.createElement(r.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.