Skip to content
⌘K

Moss Tool Example

This notebook demonstrates how to use the MossToolSpec with LlamaIndex.

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-openai

Import the necessary classes and initialize the Moss client.

import os
from llama_index.tools.moss import MossToolSpec, QueryOptions
from inferedge_moss import MossClient, DocumentInfo
# Initialize Moss Client
MOSS_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 Tool
print("Initializing MossToolSpec...")
moss_tool = MossToolSpec(
client=client, index_name="moss-cookbook", query_options=query_options
)
Initializing MossToolSpec...

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 documents
await moss_tool.index_docs(docs)
await moss_tool._load_index()

Now we can wrap the tool for use with a LlamaIndex agent.

from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
# Convert to tool list
tools = 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 agent
response = 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.