Capability

AI

@streetjs/ai is a provider-agnostic module for LLM chat, embeddings, retrieval-augmented generation (RAG), and tool calling. Write against one contract and swap OpenAI, Anthropic, or a local Ollama model without touching call sites — and test with a deterministic, network-free provider.

Why a provider-agnostic surface

AI providers change pricing, models, and APIs constantly, and binding your application directly to one of them is a liability. @streetjs/ai puts a single AiProvider contract in front of OpenAI, Anthropic, and Ollama, so switching vendors — or running a local model in development — is a one-line change rather than a refactor. A deterministic in-memory provider lets your test suite exercise AI features with no network and no API key.

What’s included

Provider-agnostic chat

One AiProvider contract. Adapters for OpenAiProvider, AnthropicProvider, and OllamaProvider swap freely.

Embeddings & RAG

RagPipeline handles embed → store → retrieve → answer, with a built-in InMemoryVectorStore.

Tool calling

ChatSession runs the tool-calling loop: the model requests a tool, your handler runs, and the result is fed back automatically.

Deterministic tests

FakeAiProvider is network-free and deterministic — the default for tests and offline development.

Example

Chat against any provider through the shared contract — then swap the adapter without changing the call site:

1
2
3
4
5
6
7
8
9
import { OpenAiProvider, AnthropicProvider, OllamaProvider } from '@streetjs/ai';

const ai = new OpenAiProvider({ apiKey: process.env.OPENAI_API_KEY });
const res = await ai.chat({ messages: [{ role: 'user', content: 'Hello!' }] });
console.log(res.message.content, res.usage);

// Same call site, different vendor:
const claude = new AnthropicProvider({ apiKey: process.env.ANTHROPIC_API_KEY });
const local  = new OllamaProvider({ baseUrl: 'http://127.0.0.1:11434' });

Retrieval-augmented generation is a pipeline you index once and query repeatedly:

1
2
3
4
5
6
7
8
import { RagPipeline, OpenAiProvider } from '@streetjs/ai';

const rag = new RagPipeline({ provider: new OpenAiProvider({ apiKey }), topK: 4 });
await rag.index([
  { id: 'd1', text: 'The Eiffel Tower is in Paris.' },
  { id: 'd2', text: 'Mount Everest is the tallest mountain.' },
]);
const { answer, context } = await rag.answer('Where is the Eiffel Tower?');

Install with npm install @streetjs/ai.

Pair AI with the rest of the framework: run inference off the request path with background jobs, stream responses to clients over realtime channels, and store embeddings alongside your application data using the PostgreSQL driver. The OpenAI plugin wires provider configuration into your app.

Next steps