The Chat API is a Chat Completions interface that automatically injects Neuradex knowledge into LLM conversations. While it offers a familiar OpenAI SDK-style API, your project’s knowledge base is automatically injected as context.Additionally, tools with an execute function are automatically loop-executed by the SDK, so you don’t need to write the logic for returning tool results to the LLM yourself.
const client = new NdxClient({ apiKey: process.env.NEURADEX_API_KEY, projectId: 'your-project-id',});// Text generation (with automatic memory injection)const stream = client.chat.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Tell me about our return policy' }], memory: { enabled: true },});// Receive via streamingfor await (const chunk of stream.textStream) { process.stdout.write(chunk);}
When memory is enabled, Neuradex automatically retrieves knowledge and episodes related to the query and injects them into the LLM context. No need to build your own RAG pipeline.
When a tool definition includes an execute function, the SDK automatically handles tool execution and returns the results to the LLM. It loops automatically until the LLM decides no more tools are needed, or until maxToolRoundtrips is reached.
const stream = client.chat.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Check the weather in Tokyo and tell me if I need an umbrella' }], tools: { getWeather: { description: 'Get current weather for a city', parameters: { type: 'object', properties: { city: { type: 'string', description: 'City name' }, }, required: ['city'], }, // The SDK automatically executes this and returns the result to the LLM execute: async ({ city }) => { const res = await fetch(`https://api.weather.example/v1/${city}`); const data = await res.json(); return JSON.stringify(data); }, }, }, maxToolRoundtrips: 3,});// Get the final answer including tool execution resultsconsole.log(await stream.text);
If a tool without an execute function is called, the stream ends with finishReason: 'tool_calls'. In this case, you can get the called tool info from the toolCalls property and handle it yourself.
const stream = client.chat.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Give me a short answer' }],});// Get final text without consuming the streamconst text = await stream.text;const usage = await stream.usage;console.log(text);console.log(`Tokens used: ${usage?.totalTokens}`);