Vocode Setup for GenAI Voice Calls: A Guide for Non-Python Developers
Tags: genai, ai, vocode, voiceai
Published: 2024-05-24 17:51:37

<p><strong>Target Audience:</strong> This guide is designed for developers who are not primarily familiar with Python or its ecosystem, and want to set up Vocode for GenAI voice calls.</p>

<h2>Table of Contents</h2>

<ol>
  <li>Introduction to Vocode and Project Tools</li>
  <li>Project Setup and Dependency Management</li>
  <li>Configuring Environment Variables and Ngrok</li>
  <li>Setting Up the Telephony Server</li>
  <li>Creating the Outbound Call Script</li>
  <li>Additional Notes and Troubleshooting</li>
  <li>References</li>
</ol>

<h2>1. Introduction to Vocode and Project Tools</h2>

<p>Vocode is a powerful tool for creating AI-powered voice applications. To streamline the development process, we'll use:</p>

<ul>
  <li><strong>Poetry:</strong> For managing project dependencies and virtual environments.</li>
  <li><strong>Pyproject.toml:</strong> A configuration file used by Poetry to define project details, Python version, and required libraries.</li>
</ul>

<h2>2. Project Setup and Dependency Management</h2>

<ol>
  <li><strong>Install Poetry:</strong>
   Follow the instructions on the <a href="https://python-poetry.org/docs/#installation">Poetry installation page</a>.</li>

  <li><strong>Create the Project:</strong>

<pre><code>poetry new project-name</code></pre>
  </li>

  <li><strong>Activate the Virtual Environment:</strong>

<pre><code>poetry config virtualenvs.create true
poetry shell</code></pre>
  </li>

  <li><strong>Specify the Python Version:</strong>

    <ul>
      <li>Open <code>pyproject.toml</code> in your project directory.</li>
      <li>Add the following under <code>[tool.poetry.dependencies]</code>:

<pre><code>python = "&gt;=3.9,&lt;3.12"</code></pre>
      </li>
      <li>Update the lock file:

<pre><code>poetry lock --no-update</code></pre>
      </li>
    </ul>
  </li>

  <li><strong>Install Dependencies:</strong>

<pre><code>poetry add redis^4.5.4 twilio^8.1.0 vocode==0.1.111 vonage^3.5.1 python-dotenv^1.0.0</code></pre>
  </li>

  <li><strong>Verify Installation:</strong>

    <ul>
      <li>Create <code>main.py</code> and add:

<pre><code>print("Hello, GenAI")</code></pre>
      </li>
      <li>Run:

<pre><code>poetry run python main.py</code></pre>
      </li>
      <li>You should see "Hello, GenAI" printed in the console.</li>
    </ul>
  </li>
</ol>

<h2>3. Configuring Environment Variables and Ngrok</h2>

<ol>
  <li><strong>Create the <code>.env</code> File:</strong>

    <ul>
      <li>Create a <code>.env</code> file in your project's root directory.</li>
      <li>Add your credentials (replace placeholders with actual values):

<pre><code>BASE_URL=
DEEPGRAM_API_KEY=
OPENAI_API_KEY=
AZURE_SPEECH_KEY=
AZURE_SPEECH_REGION=
TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=
TWILIO_FROM_NUMBER=</code></pre>
      </li>
    </ul>
  </li>

  <li><strong>Set Up Ngrok:</strong>

    <ul>
      <li>Install Ngrok from <a href="https://ngrok.com/download">https://ngrok.com/download</a>.</li>
      <li>Set up your Ngrok AuthToken from <a href="https://dashboard.ngrok.com/get-started/your-authtoken">https://dashboard.ngrok.com/get-started/your-authtoken</a>.</li>
      <li>Run in a separate terminal:

<pre><code>ngrok http 3000</code></pre>
      </li>
      <li>Copy the provided ngrok URL (e.g., <code>https://abcdef123456.ngrok.io</code>) and paste it as your <code>BASE_URL</code> in <code>.env</code>, removing the <code>https://</code> prefix and trailing slash.</li>
      <li>Image of ngrok terminal output
        <img src="https://i.imgur.com/bc7WCNd.png" alt="ngrok terminal output" />
      </li>
    </ul>
  </li>
</ol>

<h2>4. Setting Up the Telephony Server</h2>

<ol>
  <li><strong>Copy the Server Code:</strong>
    Get the code from this Gist: <a href="https://gist.github.com/bajajcodes/5722cade50a9867b98b246a2cb30ced4">https://gist.github.com/bajajcodes/5722cade50a9867b98b246a2cb30ced4</a> and paste it into <code>main.py</code>.</li>

  <li><strong>Activate Your Virtual Environment:</strong>

<pre><code>poetry shell</code></pre>
  </li>

  <li><strong>Run the Server:</strong>

<pre><code>poetry run python main.py</code></pre>
  </li>

  <li><strong>Start the FastAPI Server (Add to main.py):</strong>

<pre><code>import uvicorn

if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=port)</code></pre>
  </li>

  <li><strong>Run the Script Again:</strong>

<pre><code>poetry run python main.py</code></pre>
  </li>

</ol>

<ul>
  <li>Image of Uvicorn Server Output
    <img src="https://i.imgur.com/0ajDCfi.png" alt="Uvicorn Server Output" />
  </li>
  <li>Image of ngrok accessing localhost server
    <img src="https://i.imgur.com/RvkrOQl.png" alt="ngrok accessing localhost server" />
  </li>
</ul>

<h2>5. Creating the Outbound Call Script</h2>

<ol>
  <li><strong>Set Up Redis:</strong>

<pre><code>docker run -dp 6379:6379 -it redis/redis-stack:latest</code></pre>
  </li>

  <li><strong>Create outbound_call.py:</strong>

Copy the code from this Gist: <a href="https://gist.github.com/bajajcodes/718b598b26e146c4b6bbc784c1d2b1c0">https://gist.github.com/bajajcodes/718b598b26e146c4b6bbc784c1d2b1c0</a> and paste it into <code>outbound_call.py</code>.</li>

  <li><strong>Activate Your Virtual Environment:</strong>

<pre><code>poetry shell</code></pre>
  </li>

  <li><strong>Run the Script:</strong>

<pre><code>poetry run python outbound_call.py</code></pre>
  </li>

</ol>
<ul>
  <li>Image of Script Console Output
    <img src="https://i.imgur.com/13bM79B.png" alt="Script Console Output" />
  </li>
  <li>Image of Server Logs
    <img src="https://i.imgur.com/qclFgDy.png" alt="Server Logs" />
  </li>
</ul>

<h2>6. Additional Notes and Troubleshooting</h2>

<ul>
 <li><strong>Ngrok URL:</strong> Ensure the <code>BASE_URL</code> in <code>.env</code> matches the ngrok URL.</li>
 <li><strong>Endpoints:</strong> Access your FastAPI app at the ngrok URL or your <code>BASE_URL</code>. You can test endpoints like <code>/inbound_call</code>, <code>/events</code>, and <code>/recordings</code> using <code>curl</code> or a web browser.</li>
</ul>

<h2>7. References</h2>

<ul>
 <li>Vocode Telephony Server Documentation: <a href="https://docs.vocode.dev/open-source/telephony">https://docs.vocode.dev/open-source/telephony</a></li>
 <li>Vocode Telephony Server Code: <a href="https://github.com/vocodedev/vocode-python/tree/main/apps/telephony_app">https://github.com/vocodedev/vocode-python/tree/main/apps/telephony_app</a></li>
</ul>