New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

ai-react-hooks

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ai-react-hooks

A powerful collection of React hooks for AI models including ChatGPT, Ollama, Groq, DeepSeek, Anthropic, Google Gemini, and more.

latest
Source
npmnpm
Version
1.0.9
Version published
Weekly downloads
1
-94.74%
Maintainers
1
Weekly downloads
 
Created
Source

ai-react-hooks 🤖

npm version

npm downloads

bundle size

license

TypeScript

A collection of React hooks for seamless AI integration across multiple providers

✨ Features

  • Unified API: Single hook (useAI) for all providers

  • Multi-Provider: OpenAI, Anthropic, Google Gemini, Ollama, Groq, DeepSeek

  • Feature-Rich: Chat, image generation, speech-to-text, summarization

  • TypeScript: Full type safety and IntelliSense support

  • Lightweight: Zero dependencies, tree-shakeable

  • React 18+: Built for modern React applications

📦 Installation

Install using your preferred package manager.

npm

npm  install  ai-react-hooks

yarn

yarn  add  ai-react-hooks

pnpm

pnpm  add  ai-react-hooks

🚀 Quick Start

Basic Usage


import { useAI } from  "ai-react-hooks";

function  ChatComponent() {

const { ask, response, loading, error } = useAI({

provider:  "openai", // "ollama", "groq", "deepseek", etc.

apiKey:  process.env.OPENAI_API_KEY,

model:  "gpt-4o-mini",
});


const  handleClick = () => {

ask("Explain quantum computing in simple terms");

};

return (

<div>

<button  onClick={handleClick}  disabled={loading}>

{loading ? "Thinking..." : "Ask AI"}

</button>

{response && <p>{response}</p>}

  

{error && <p  className="error">Error: {error.message}</p>}

  

</div>


);

  

}

🎯 Available Hooks

HookProviderDescription
useAIAll ProvidersUnified hook for any AI provider
useChatGPTOpenAIGPT-4, GPT-4o, GPT-3.5
useAnthropicAnthropicClaude 3, Claude Instant
useGoogleGeminiGoogleGemini Pro, Gemini Flash
useOllamaOllamaLocal models (Llama, Mistral)
useGroqGroqUltra-fast LLM inference
useDeepSeekDeepSeekDeepSeek models
useSummarizeText ProcessingText summarization
useAiImageGeneratorImage GenerationDALL·E, Stable Diffusion
useSpeechToTextBrowser APIWeb Speech API integration

📖 Examples

1. Chat with OpenAI


import { useChatGPT } from  "ai-react-hooks";

function  OpenAIChat() {

const { ask, response, loading } = useChatGPT({

apiKey:  "your-openai-key",

model:  "gpt-4",

temperature:  0.7,
});

return (

<div>

<button  onClick={() =>  ask("Write a poem about React hooks")}>

Generate Poem

</button>

{loading && <span>Thinking...</span>}

{response && <p>{response}</p>}

</div>

);

}

2. Image Generation


import { useAiImageGenerator } from  "ai-react-hooks";

function  ImageGenerator() {
const { generate, imageUrl, loading } = useAiImageGenerator({

provider:  "openai",

apiKey:  process.env.OPENAI_API_KEY,

size:  "1024x1024",

});
return (

<div>

<input

placeholder="Describe an image..."

onKeyPress={(e) =>  e.key === 'Enter' && generate(e.target.value)}
/>

{loading && <p>Generating...</p>}

{imageUrl && <img  src={imageUrl}  alt="Generated"  />}

</div>

)


3. Speech to Text

import { useSpeechToText } from  "ai-react-hooks";

function  VoiceInput() {

const { start, stop, text, listening } = useSpeechToText({

continuous:  true,

language:  "en-US",


});

return (

<div>

<button  onClick={listening ? stop : start}>

{listening ? "Stop Recording" : "Start Recording"}

</button>

<p>{text || "Speak to see transcription..."}</p>

</div>

);

}

🔧 Configuration

Environment Setup

.env.local

OPENAI_API_KEY=sk-...

ANTHROPIC_API_KEY=sk-ant-...

GOOGLE_AI_API_KEY=AIza...

Provider Configuration


// OpenAI

const  openAI = useChatGPT({

apiKey:  "your-key",

model:  "gpt-4",

temperature:  0.7,
maxTokens:  1000,

});


// Anthropic

  

const  anthropic = useAnthropic({

apiKey:  "your-key",

model:  "claude-3-opus-20240229",

maxTokens:  1000,

});


// Ollama (Local)

  

const  ollama = useOllama({

baseUrl:  "http://localhost:11434",

model:  "llama2",

});

  

📚 API Reference

Common Props

PropTypeDescription
apiKeystringProvider API key
modelstringModel identifier
temperaturenumberCreativity level (0–1)
maxTokensnumberMaximum response length

Hook Return Values

All hooks return an object with:

PropertyTypeDescription
ask / generateFunctionTrigger AI request
responsestring | nullAI response
loadingbooleanRequest in progress
errorError | nullError information
resetFunctionReset hook state

🤝 Contributing

Contributions are welcome! Please read the Contributing Guide for details.

Steps to contribute:

  • Fork the repository

  • Create a feature branch

  • Commit your changes

  • Push to your branch

  • Open a Pull Request

🐛 Troubleshooting

CORS Issues with Ollama

Start Ollama with CORS enabled:

ollama serve --cors "*"

API Key Security

Always use environment variables for API keys:

import { useChatGPT } from 'your-package-name';

const { ask } = useChatGPT({
  apiKey: process.env.OPENAI_API_KEY, // ✅ Secure
  // apiKey: "sk-..." // ❌ Do not hardcode
});

📄 License

MIT © Yared Abebe

Keywords

react

FAQs

Package last updated on 27 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts