Skip to main content
useAskContent lets users ask natural language questions and receive AI-generated answers grounded in your project’s actual content. The answer streams token by token via SSE, and once streaming completes, a list of source records is provided.

Usage Example

import { useAskContent } from "@replyke/react-js";

function AskBox() {
  const { answer, sources, streaming, loading, error, ask, reset } = useAskContent();

  const handleAsk = () => {
    ask({
      query: "What are the most common questions about billing?",
      sourceTypes: ["entity", "comment"],
      limit: 5,
    });
  };

  return (
    <div>
      <button onClick={handleAsk} disabled={loading || streaming}>Ask</button>
      <button onClick={reset}>Clear</button>

      {loading && <p>Thinking...</p>}
      {answer && <p>{answer}{streaming && "▋"}</p>}

      {sources.length > 0 && (
        <ul>
          {sources.map((s) => (
            <li key={s.record.id}>
              [{s.sourceType}] {s.record.id}
            </li>
          ))}
        </ul>
      )}
      {error && <p style={{ color: "red" }}>{error}</p>}
    </div>
  );
}

Parameters (ask function)

query
string
required
The natural language question to answer.
sourceTypes
("entity" | "comment" | "message")[]
Limit the content types searched for context. Defaults to all types.
spaceId
string
Scope the context lookup to a specific space.
conversationId
string
Scope the context lookup to a specific conversation.
limit
number
Maximum number of source records to retrieve and use as context.

Returns

answer
string
The AI-generated answer. Grows incrementally as tokens arrive during streaming.
sources
ContentSearchResult[]
The source records that were used as context for the answer. Populated after streaming completes.
streaming
boolean
true while the SSE stream is open and tokens are arriving.
loading
boolean
true from when ask() is called until the first token arrives (or an error occurs).
error
string | null
Error message if the request failed.
ask
(props) => void
Initiates the AI question. Cancels any in-flight stream before starting a new one.
reset
() => void
Aborts any in-flight stream and clears all state.
useAskContent uses the Fetch API with ReadableStream for SSE. In React Native, install react-native-fetch-api, web-streams-polyfill, and react-native-polyfill-globals, then call polyfillGlobals() at app startup.