Technology•April 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.
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); }