Using External Tools in Agent with Llamaindex

Overview

For Agents, it is common to call multiple external tools to achieve various functions. This article introduces how to use external tools through llamaindex. Of course, these tools are all provided by llamaindex. The framework offers several external tools that can infinitely expand the capabilities of the Agent. These tools can be downloaded from https://llamahub.ai/?tab=tools.

Similarly, this article uses a local large model to complete the experiment, running on a local machine with 16C32G.

It should be noted that the locally deployed large model via Ollama may not perform as well as ChatGPT-4.

Preparation

The external tool used for the text is llama-index-tools-database, which can be installed using the following command:

$ pip install llama-index-tools-database

Implementation Logic

  1. Create a local large model using Ollama

  2. Use DatabaseToolSpec to utilize external database tools

  3. Create an Agent and send instructions to the Agent. Through planning, the Agent will automatically call external tools to accomplish corresponding tasks.

# -*- coding: utf-8 -*-
#
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
from llama_index.core.agent import Task, AgentChatResponse
from typing import Dict, Any
from llama_index.core.query_pipeline import StatefulFnComponent
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core.agent import ReActAgent

local_model = "/opt/models/BAAI/bge-base-en-v1.5"
# local bge-base embedding model
Settings.embed_model = HuggingFaceEmbedding(model_name=local_model)

Settings.llm = Ollama(model="gemma2", request_timeout=360)
llm=Settings.llm 

from llama_index.core import SQLDatabase
from sqlalchemy import (
    create_engine,
    MetaData,
    Table,
    Column,
    String,
    Integer,
    select,
    column,
)

engine = create_engine("sqlite:///chinook.db")
sql_database = SQLDatabase(engine)

from llama_index.tools.database import DatabaseToolSpec

db_tools = DatabaseToolSpec(
    sql_database=sql_database
)

agent = ReActAgent.from_tools(db_tools.to_tool_list(), llm=llm, verbose=True)

agent.chat("What tables does this database contain")
agent.chat("Describe the first table")
agent.chat("Retrieve the first row of that table")

Output Results

It can be seen that the large model understood our goal and used external tools to complete our tasks.

 python agent_tools_2.py 
> Running step cc46278c-d946-40b8-a42a-c0ead10134ec. Step input: What tables does this database contain
Thought: The current language of the user is: English. I need to use a tool to help me answer the question.
Action: list_tables
Action Input: {}
Observation: ['artists', 'employees', 'genres', 'media_types', 'playlists', 'albums', 'customers', 'invoices', 'tracks', 'invoice_items', 'playlist_track']

> Running step a2208442-2646-4ff4-b91c-e8761d0fdb2f. Step input: None
Thought: I can answer without using any more tools. I'll use the user's language to answer
Answer: This database contains the following tables: artists, employees, genres, media_types, playlists, albums, customers, invoices, tracks, invoice_items, and playlist_track.
> Running step 95f9b7d2-2d15-48d3-8c62-f140ad180db9. Step input: Describe the first table
Thought: The user asked for a description of the first table in the database. I need to use the `describe_tables` tool with a list containing the name of the first table.
Action: describe_tables
Action Input: {'tables': ['artists']}
Observation: 
CREATE TABLE artists (
        "ArtistId" INTEGER NOT NULL, 
        "Name" NVARCHAR(120), 
        PRIMARY KEY ("ArtistId")
)

> Running step 1261af98-5048-4e55-a25d-4efbc6895a55. Step input: None
Thought: I can answer without using any more tools. I'll use the user's language to answer
Answer: The `artists` table has three columns:  `ArtistId`, which is an integer and the primary key, `Name`, which is a string with a maximum length of 120 characters.

> Running step ccf9386b-19d1-4c64-a2d0-4b1fad1f8f80. Step input: Retrieve the first row of that table
Thought: (Implicit) I can answer without any more tools!
Answer: ```json
{
 "query": "SELECT * FROM artists LIMIT 1"
}
```

From the above output, it can be seen that the large model correctly executed our requests.

Conclusion

By using external tools, the capabilities of the Agent can be greatly enhanced, allowing for infinite expansion of its abilities. We can also write our own tools according to the specifications.

Leave a Comment