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

use-ollama

v0.1.1

Published

A custom hook that allows you to use the Ollama API in your React project.

Downloads

10

Readme

useOllama

A custom hook that allows you to use the Ollama API in your React project.

Installation

npm install use-ollama

API

For more information on the Ollama API, visit the Ollama API documentation.

<OllamaProvider
    host="localhost" // string - host of the Ollama API
/>

const {
    loading, // boolean - Loading state indicator
    error, // string - Error message
    response, // object - Response from the API
    chat, // function - Generate the next message in a chat with a provided model
    generate, // function - Generate a response for a given prompt with a provided model
    pull, // function - Download a model from the ollama library
    push, // function - Upload a model to a model library. Requires registering for ollama.ai and adding a public key first
    create, // function - Create a model from a Modelfile
    remove, // function - Delete a model and its data
    copy, // function - Copy a model. Creates a model with another name from an existing model
    list, // function - List models that are available locally on the host
    show, // function - Show information about a model including details, modelfile, template, parameters, license, and system prompt
    embeddings // function - Generate embeddings from a model
} = useOllama();

Usage

Here is a really simple example of how you can use the hook to create a chat-like app.

import { useEffect, useState } from 'react';
import { OllamaProvider, useOllama } from 'use-ollama';

const App = () => {
    return (
        <OllamaProvider host="localhost">
            <Component />
        </OllamaProvider>
    )
}

const Component = () => {
    const { chat, response, error } = useOllama();
    const [ message, setMessage ] = useState('');
    const [ messages, setMessages ] = useState([]);

    const handleMessageChange = event => {
        setMessage(event.target.value)
    }

    const handleMessageSend = event => {
        setMessages([
            ...messages,
            {
                role: 'user',
                content: message
            }
        ]);
    }

    useEffect(() => {
        const isLatestMessageFromUser = () => (
            messages[messages.length - 1]?.role === 'user'
        )

        const sendMessage = async () => {
            const message = {
                role: 'assistant',
                content: ''
            }

            setMessages([
                ...messages,
                message
            ]);

            await chat( {
                model: 'llama3',
                messages: messages.map( message => ( {
                    role: message.role,
                    content: message.content
                } ) ),
                stream: true
            } )
        }

        if( messages.length > 0 && isLatestMessageFromUser() ) {
            sendMessage()
        }
    }, [messages, chat]);

    useEffect(() => {
        const collectResponse = async response => {
            const message = messages[messages.length - 1]

            for await ( const part of response ) {
                message.content += part.message.content

                setMessages([
                    ...messages.slice(0, -1),
                    message
                ])

                if(part.done === true) {
                    break
                }
            }
        }

        if(response) {
            collectResponse(response)
        }
    }, [response])

    return (
        <div>
            <div>
                {messages.map((message, index) => (
                    <div key={index}>{message.content}</div>
                ))}
            </div>

            <div>
                <input
                    type="text"
                    value={message}
                    onChange={handleMessageChange}
                />

                <button onClick={handleMessageSend}>Send</button>
            </div>

            <div>{error}</div>
        </div>
    )
}

export default App

License

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