|
1 | | -# ⌘ Langbase AI SDK |
| 1 | +# Langbase SDK |
2 | 2 |
|
3 | | -⌘ Langbase AI SDK for JavaScript, TypeScript, and Python. |
| 3 | +The AI SDK for building declarative and composable AI-powered LLM products. |
| 4 | + |
| 5 | +## Documentation |
| 6 | + |
| 7 | +Check the [Langbase SDK documentation](https://langbase.com/docs/langbase-sdk/overview) for more details. |
| 8 | + |
| 9 | +The following examples are for reference only. Prefer docs for the latest information. |
| 10 | + |
| 11 | +## Getting Started with `langbase` SDK |
| 12 | + |
| 13 | +### Installation |
| 14 | + |
| 15 | +First, install the `langbase` package using npm or yarn: |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install langbase |
| 19 | +``` |
| 20 | + |
| 21 | +or |
| 22 | + |
| 23 | +```bash |
| 24 | +pnpm add langbase |
| 25 | +``` |
| 26 | + |
| 27 | +or |
| 28 | + |
| 29 | +```bash |
| 30 | +yarn add langbase |
| 31 | +``` |
| 32 | + |
| 33 | +### Usage |
| 34 | + |
| 35 | +You can [`generateText`](https://langbase.com/docs/langbase-sdk/generate-text) or [`streamText`](https://langbase.com/docs/langbase-sdk/stream-text) based on the type of a pipe. |
| 36 | + |
| 37 | +Check our [SDK documentation](https://langbase.com/docs/langbase-sdk/overview) for more details. |
| 38 | + |
| 39 | +### Example projects |
| 40 | + |
| 41 | +Check the following examples: |
| 42 | + |
| 43 | +- [Node: Generate Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/generate-text.ts) |
| 44 | +- [Node: Stream Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/stream-text.ts) |
| 45 | +- [Next.js Example](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs) |
| 46 | + - TypeScript code |
| 47 | + - [React component](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/components/langbase) to display the response |
| 48 | + - [API Route handlers](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/app/api/langbase/pipe) to send requests to ⌘ Langbase |
| 49 | + |
| 50 | +### Node.js Example Code |
| 51 | + |
| 52 | + |
| 53 | +## Node.js Examples |
| 54 | + |
| 55 | +### Add a `.env` file with your Pipe API key |
| 56 | + |
| 57 | +```bash |
| 58 | +# Add your Pipe API key here. |
| 59 | +LANGBASE_PIPE_API_KEY="pipe_12345`" |
| 60 | +``` |
| 61 | +
|
| 62 | +--- |
| 63 | +
|
| 64 | +### Generate text [`generateText()`](https://langbase.com/docs/langbase-sdk/generate-text) |
| 65 | +
|
| 66 | +For more check the API reference of [`generateText()`](https://langbase.com/docs/langbase-sdk/generate-text) |
| 67 | +
|
| 68 | +```ts |
| 69 | +import 'dotenv/config'; |
| 70 | +import {Pipe} from 'langbase'; |
| 71 | +
|
| 72 | +// 1. Initiate the Pipe. |
| 73 | +const pipe = new Pipe({ |
| 74 | + // Make sure you have a .env file with any pipe you wanna use. |
| 75 | + // As a demo we're using a pipe that has less wordy responses. |
| 76 | + apiKey: process.env.LANGBASE_PIPE_API_KEY!, |
| 77 | +}); |
| 78 | +
|
| 79 | +// 3. Generate the text by asking a question. |
| 80 | +const result = await pipe.generateText({ |
| 81 | + messages: [{role: 'user', content: 'Who is an AI Engineer?'}], |
| 82 | +}); |
| 83 | +
|
| 84 | +// 4. Done: You got the generated completion. |
| 85 | +console.log(result.completion); |
| 86 | +``` |
| 87 | +
|
| 88 | +--- |
| 89 | +
|
| 90 | +### Stream text [`streamText()`](https://langbase.com/docs/langbase-sdk/stream-text) |
| 91 | +
|
| 92 | +For more check the API reference of [`streamText()`](https://langbase.com/docs/langbase-sdk/stream-text) |
| 93 | +
|
| 94 | +```ts |
| 95 | +import 'dotenv/config'; |
| 96 | +import {Pipe} from 'langbase'; |
| 97 | +
|
| 98 | +// 1. Initiate the Pipe. |
| 99 | +const pipe = new Pipe({ |
| 100 | + // Make sure you have a .env file with any pipe you wanna use. |
| 101 | + // As a demo we're using a pipe that has less wordy responses. |
| 102 | + apiKey: process.env.LANGBASE_PIPE_API_KEY!, |
| 103 | +}); |
| 104 | +
|
| 105 | +// 2. Generate a stream by asking a question |
| 106 | +const stream = await pipe.streamText({ |
| 107 | + messages: [{role: 'user', content: 'Who is an AI Engineer?'}], |
| 108 | +}); |
| 109 | +
|
| 110 | +// 3. Print the stream |
| 111 | +for await (const chunk of stream) { |
| 112 | + // Streaming text part — a single word or several. |
| 113 | + const textPart = chunk.choices[0]?.delta?.content || ''; |
| 114 | +
|
| 115 | + // Demo: Print the stream — you can use it however. |
| 116 | + process.stdout.write(textPart); |
| 117 | +} |
| 118 | +``` |
| 119 | +
|
| 120 | +Check out [more examples in the docs](https://langbase.com/docs/langbase-sdk/examples) → |
0 commit comments