SDK
Vercel AI SDK tools
Easily integrate memory into your models with the Vercel AI SDK
@neurophic/ai-sdk wraps the JavaScript SDK for the Vercel AI SDK.
Install
npm install neurophic @neurophic/ai-sdk ai zod
pnpm add neurophic @neurophic/ai-sdk ai zod
yarn add neurophic @neurophic/ai-sdk ai zod
createTools
import { Neurophic } from 'neurophic';
import { createTools } from '@neurophic/ai-sdk';
const client = new Neurophic({ apiKey: process.env.NEUROPHIC_API_KEY });
const { remember, recall } = createTools({
client,
identifier: 'user-123',
name: 'Ada',
});
createTools returns two AI SDK tools you can pass straight to streamText or generateText:
remember. Takescontentand an optionaltopic; see API reference > Ingest.recall. Takesqueryand optionalgoal,sinceandlimit; see API reference > Retrieve.
Each tool carries a description and a zod input schema, so the model knows when and how to call it; invalid input is rejected before any request is made.
Example
import { Neurophic } from 'neurophic';
import { createTools } from '@neurophic/ai-sdk';
import { streamText, stepCountIs } from 'ai';
import { openai } from '@ai-sdk/openai';
const client = new Neurophic({ apiKey: process.env.NEUROPHIC_API_KEY });
const { remember, recall } = createTools({ client, identifier: 'user-123' });
const system = 'You are a travel assistant. Use recall before answering and remember what the user shares.';
const result = streamText({
model: openai('gpt-4o'),
system,
tools: { remember, recall },
stopWhen: stepCountIs(3),
messages: [
{ role: 'user', content: 'Remember that I prefer window seats on long flights.' },
],
});
Learn more under API reference > Ingest and API reference > Retrieve.
Warning
If a call fails, the tool'sexecute throws a NeurophicError and the AI SDK reports it as a tool error for that step.withContext
withContext calls client.context and returns a system prompt string with the context appended. Use it when you want memory in front of the model on every turn without giving it a tool.
import { Neurophic } from 'neurophic';
import { withContext } from '@neurophic/ai-sdk';
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
const client = new Neurophic({ apiKey: process.env.NEUROPHIC_API_KEY });
const userMessage = 'Can you find me a flight to Lisbon next month?';
const system = await withContext(client, {
identifier: 'user-123',
query: userMessage,
system: 'You are a travel assistant.',
});
const result = streamText({
model: openai('gpt-4o'),
system,
messages: [{ role: 'user', content: userMessage }],
});
withContext takes:
query. The text to fetch context for, usually the latest user message.system. Optional; your own system prompt.template. Optional(context, system) => string; the default appends a## Context from memoryheading and the context lines tosystem, or returns just that block whensystemis not set.
With the default template and the example above, the returned string is:
You are a travel assistant.
## Context from memory
- Prefers window seats on long flights. (memory)
Warning
withContext returns system unchanged (or an empty string when system is not set) in two cases: when nothing relevant is found, and when the request fails for any reason. Errors are not thrown.Learn more under API reference > Context.