Moss Tool Example
This notebook demonstrates how to use the MossToolSpec with LlamaIndex.
Installation
Section titled “Installation”First, you need to install the package (and LlamaIndex core if not present).
%pip install llama-index-tools-moss llama-index-core llama-index-llms-openaiImport the necessary classes and initialize the Moss client.
import osfrom llama_index.tools.moss import MossToolSpec, QueryOptionsfrom inferedge_moss import MossClient, DocumentInfo
# Initialize Moss ClientMOSS_PROJECT_KEY = os.getenv("MOSS_PROJECT_KEY")MOSS_PROJECT_ID = os.getenv("MOSS_PROJECT_ID")client = MossClient(project_id=MOSS_PROJECT_ID, project_key=MOSS_PROJECT_KEY)
# 2. Configure query settings - Instantiate QueryOptions (Optional)# If skipped, the tool will use its own defaults.query_options = QueryOptions(top_k=3, alpha=0.5)# 3. Initialize Toolprint("Initializing MossToolSpec...")moss_tool = MossToolSpec( client=client, index_name="moss-cookbook", query_options=query_options)Initializing MossToolSpec...Indexing Data
Section titled “Indexing Data”Let’s add some sample data to our index.
docs = [ DocumentInfo( id="123", text="LlamaIndex connects your data to LLMs.", metadata={"topic": "AI"}, ), DocumentInfo( id="456", text="Moss provides fast semantic search integration.", metadata={"topic": "Search"}, ),]
# Index the documentsawait moss_tool.index_docs(docs)await moss_tool._load_index()Using with an Agent
Section titled “Using with an Agent”Now we can wrap the tool for use with a LlamaIndex agent.
from llama_index.core.agent import ReActAgentfrom llama_index.llms.openai import OpenAI
# Convert to tool listtools = moss_tool.to_tool_list()
# Create an agent (Using OpenAI llm for demonstration)# os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')llm = OpenAI(model="gpt-4.1-mini", api_key=api_key)agent = ReActAgent(tools=tools, llm=llm, verbose=True)
# Chat with the agentresponse = await agent.run(user_msg="What is your return policy?")print(response)Generally, a return policy allows customers to return products within a specified period, usually 30 days, for a refund, exchange, or store credit, provided the items are in their original condition and packaging. However, specific return policies can vary by company, so it's best to check the exact terms on the company's website or contact their customer service for detailed information.