npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nexaleaf/react-ai-hooks

v1.1.0

Published

React hooks for seamless LLM integration with streaming, conversation management, and multi-provider support

Downloads

15

Readme

@nexaleaf/react-ai-hooks

React hooks for seamless LLM integration with streaming, conversation management, and multi-provider support.

Features

Provider Support: Currently supports OpenAI and Anthropic with unified API
Streaming built-in: Real-time token-by-token updates for ChatGPT-like experiences
TypeScript-first: Full TypeScript support with comprehensive type definitions
SSR & Edge-ready: Works with Next.js, Remix, and other modern frameworks
Lightweight: Optimized bundle size (52.8kB unpacked, 10.8kB package)
Enterprise-ready: Built-in retry logic, circuit breakers, rate limiting, and error handling
Load Balancing: Fallback providers and load balancing support

Installation

npm install @nexaleaf/react-ai-hooks
# or
yarn add @nexaleaf/react-ai-hooks
# or
pnpm add @nexaleaf/react-ai-hooks

Quick Start

import { useLLM, useChatCompletion, useStreamingResponse } from '@nexaleaf/react-ai-hooks';

// Simple LLM generation
function TextGenerator() {
  const { generate, loading, result, error } = useLLM({
    provider: 'openai',
    apiKey: process.env.REACT_APP_OPENAI_API_KEY,
  });

  return (
    <div>
      <button onClick={() => generate('Write a haiku about coding')}>
        Generate
      </button>
      {loading && <p>Loading...</p>}
      {result && <p>{result}</p>}
      {error && <p>Error: {error.message}</p>}
    </div>
  );
}

// Chat conversation
function ChatInterface() {
  const { messages, sendMessage, loading } = useChatCompletion({
    provider: 'openai',
    apiKey: process.env.REACT_APP_OPENAI_API_KEY,
  });

  const [input, setInput] = useState('');

  const handleSend = () => {
    sendMessage(input);
    setInput('');
  };

  return (
    <div>
      <div>
        {messages.map((msg, i) => (
          <div key={i}>
            <strong>{msg.role}:</strong> {msg.content}
          </div>
        ))}
      </div>
      <input 
        value={input} 
        onChange={(e) => setInput(e.target.value)} 
        onKeyPress={(e) => e.key === 'Enter' && handleSend()}
      />
      <button onClick={handleSend} disabled={loading}>
        Send
      </button>
    </div>
  );
}

// Streaming response
function StreamingChat() {
  const { streamText, currentText, isStreaming } = useStreamingResponse({
    provider: 'openai',
    apiKey: process.env.REACT_APP_OPENAI_API_KEY,
  });

  return (
    <div>
      <button onClick={() => streamText('Tell me a story')}>
        Start Stream
      </button>
      <div>
        {currentText}
        {isStreaming && <span className="cursor">|</span>}
      </div>
    </div>
  );
}

Available Hooks

useLLM

General-purpose hook for single prompt-response interactions.

const { generate, loading, result, error } = useLLM({
  provider: 'openai',
  apiKey: 'your-api-key',
  model: 'gpt-4',
  temperature: 0.7,
});

useChatCompletion

Handles chat conversations with message history.

const { messages, sendMessage, loading, clearMessages } = useChatCompletion({
  provider: 'anthropic',
  apiKey: 'your-api-key',
  model: 'claude-3-sonnet-20240229',
});

useStreamingResponse

Real-time streaming for token-by-token updates.

const { streamText, currentText, isStreaming, stop } = useStreamingResponse({
  provider: 'openai',
  apiKey: 'your-api-key',
});

useEmbeddings

Generate text embeddings for search and RAG applications.

const { embed, vector, loading } = useEmbeddings({
  provider: 'openai',
  apiKey: 'your-api-key',
});

Currently Supported Providers

  • OpenAI ✅ - GPT-4o, GPT-4o-mini, GPT-4, GPT-3.5-turbo, embeddings
  • Anthropic ✅ - Claude 3.5 Sonnet, Claude 3 (Opus, Sonnet, Haiku)

Coming Soon

  • Google 🚧 - Gemini Pro, Gemini Pro Vision
  • Ollama 🚧 - Local models (Llama, Mistral, etc.)
  • Custom 🚧 - Bring your own API endpoint

Configuration

Environment Variables

Create a .env.local file:

# OpenAI
REACT_APP_OPENAI_API_KEY=your_openai_api_key

# Anthropic
REACT_APP_ANTHROPIC_API_KEY=your_anthropic_api_key

# Google
REACT_APP_GOOGLE_API_KEY=your_google_api_key

Provider Configuration

const config = {
  provider: 'openai',
  apiKey: process.env.REACT_APP_OPENAI_API_KEY,
  model: 'gpt-4',
  temperature: 0.7,
  maxTokens: 1000,
  baseURL: 'https://api.openai.com/v1', // Custom endpoint
  organization: 'your-org-id', // OpenAI specific
};

Advanced Features

Multi-Provider Support & Load Balancing

import { useMultiProvider, createProvider } from '@nexaleaf/react-ai-hooks';

const { getProvider } = useMultiProvider();

// Get different providers as needed
const openaiProvider = getProvider({
  provider: 'openai',
  apiKey: process.env.REACT_APP_OPENAI_API_KEY,
});

const anthropicProvider = getProvider({
  provider: 'anthropic', 
  apiKey: process.env.REACT_APP_ANTHROPIC_API_KEY,
});

// Or create a load-balanced setup with fallbacks
import { createLoadBalancedProvider } from '@nexaleaf/react-ai-hooks';

const provider = createLoadBalancedProvider(
  { provider: 'openai', apiKey: process.env.OPENAI_KEY },
  [{ provider: 'anthropic', apiKey: process.env.ANTHROPIC_KEY }]
);

Error Handling

const { generate, error } = useLLM({
  provider: 'openai',
  apiKey: 'your-api-key',
  onError: (error) => {
    console.error('LLM Error:', error);
    // Custom error handling
  },
});

Retry Configuration

const config = {
  provider: 'openai',
  apiKey: 'your-api-key',
  retryAttempts: 3,
  retryDelay: 1000,
  circuitBreakerThreshold: 5,
};

TypeScript Support

Full TypeScript support with comprehensive type definitions:

import type { 
  LLMResponse, 
  StreamingChunk, 
  BaseMessage,
  OpenAIConfig 
} from '@nexaleaf/react-ai-hooks';

interface CustomResponse extends LLMResponse {
  customField: string;
}

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT © NexaLeaf

Support

Current Integrations

This library is actively used in production by:

Roadmap

  • [ ] Google Gemini provider implementation
  • [ ] Ollama provider for local models
  • [ ] Custom provider support
  • [ ] Function calling / tools support
  • [ ] Built-in RAG utilities
  • [ ] React Native support
  • [ ] More provider integrations (Cohere, Together AI)