0.0.4 • Published 7 months ago

use-prediction v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

use-prediction

It is like a Copilot for your input fields.

Installation

npm install use-prediction

Usage

import { usePrediction } from "use-prediction"

const Example = () => {
  const prediction = usePrediction({ get: getOpenAICompletion })

  return (
    <div>
      <input ref={prediction.ref} />
    </div>
  )
}

function getOpenAICompletion(text: string, controller: AbortController) {
  return fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${process.env.OPENAI_TOKEN}`,
    },
    body: JSON.stringify({
      model: "gpt-3.5-turbo",
      messages: [{ role: "user", content: text }],
      temperature: 1,
      max_tokens: 5,
      top_p: 1,
      frequency_penalty: 0,
      presence_penalty: 0,
    }),
    signal: controller.signal,
  })
    .then((res) => res.json())
    .then((data) => data.choices[0].message.content)
}
0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago