The AI SDK for building declarative and composable AI-powered LLM products.
Check the Langbase SDK documentation for more details.
The following examples are for reference only. Prefer docs for the latest information.
First, install the langbase
package using npm or yarn:
npm install langbase
or
pnpm add langbase
or
yarn add langbase
You can langbase.pipe.run()
to generate or stream from a Pipe.
Check our SDK documentation for more details.
Check the following examples:
- Node: Generate Text
- Node: Stream Text
- Next.js Example
- TypeScript code
- React component to display the response
- API Route handlers to send requests to ⌘ Langbase
# Add your Langbase API key here: https://langbase.com/docs/api-reference/api-keys
LANGBASE_API_KEY="your-api-key"
Generate text langbase.pipe.run()
Set the stream
to false
. For more, check the API reference of langbase.pipe.run()
import 'dotenv/config';
import {Langbase} from 'langbase';
// 1. Initiate the Langbase.
const langbase = new Langbase({
// Make sure you have a .env file with LANGBASE_API_KEY.
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
// 2. Run the pipe with a question.
const response = await langbase.pipe.run({
stream: false,
name: 'summary' // pipe name to run
messages: [
{
role: 'user',
content: 'Who is an AI Engineer?',
},
],
});
// 3. Print the response.
console.log('response: ', response);
}
main();
Stream text langbase.pipe.run()
Set the stream
to true
. For more, check the API reference of langbase.pipe.run()
import 'dotenv/config';
import {getRunner, Langbase} from 'langbase';
// 1. Initiate the Langbase.
const langbase = new Langbase({
// Make sure you have a .env file with LANGBASE_API_KEY.
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const userMsg = 'Who is an AI Engineer?';
// 2. Run the pipe with a question.
const {****stre**am**} = await langbase.pipe.run({
stream: true,
name: 'summary', // pipe name to run
messages: [{role: 'user', content: userMsg}],
});
// 3. Get the runner and listen to the content.
const runner = getRunner(stream);
// 4. Print the response.
runner.on('content', content => {
process.stdout.write(content);
});
}
main();
Check out more examples in the docs →