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

@typedai/react

v0.0.2

Published

### Install the package for React or Next.js

Downloads

4

Readme

A package for OpenAI to easily create text in realtime like you were writing a haiku.

Install the package for React or Next.js

npm install @typedai/react

Example in the client using Next.js 13

'use client'
import { useTypedAI } from './hooks/useTypedAI'
import { useEffect, useState } from 'react'

export default function Home() {
	const [msg, setMsg] = useState('')
	const { realTimeTypedAI, startTypedAI, setUserInput } = useTypedAI('/api/explain')

	useEffect(() => {
		setUserInput(msg)
	}, [msg])

	const handleForm = (event: React.FormEvent<HTMLFormElement>) => {
		event.preventDefault()

		startTypedAI()
	}

	return (
		<main>
			<div>
				<form onSubmit={handleForm}>
					<label htmlFor="userInput">Type something</label>
					<input type="text" value={msg} onChange={(e) => setMsg(e.currentTarget.value)} />
					<button type="submit">Ask</button>
				</form>
				<p>{realTimeTypedAI}</p>
			</div>
		</main>
	)
}

Example in the server using Next.js 13 route handler

import { oneLine, stripIndent } from 'common-tags'
import type { CreateCompletionRequest } from 'openai'

const OPENAPI_TOKEN = ''

export const POST = async (request: Request) => {
	try {
		if (!OPENAPI_TOKEN) {
			throw new Error('OPENAPI_TOKEN is not set')
		}
		// request.json() is a function that returns a promise
		const body = await request.json()
		if (!body) {
			throw new Error('No openapi field')
		}

		// prompt is the text that the model will use to generate a response
		const prompt = stripIndent`
	     ${oneLine`
        You are going to answer a few questions about if someone is a good fit for a course.

        Here is the information that gpt-3 will use to generate a response:
        """
         name: foo
         course: foo
         age: foo
         instructor: foo
        """
      `}

      Context: """${body.trim()}"""

      Answer:
	    `
		const completionsOpts = {
			// engine is the name of the model to use
			model: 'text-davinci-003',
			// prompt is the text that the model will use to generate a response
			prompt,
			// max_tokens is how many words the model will generate
			max_tokens: 256,
			// temperature means how much the model will deviate from the prompt
			temperature: 0.9,
			// stream true means the API will return results as available
			stream: true,
		} satisfies CreateCompletionRequest
		console.log(
			`
	      Hello, this is a test of the OpenAI API.
	      `
		)
		const resp = await fetch('https://api.openai.com/v1/completions', {
			method: 'POST',
			headers: {
				Authorization: `Bearer ${OPENAPI_TOKEN}`,
				'Content-Type': 'application/json',
			},
			body: JSON.stringify(completionsOpts),
		})
		if (!resp.ok) {
			throw new Error('OpenAI API error')
		}
		return new Response(resp.body, {
			headers: {
				'Content-Type': 'text/event-stream',
			},
		})
	} catch (err) {
		console.log(err)
	}
}