OpenAI-Compatible API

medium10 minopenai-compat.ts

Existing code uses the OpenAI SDK (Assistants API). You want it to run on Cogitator without rewriting it.

What it shows

  • Start createOpenAIServer() with your tools
  • Point the official openai client at it and use assistants, threads and runs

The code

openai-compat.ts
import { Cogitator, tool } from '@cogitator-ai/core';
import { createOpenAIServer } from '@cogitator-ai/openai-compat';
import OpenAI from 'openai';
import { z } from 'zod';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const multiply = tool({
  name: 'multiply',
  description: 'Multiply two numbers',
  parameters: z.object({ a: z.number(), b: z.number() }),
  execute: async ({ a, b }) => ({ product: a * b }),
});

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });
const server = createOpenAIServer(cog, {
  port: 8080,
  tools: [multiply],
  defaultModel: 'google/gemini-3.5-flash-lite',
  logging: false,
});
await server.start();

const openai = new OpenAI({ baseURL: 'http://localhost:8080/v1', apiKey: 'not-needed' });

try {
  const assistant = await openai.beta.assistants.create({
    name: 'math-helper',
    model: 'cogitator',
    instructions: 'You are a math helper. Use the multiply tool for products. Be concise.',
  });
  const thread = await openai.beta.threads.create();

  await openai.beta.threads.messages.create(thread.id, { role: 'user', content: 'What is 42 * 17?' });
  const run = await openai.beta.threads.runs.createAndPoll(thread.id, { assistant_id: assistant.id });
  console.log('Run status:', run.status);

  const messages = await openai.beta.threads.messages.list(thread.id);
  const reply = messages.data[0]?.content[0];
  if (reply?.type === 'text') console.log(reply.text.value);
} finally {
  await server.stop();
  await cog.close();
}

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/openai-compat openai zod
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx openai-compat.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/integrations/06-openai-compat.ts

Learn more