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

copilot-react

v1.1.0

Published

React component that provides an autocompletion for LLM.

Downloads

26

Readme

copilot-react

React component that provides an autocompletion for LLM.

Background

LLM has changed people's work and lives significantly. However, it does not always return the correct answer and has limited applications. Copilot UI dramatically expands the applications of LLM by allowing collaboration between LLM and humans. For this, high-quality Copilot components are essential for the development of humans with LLM.

Features

  • Easy-to-use UI specialized for natural language input
  • Support for text-only or text-containing HTML
  • Provide helpers

Demo

See /packages/demo

Guide for users

normal state:

| Key | Action | |-----|--------| | Ctrl+Enter | Start completion |

completion state:

| Key | Action | |-----|--------| | Esc | Collapse completion | | Character Key | Collapse completion and insert pressed key |

Unlike Github copilot, you can perform operations such as arrow keys, copy, and paste even during completion.

Quick Example

See /packages/demo/src/index.tsx for details.

import { fromFetchSSEStream } from "copilot-js/dist/helpers/fromFetchSSEStream";

export const callCompletion = ({
  text,
  precedingText,
  followingText,
  callback,
  apiKey,
}: {
  text: string;
  precedingText?: string;
  followingText?: string;
  callback: (output: string) => void;
  apiKey: string;
}) => {
  const controller = new AbortController();
  const responsePromise = fetch("https://api.openai.com/v1/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify({
      model: "text-davinci-003",
      stream: true,
      prompt: `#instuction${text}\n#output\n${precedingText}`,
      max_tokens: 2000,
      temperature: 0.5,
      suffix: followingText,
      n: 1,
    }),
    signal: controller.signal,
  });
  return fromFetchSSEStream<{ choices: { text: string }[] }>({
    callback: (data) => {
      callback(data.choices[0].text);
    },
    controller,
    responsePromise,
  });
};

const Component = () => {
  const instruction = "write a novel.";
  const [text, setText] = useState<string>("");
  const copilotProps: Pick<
    ComponentProps<typeof Copilot>,
    "onChange" | "handler" | "errorHandler"
  >() => {
    useMemo(
    () => ({
      style: {
        width: "300px",
        minHeight: "100px",
        padding: "5px",
        border: "1px solid #ccc",
        height: "200px",
        overflowY: "auto",
        scrollBehavior: "smooth",
      },
      onChange: (value: string) => {
        setText(value);
      },
      handler: (params) => {
        return callCompletion({
          ...params,
          apiKey: process.env.OPENAI_API_KEY,
          text: instruction,
        });
      },
      errorHandler: async (e) => {
        if (e instanceof FetchResponseError) {
          alert(e.data.error?.message);
        }
      },
    }),
    [apiKey, instruction],
  );
  return <Copilot textOnly={true} value={text} {...copilotProps} />
}

Guide for developers

Installation

To install copilot-react, you can use npm:

npm i copilot-react

Step 1 Create a completion handler.

Use helpers in "./dist/helper/*" or write a handler by yourself. See samples in files:

| API sample | Description | |--------------|-------------| | /packages/demo/src/api/completion_sse.ts | API sample for fetch() that responds to server-sent event (can also be used for OpenAI API)| | /packages/demo/src/api/completion_text.ts | API sample for fetch() that responds to text stream | | /packages/demo/src/api/completion_openai.ts | API sample for OpenAI official library |

Step2 Use React component

See /packages/demo/src/index.tsx for details.

API Reference

Copilot

Component to handle completion

<Copilot
  {...{
    value: string;
    textOnly?: boolean = true;
    onChange?: (value: string) => void;
    delay?: number;
    handler: CompletionHandler;
    errorHandler?: ErrorHandler;
    className?: string;
    style?: CSSProperties;

  }}
/>

type CompletionHandler = (params: {
  precedingText?: string | undefined;
  followingText?: string | undefined;
  callback: (output: string) => void;
}) => StreamState;

type StreamState = {
  abort: () => void;
  promise: Promise<void>;
};

type ErrorHandler = (error: any) => void;

Props

  • handler: Completion handler.
  • value: Initial value.
  • textOnly (optional): If true, only text can be inserted. Default true.
  • onChange (optional): Callback when the value is changed.
  • delay (optional): Delay to start automatic completion. If not specified, completion will not start automatically.
  • errorHandler (optional): Error handler, callback error in CompletionHandler.
  • style (optional): The style object to apply to the component.
  • className (optional): The CSS class to apply to the component.