MCPcopy Create free account
hub / github.com/CommandCodeAI/BaseAI / PipeRunExample

Function PipeRunExample

examples/nextjs/components/pipe-run.tsx:7–72  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import {useState} from 'react';
6
7export default function PipeRunExample() {
8 const [prompt, setPrompt] = useState('Who are you?');
9 const [completion, setCompletion] = useState('');
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">
48 <form
49 onSubmit={handleSubmit}
50 className="flex flex-col w-full items-center gap-2"
51 >
52 <Input
53 type="text"
54 placeholder="Enter prompt message here"
55 value={prompt}
56 onChange={e => setPrompt(e.target.value)}
57 required
58 />
59
60 <Button type="submit" className="w-full" disabled={loading}>
61 {loading ? 'AI is thinking...' : 'Ask AI'}
62 </Button>
63 </form>
64

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected