()
| 3 | import {useState} from 'react'; |
| 4 | |
| 5 | export default function PipeRunExample() { |
| 6 | const [prompt, setPrompt] = useState('Who are you?'); |
| 7 | const [completion, setCompletion] = useState(''); |
| 8 | const [loading, setLoading] = useState(false); |
| 9 | |
| 10 | const handleSubmit = async (e: any) => { |
| 11 | e.preventDefault(); |
| 12 | if (!prompt.trim()) return; |
| 13 | |
| 14 | setLoading(true); |
| 15 | try { |
| 16 | const response = await fetch('/api/langbase/pipes/run', { |
| 17 | method: 'POST', |
| 18 | headers: {'Content-Type': 'application/json'}, |
| 19 | // Send prompt as an LLM message. |
| 20 | body: JSON.stringify({ |
| 21 | messages: [{role: 'user', content: prompt}], |
| 22 | }), |
| 23 | }); |
| 24 | |
| 25 | if (!response.ok) { |
| 26 | throw new Error('Network response was not ok'); |
| 27 | } |
| 28 | |
| 29 | // Parse the JSON response. |
| 30 | const data = await response.json(); |
| 31 | setCompletion(data.completion); |
| 32 | } catch (error) { |
| 33 | console.error('Error:', error); |
| 34 | setCompletion('An error occurred while generating the completion.'); |
| 35 | } finally { |
| 36 | setLoading(false); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | return ( |
| 41 | <div className="bg-neutral-200 rounded-md p-2 flex flex-col gap-2 w-full"> |
| 42 | <form |
| 43 | onSubmit={handleSubmit} |
| 44 | className="flex flex-col w-full items-center gap-2" |
| 45 | > |
| 46 | <Input |
| 47 | type="text" |
| 48 | placeholder="Enter prompt message here" |
| 49 | value={prompt} |
| 50 | onChange={e => setPrompt(e.target.value)} |
| 51 | required |
| 52 | /> |
| 53 | |
| 54 | <Button type="submit" className="w-full" disabled={loading}> |
| 55 | {loading ? 'AI is thinking...' : 'Ask AI'} |
| 56 | </Button> |
| 57 | </form> |
| 58 | |
| 59 | {!loading && completion && ( |
| 60 | <p className="mt-4"> |
| 61 | <strong>Generated completion:</strong> {completion} |
| 62 | </p> |
nothing calls this directly
no outgoing calls
no test coverage detected