| 10 | const [loading, setLoading] = useState(false); |
| 11 | |
| 12 | const handleSubmit = async (e: any) => { |
| 13 | e.preventDefault(); |
| 14 | if (!prompt.trim()) return; |
| 15 | |
| 16 | setLoading(true); |
| 17 | try { |
| 18 | const response = await fetch('/api/langbase/pipes/run', { |
| 19 | method: 'POST', |
| 20 | headers: {'Content-Type': 'application/json'}, |
| 21 | // Send prompt as an LLM message. |
| 22 | body: JSON.stringify({ |
| 23 | messages: [{role: 'user', content: prompt}], |
| 24 | }), |
| 25 | }); |
| 26 | |
| 27 | if (!response.ok) { |
| 28 | const res = await response.json(); |
| 29 | throw new Error(res.error.error.message); |
| 30 | } |
| 31 | |
| 32 | // Parse the JSON response. |
| 33 | const data = await response.json(); |
| 34 | setCompletion(data.completion); |
| 35 | } catch (error: any) { |
| 36 | if (error.message) setCompletion(error.message); |
| 37 | else |
| 38 | setCompletion( |
| 39 | 'An error occurred while generating the completion.', |
| 40 | ); |
| 41 | } finally { |
| 42 | setLoading(false); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | return ( |
| 47 | <div className="bg-neutral-200 rounded-md p-2 flex flex-col gap-2 w-full"> |