AI Integration

Pass in an LLM provider and InferaGraph unlocks chat, semantic search, and tool-driven graph manipulation.

AI Integration

InferaGraph is AI-first but provider-agnostic. You import a provider package, construct an instance, and pass it in via the llm prop. The library handles every LLM call from there—chat, embeddings, and tool dispatch—so your host code never has to.

  • Provider injection — host imports a provider, never invokes the LLM directly.
  • Predicate + NLQ chain — developer-set scope plus user-driven natural-language queries run as one chain.
  • Tool-call dispatch — the LLM emits apply_filter, highlight, focus, annotate; InferaGraph applies them to the graph visually.

Pass a provider

Construct your provider with its API key, then hand it to <InferaGraph llm={...} /> (React) or new AIEngine(store, llm) (vanilla).

import { InferaGraph } from '@inferagraph/core/react';
import { openaiProvider } from '@inferagraph/openai-provider';

// Construct the provider once and pass it in.
// InferaGraph never invokes the LLM directly.
const llm = openaiProvider({
  apiKey: process.env.OPENAI_API_KEY,
  model: 'gpt-4o-mini',  // optional
});

function App() {
  return <InferaGraph data={data} llm={llm} />;
}

Chat with the graph

The useInferaGraphChat() hook returns a chat(message) function whose AsyncIterable streams text deltas to your UI. Tool-call events—filter, highlight, focus, annotate—are handled internally and never surface to the host. Pass an AbortSignal to cancel cleanly.

import { useInferaGraphChat } from '@inferagraph/core/react';
import { useState } from 'react';

function Ask() {
  const { chat } = useInferaGraphChat();
  const [text, setText] = useState('');
  const controller = new AbortController();

  async function send(message) {
    setText('');
    for await (const event of chat(message, { signal: controller.signal })) {
      if (event.type === 'text') {
        setText(prev => prev + event.delta);
      }
      // tool calls (apply_filter / highlight / focus / annotate)
      // are handled internally and update the graph.
    }
  }

  return <div>{text}</div>;
}

Mock provider

A built-in mockLLMProvider ships with the core package—useful for tests, Storybook, or wiring up the chat surface before keys are in place.

import { mockLLMProvider } from '@inferagraph/core/data';

// Built-in mock for tests and stories. Replays a scripted
// sequence of ChatEvents the same way a real provider would.
const llm = mockLLMProvider({
  events: [
    { type: 'text', delta: 'Sarah was Abraham\'s wife.' },
    { type: 'tool_call', name: 'highlight', args: { ids: ['abraham', 'sarah'] } },
    { type: 'done', reason: 'stop' },
  ],
});