hi, i recently deployed my livekit voice agent to ...
# ask-ai
d
hi, i recently deployed my livekit voice agent to livekit cloud; my setup is a multi agent workflow with tool calls in the process. i’ve noticed the tool calls to write data to my mongo database doesn’t work on livekit cloud; when i run the agent locally in development, the tool calls work and i can save records to my database. due to the limited logging available for livekit cloud agents, its a bit difficult to troubleshoot why this is the case. are there any working examples of making external database calls in tools from livekit cloud agents? and how do i find that.
t
Thanks for reaching out, George. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you with using LiveKit to make external database calls through custom tools in agents! There are some great examples available for Python, including how to use SQLite with asynchronous functions to keep things running smoothly during I/O operations. For instance, the Personal Shopper Agent and Nutrition Assistant show how to create tools for tasks like retrieving order history or recording nutrition data. For Node.js, while there aren't specific database examples provided, you can still define tools using the LiveKit SDK and connect to databases with libraries like
pg
,
mysql2
, or
sqlite3
. You can securely pass credentials through job metadata or environment variables to keep everything safe. If you'd like, I’d be more than happy to guide you through implementing a specific database—say MongoDB—with either Python or Node.js in a LiveKit agent. Just let me know how I can support you! Sources: Tool definition and use | LiveKit Docs | complex-agents/personal_shopper/database.py | Tool definition and use | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs | External data and RAG | LiveKit Docs
d
sure, show me the example of a python agent using tools to connect to a remove mongodb or postgresql database
t
I'm happy to help you with connecting LiveKit agents to external databases like MongoDB or PostgreSQL using custom tools. While there aren't direct examples for these databases just yet, the great news is that the framework supports integrating any Python library, such as
pymongo
for MongoDB or `psycopg2`/`asyncpg` for PostgreSQL. Here’s a simple example for MongoDB to get you started:
Copy code
from livekit.agents import function_tool
import pymongo

@function_tool
async def query_user_data(user_id: str):
    client = pymongo.MongoClient("<mongodb://your-host:27017/>")
    db = client["your_db"]
    user = db.users.find_one({"id": user_id})
    return str(user) or "User not found"
And here’s how you might do it for PostgreSQL using `psycopg2`:
Copy code
from livekit.agents import function_tool
import psycopg2

@function_tool
async def get_user_orders(user_id: str):
    conn = psycopg2.connect(
        host="your-postgres-host",
        database="your_db",
        user="your_user",
        password="your_password"
    )
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM orders WHERE user_id = %s", (user_id,))
    results = cursor.fetchall()
    cursor.close()
    conn.close()
    return str(results)
If you're looking to handle asynchronous operations, I recommend considering
asyncpg
for PostgreSQL or wrapping blocking calls in a thread pool. Please feel free to reach out if you'd like any further guidance or help with the implementation—I’m here to support you every step of the way! Sources: External data and RAG | LiveKit Docs | External data and RAG | LiveKit Docs | complex-agents/nutrition-assistant/agent.py | examples/drive-thru/drivethru_agent.py