Build AI-powered applications with Vercel's AI SDK and streaming capabilities.
npm install ai cognitora
1import { generateText, tool } from 'ai';
2import { openai } from '@ai-sdk/openai';
3import { Cognitora } from 'cognitora';
4import { z } from 'zod';
5
6const cognitora = new Cognitora({ apiKey: process.env.COGNITORA_API_KEY });
7
8async function runVercelAICodeInterpreter(userQuery: string) {
9 const session = await cognitora.sessions.create({
10 image: 'python:3.11-slim',
11 timeout: 300,
12 persistent: true
13 });
14
15 const executePythonTool = tool({
16 description: 'Execute Python code in a secure sandbox',
17 parameters: z.object({
18 code: z.string().describe('Python code to execute'),
19 }),
20 execute: async ({ code }) => {
21 const execution = await cognitora.compute.execute({
22 sessionId: session.id,
23 command: ["python", "-c", code]
24 });
25
26 return {
27 success: execution.exitCode === 0,
28 stdout: execution.stdout,
29 stderr: execution.stderr,
30 exitCode: execution.exitCode
31 };
32 },
33 });
34
35 const { text } = await generateText({
36 model: openai('gpt-4'),
37 messages: [
38 { role: 'system', content: 'You are a Python expert. Use the execute_python tool to run code.' },
39 { role: 'user', content: userQuery }
40 ],
41 tools: {
42 execute_python: executePythonTool,
43 },
44 });
45
46 return {
47 response: text,
48 sessionId: session.id
49 };
50}
Get started with Vercel AI SDK and Cognitora in minutes. Secure, scalable, and ready for anything.