Integrate Superagent with Astra DB Serverless

query_builder 20 min

This Superagent integration with Astra DB Serverless allows you to build, manage, and deploy unique AI Assistants.

This guide explains how to use Astra DB Serverless as a backend vector store for Superagent data.

Prerequisites

For this tutorial, you need the following:

Connect to the database

  1. In the Astra Portal, go to Databases, and select your database.

  2. Make sure the database is in Active status, and then, in the Database Details section, click Generate Token.

  3. In the Application Token dialog, click Copy, and then store the token securely. The token format is AstraCS: followed by a unique token string.

    Application tokens created from Database Details have the Database Administrator role for the associated database.

  4. In Database Details, copy your database’s API endpoint. The endpoint format is https://ASTRA_DB_ID-ASTRA_DB_REGION.apps.astra.datastax.com.

  • Linux or macOS

  • Windows

export ASTRA_DB_API_ENDPOINT=*API_ENDPOINT*
export ASTRA_DB_APPLICATION_TOKEN=*TOKEN*
export OPENAI_API_KEY=*API_KEY*
set ASTRA_DB_API_ENDPOINT=*API_ENDPOINT*
set ASTRA_DB_APPLICATION_TOKEN=*TOKEN*
set OPENAI_API_KEY=*API_KEY*

Set up your environment

Create a Python environment and install the dependencies:

conda create --name superagent-demo python=3.10
conda activate superagent-demo
brew install poetry

Set up Superagent

You can deploy Superagent on Replit. It’s an online platform that provides an IDE for coding and collaborating on projects in various programming languages. It’s designed to be an accessible and user-friendly platform for both beginners and experienced developers. Replit offers multiple plans, with more features available to paid plans.

  1. Deploy Superagent on Replit

  2. Create a .env file with the following environment variables:

JWT_SECRET="superagent"
VECTORSTORE="astra"
ASTRA_DB_ENDPOINT="API_ENDPOINT" # Your database API endpoint
ASTRA_DB_APPLICATION_TOKEN="APPLICATION_TOKEN" # Your database application token
ASTRA_DB_COLLECTION_NAME="COLLECTION" # A collection in your database where you want to store the data
ASTRA_DB_KEYSPACE_NAME="KEYSPACE" # The keyspace in your database that contains the collection.
OPENAI_API_KEY="API_KEY" # Your OpenAI API key
SUPERAGENT_API_KEY="API_KEY" # Your Superagent API key
MEMORY_API_URL=https://memory.superagent.sh
TZ="Etc/UTC"

Use Astra DB Serverless with Superagent

  1. Configure a Large Language Model (LLM) using OpenAI.

    llm = client.llm.create(request={
     "provider": "OPENAI",
     "apiKey": "YOUR_OPENAPI_KEY"
    })
  2. Create an agent; also known as an assistant. Because the LLM doesn’t understand when to trigger the datasource, the code prompts the user for a question that’s used to query the datasource.

    agent = client.agent.create(request={
        "name": "Chat Assistant",
        "description": "My first Assistant",
        "avatar": "https://myavatar.com/homanp.png",
        "isActive": True,
        "initialMessage": "Hi there! How can I help you?",
        "llmModel": "GPT_3_5_TURBO_16K_0613",
        "prompt": "You are a helpful AI Assistant, use the Tourism trend to answer any questions.",
    })
  3. Attach the LLM to the agent.

    client.agent.add_llm(agent_id=agent.data.id, llm_id=llm.data.id)
  4. Create a datasource. This tutorial uses a PDF file for demonstration purposes, but you can specify a different source document.

    datasource = client.datasource.create(request={
        "name": "tourism trend",
        "description": "demo pdf doc from internet",
        "type": "PDF",
        "url": "https://cor.europa.eu/en/events/Documents/NAT/Tourism%20-%20new%20trends,%20challenges%20and%20solutions%20-%20background%20note.pdf"
    })

    This command reads the identified document, processes and encodes the string content, and then inserts the data into the Astra DB Serverless database specified in the .env file.

  5. Add the datasource to the agent:

    # Connect the datasource to the Agent
    client.agent.add_datasource(
        agent_id=agent.data.id,
        datasource_id=datasource.data.id
    )
  6. Invoke the client. The example asks a specific question that is relevant to the PDF file’s tourism content.

    prediction = client.agent.invoke(
        agent_id=agent.data.id,
        input="summarize the tourism trends",
        enable_streaming=False,
        session_id="my_session" # Best practice is to create a unique session per user
    )

Here’s sample output:

superagent output

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com