Speakeasy Logo
Skip to Content

Using environments with the Vercel AI SDK

When initializing the Gram SDKs, specify which environment to use. The tools passed to the LLM will be bound to that environment.

vercel-example.ts
import { generateText } from "ai"; import { VercelAdapter } from "@gram-ai/sdk/vercel"; import { createOpenAI } from "@ai-sdk/openai"; const key = process.env.GRAM_API_KEY; const vercelAdapter = new VercelAdapter({ apiKey: key }); const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY, }); const tools = await vercelAdapter.tools({ project: "acme", toolset: "marketing", environment: "production", }); const result = await generateText({ model: openai("gpt-4"), tools, maxSteps: 5, prompt: "Can you tell me what tools you have available?", }); console.log(result.text);
openai-agents-example.py
import asyncio import os from agents import Agent, Runner, set_default_openai_key from gram_ai.openai_agents import GramOpenAIAgents key = os.getenv("GRAM_API_KEY") gram = GramOpenAIAgents(api_key=key) set_default_openai_key(os.getenv("OPENAI_API_KEY")) agent = Agent( name="Assistant", tools=gram.tools( project="acme", toolset="marketing", environment="production", ), ) async def main(): result = await Runner.run( agent, "Can you tell me what tools you have available?", ) print(result.final_output) if __name__ == "__main__": asyncio.run(main())

Environments are not required to use the Gram SDKs. You can pass the necessary variables directly when creating an instance. This is useful when users of your Gram toolsets prefer to use their own credentials.

byo-env-vars.ts
import { VercelAdapter } from "@gram-ai/sdk/vercel"; const vercelAdapter = new VercelAdapter({ apiKey: process.env.GRAM_API_KEY, environmentVariables: { ACME_API_KEY: process.env.ACME_API_KEY, ACME_SERVER_URL: "https://janesmith.acme.com", }, });
byo-env-vars.py
import os from gram_ai.openai_agents import GramOpenAIAgents gram = GramOpenAIAgents( api_key=os.getenv("GRAM_API_KEY"), environment_variables= { "ACME_API_KEY": os.getenv("ACME_API_KEY"), "ACME_SERVER_URL": "https://janesmith.acme.com", } )

Last updated on