# @typedai/react

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

Latest version **0.0.2** (published 2023-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @typedai/react
pnpm add @typedai/react
yarn add @typedai/react
bun add @typedai/react
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2023-03-03 |
| First published | 2023-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | gustavocadev |

## Links

- npm: https://www.npmjs.com/package/@typedai/react
- npm.io page: https://npm.io/package/@typedai/react

## Dependencies (1)

- [sse.js](https://npm.io/package/sse.js.md) ^0.6.1

## Recent versions

- 0.0.2 (latest) — 2023-03-03
- 0.0.1 — 2023-03-02

## 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

```bash
npm install @typedai/react
```

### Example in the client using Next.js 13

```tsx
'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

```ts
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)
	}
}
```

---
_Source: https://npm.io/package/@typedai/react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
