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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pollinations/react

v2.0.7

Published

React components and hooks for Pollinations AI

Downloads

477

Readme

🌸 Pollinations Generative React Hooks 🌸

A simple way to generate images, text and markdown using the Pollinations API in your React projects.

🚀 Quick Start

For interactive example code and documentation, visit Pollinations React Hooks.

Install the package:

npm install @pollinations/react

🛠️ Hooks

usePollinationsText

The usePollinationsText hook allows you to generate text from Pollinations' API and use it directly in your React components.

import React from 'react';
import { usePollinationsText } from '@pollinations/react';

const HaikuComponent = () => {
  const text = usePollinationsText('Write a short haiku about Pollinations.AI', { 
    seed: 42,
    model: 'mistral',
    systemPrompt: 'You are a poetic AI assistant.'
  });
  
  return (
    <div>
      {text ? <p>{text}</p> : <p>Loading...</p>}
    </div>
  );
};

export default HaikuComponent;

Options

  • seed (number, default: -1): The seed for random text generation. If -1, a random seed will be used.
  • model (string, default: 'openai'): The model to use for text generation. Options: 'openai', 'mistral'.
  • systemPrompt (string, optional): A system prompt to set the behavior of the AI.

usePollinationsImage

The usePollinationsImage hook allows you to generate image URLs from Pollinations' API and use them directly in your React components.

import React from 'react';
import { usePollinationsImage } from '@pollinations/react';

const SunsetImageComponent = () => {
  const imageUrl = usePollinationsImage('A beautiful sunset over the ocean', {
    width: 800,
    height: 600,
    seed: 42,
    model: 'turbo',
    nologo: true,
    enhance: false
  });

  return (
    <div>
      {imageUrl ? <img src={imageUrl} alt="Generated sunset" /> : <p>Loading...</p>}
    </div>
  );
};

export default SunsetImageComponent;

Options

  • width (number, default: 1024): The width of the generated image.
  • height (number, default: 1024): The height of the generated image.
  • model (string, default: 'turbo'): The model to use for image generation.
  • seed (number, default: -1): The seed for random image generation. If -1, a random seed will be used.
  • nologo (boolean, default: true): Whether to generate the image without a logo.
  • enhance (boolean, default: false): Whether to enhance the generated image.

usePollinationsChat

The usePollinationsChat hook allows you to generate chat responses from Pollinations' API and use them directly in your React components.

import React, { useState } from 'react';
import { usePollinationsChat } from '@pollinations/react';

const ChatComponent = () => {
  const [input, setInput] = useState('');
  const { sendUserMessage, messages } = usePollinationsChat([
    { role: "system", content: "You are a helpful assistant" }
  ], { 
    seed: 42, 
    jsonMode: false,
    model: 'mistral'
  });

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

  return (
    <div>
      <div>
        {messages.map((msg, index) => (
          <p key={index}><strong>{msg.role}:</strong> {msg.content}</p>
        ))}
      </div>
      <input value={input} onChange={(e) => setInput(e.target.value)} />
      <button onClick={handleSend}>Send</button>
    </div>
  );
};

export default ChatComponent;

Options

  • seed (number, default: 42): The seed for random text generation.
  • jsonMode (boolean, default: false): Whether to parse the response as JSON.
  • model (string, default: 'openai'): The model to use for text generation. Options: 'openai', 'mistral'.

Markdown Example

Here's an example of how to use the usePollinationsText hook to generate and render markdown content:

import React from 'react';
import { usePollinationsText } from '@pollinations/react';
import ReactMarkdown from 'react-markdown';

const MarkdownExample = () => {
  const markdownContent = usePollinationsText('Create a guide on pollination techniques', {
    seed: 42,
    model: 'openai',
    systemPrompt: 'You are a technical writer specializing in biology. Responding always in Markdown format.'
  });

  return (
    <div>
      {markdownContent ? (
        <ReactMarkdown>{markdownContent}</ReactMarkdown>
      ) : (
        <p>Loading markdown content...</p>
      )}
    </div>
  );
};

export default MarkdownExample;

Note: This example uses react-markdown to render the markdown content. You'll need to install it separately:

npm install react-markdown

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.


Made with ❤️ by the Pollinations.AI team