React
Vite, Create React App or any React setup.
Add the script tag to index.html, then call window.charge.ai.chat() from a component.
index.html (inside <body>)
<script src="https://cdn.chargemy.ai/v1/charge.js"></script>
Summarise.jsx
import { useState } from 'react'
export function Summarise() {
const [text, setText] = useState('')
const [reply, setReply] = useState('')
const [busy, setBusy] = useState(false)
async function run() {
setBusy(true)
try {
const r = await window.charge.ai.chat('Summarise in two lines: ' + text)
setReply(r.text)
} catch (e) {
if (e.code !== 'cancelled') setReply(e.message)
} finally {
setBusy(false)
}
}
return (
<div>
<textarea value={text} onChange={(e) => setText(e.target.value)} />
<button onClick={run} disabled={busy}>{busy ? 'Working…' : 'Summarise'}</button>
<p>{reply}</p>
</div>
)
} TypeScript
charge.d.ts
declare global {
interface Window { charge: any }
}
export {} Read next
Still stuck? Talk to us and we'll help.