TechnologyApril 16, 2024

Vercel + Astra DB: Get Data into Your GenAI Apps Fast

An exciting new integration enables developers to provision databases directly within the Vercel experience.
Carter Rabasa
Carter RabasaHead of Developer Relations
Vercel + Astra DB: Get Data into Your GenAI Apps Fast
npm i @datastax/astra-db-ts
cp .env.example .env
OPENAI_API_KEY=sk-xxx
ASTRA_DB_API_ENDPOINT=https://xxx.apps.astra.datastax.com
ASTRA_DB_APPLICATION_TOKEN=AstraCS:xxx
export const runtime = 'nodejs';
import { AstraDB } from '@datastax/astra-db-ts'
const {
  ASTRA_DB_API_ENDPOINT,
  ASTRA_DB_APPLICATION_TOKEN
} = process.env
const db = new AstraDB(ASTRA_DB_APPLICATION_TOKEN, ASTRA_DB_API_ENDPOINT)
    functions: [
      {
        name: 'show_stock_price',
        description:
          'Get the current stock price of a given stock or currency. 
Use this to show the price to the user.',
        parameters: z.object({
          symbol: z
            .string()
            .describe(
              'The name or symbol of the stock or currency. 
e.g. DOGE/AAPL/USD.',
            ),
          price: z.number().describe('The price of the stock.'),
          delta: z.number().describe('The change in price of the stock'),
        }),
      }, ...]
  completion.onFunctionCall(
    'show_stock_price',
    async ({ symbol, price, delta }) => {
      reply.update(
        <BotCard>
          <StockSkeleton />
        </BotCard>,
      );

      await sleep(1000);
...
      // fetch actual price from Astra DB if it's in the S&P 500
      const collection = await db.collection("stock_prices")
      const doc = await collection.findOne({ Symbol: symbol })
      console.log("Queried DB, got back ", doc)
      if (doc) {
        price = parseFloat(doc.Currentprice)
      }
      else {
        await sleep(1000);
      }
Discover more
DataStax Astra DB
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.