A lightweight React integration layer for @litert-lm/core, crafted for elegant conversational UI workflows.
react-litert-lm gives you:
LiteRt engine cleanlyreact-litert-lm is built to help React teams ship modern chat experiences with clarity and composability.
@tanstack/react-query@litert-lm/corenpm install react-litert-lm
Peer dependencies:
react,@litert-lm/core.@tanstack/react-queryis optional and only required for the TanStack integrations.
Use LiteRtEngineProvider to wrap your app and initialize the engine once.
import { LiteRtEngineProvider, useLiteRtChatNonStream } from "react-litert-lm";
function Chat() {
const { result, sendMessage, cancelMessage, isPending } = useLiteRtChatNonStream({
preface: "You are an assistant that answers clearly.",
});
return (
<div>
<button onClick={() => sendMessage({ content: "Hello" })}>Send</button>
{isPending && <span>Loadingβ¦</span>}
{result && <p>{result.content}</p>}
<button onClick={cancelMessage}>Cancel</button>
</div>
);
}
export default function App() {
return (
<LiteRtEngineProvider model="gpt-4" backend="openai">
<Chat />
</LiteRtEngineProvider>
);
}
Stream messages in real time and render partial responses as they arrive.
import { useLiteRtChatStream } from "react-litert-lm";
function StreamingChat() {
const { streamingText, sendMessage, isStreaming, cancelMessage, error } =
useLiteRtChatStream({ preface: "You are a streaming assistant." });
return (
<div>
<button onClick={() => sendMessage({ content: "Tell me a short story." })}>
Start
</button>
{isStreaming ? <p>Streamingβ¦</p> : <p>{streamingText}</p>}
{error && <p style={{ color: "red" }}>{error.message}</p>}
<button onClick={cancelMessage}>Cancel</button>
</div>
);
}
Use TanStack React Query adapters for cache-backed chat requests and predictable network behavior.
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { LiteRtEngineProvider } from "react-litert-lm";
import { useLiteRtChatNonStreamTanstackQuery } from "react-litert-lm/tanstack";
const queryClient = new QueryClient();
function QueryChat({ message }: { message: string }) {
const { data, isFetching } = useLiteRtChatNonStreamTanstackQuery({
message: { content: message },
preface: "Answer in one sentence.",
});
return <div>{isFetching ? <span>Loadingβ¦</span> : <p>{data?.content}</p>}</div>;
}
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<LiteRtEngineProvider model="gpt-4" backend="openai">
<QueryChat message="What is React?" />
</LiteRtEngineProvider>
</QueryClientProvider>
);
}
LiteRtEngineProviderInitializes a memoized @litert-lm/core engine and exposes it through React context.
useLiteRtEngineReturns the engine instance created from provider settings.
useLiteRtChatConversationInitCreates a conversation instance with optional session configuration and preface.
useLiteRtChatNonStreamSimple non-streaming chat hook with cancellation support.
useLiteRtChatStreamStreaming chat hook that exposes incremental response text, loading state, and errors.
useLiteRtChatNonStreamTanstackQueryTanStack Query hook for non-streaming chat with caching and query lifecycle control.
useLiteRtChatStreamTanstackQueryStream-aware query hook built around experimental_streamedQuery.
npm run build
npm run format
npm run lint
npm run docs
npm run deploy
tsup for builds and typedoc for docs generationReleased under the ISC license.