Skip to main content

Introduction

This example shows how to use Vocode as a tool augmenting the abilities of a Langchain agent. By providing it with access to Vocode, a Langchain Agent can now make autonomous phone calls and take action based on the outcome of the calls. Our demo will walk through how to instruct the agent to lookup a phone number and make the appropriate call.

How to run it

Requirements

  1. Install Ngrok
  2. Install Redis
  3. Install Poetry

Run the example

Note: gpt-4 is required for this to work. gpt-3.5-turbo or older models are not smart enough to parse JSON responses in Langchain agents reliably out-of-the-box. To get started, clone the Vocode repo or copy the Langchain agent app directory.

Environment

  1. Copy the .env.template and fill in your API keys. You’ll need:
  1. Tunnel port 3000 to ngrok by running:
Fill in the TELEPHONY_SERVER_BASE_URL environment variable with your ngrok base URL: don’t include https:// so should be something like:
  1. Buy a phone number on Twilio or verify your caller ID to use as the outbound phone number. Set this phone number as the OUTBOUND_CALLER_NUMBER environment variable. Include + and the area code, so for a US phone number, it would look something like.

Set up self-hosted telephony server

Run the following setups from the langchain_agent directory.

Running with Docker

  1. Build the telephony server Docker image
  1. Run the service using docker-compose

Running with Python

  1. (optional) Set up a Python environment: we recommend virtualenv
  1. Install requirements
  1. Run an instance of Redis at http://localhost:6379. With Docker, this can be done with:
  1. Run the TelephonyServer:

Set up the Langchain agent

With the self-hosted telephony server running:
  1. Update the phone numbers in the contact book in tools/contacts.py
  1. Run main.py

Code explanation

The Langchain agent is implemented in main.py. It uses the Langchain library to initialize an agent that can have a conversation.

Langchain Agent

main.py instantiates the langchain agent and relevant tools. It sets an objective, initializes a Langchain agent, and runs the conversation.

Langchain tools

Tool to get all contacts

Tool to call phone number

tools/vocode.py makes use of the OutboundCall class to initiate a phone call

TelephonyServer

telephony_app.py instantiates a TelephonyServer object to manage the phone call initiated by OutboundCall