diff --git a/packages/langbase/readme.md b/packages/langbase/readme.md index 4a1b346..9da4adb 100644 --- a/packages/langbase/readme.md +++ b/packages/langbase/readme.md @@ -28,31 +28,57 @@ yarn add langbase You can `generateText` or `streamText` based on the type of a pipe. +Check our [SDK documentation](https://langbase.com/docs) for more details. + +### Example projects + +Check the following examples: +- [Node: Generate Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/generate-text.ts) +- [Node: Stream Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/stream-text.ts) +- [Next.js Example)](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs) + - TypeScript code + - [React component](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/components/langbase) to display the response + - [API Route handlers](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/app/api/langbase/pipe) to send requests to ⌘ Langbase + +### Demo code: + ```TypeScript import 'dotenv/config'; import {Pipe} from 'langbase'; // STREAM: OFF -const pipeStreamOff = new Pipe({ +console.log('STREAM-OFF: generateText()'); + +// 1. Initiate the Pipe. +const pipe = new Pipe({ apiKey: process.env.PIPE_LESS_WORDY!, }); -const result = await pipeStreamOff.generateText({ - messages: [{role: 'user', content: 'Who is Ahmad Awais?'}], +// 3. Generate the text by asking a question. +const result = await pipe.generateText({ + messages: [{role: 'user', content: 'Who is an AI Engineer?'}], }); +// 4. Done: You got the generated completion. console.log(result.completion); +// ======= OR ======== // + // STREAM: ON -const pipeStreaming = new Pipe({ - apiKey: process.env.PIPE_LESS_WORDY_STREAM!, -}); +console.log(''); +console.log('STREAM-ON: streamText()'); -const stream = await pipeStreaming.streamText({ - messages: [{role: 'user', content: 'Who is Ahmad Awais?'}], +// 2. Generate a stream by asking a question +const stream = await pipe.streamText({ + messages: [{role: 'user', content: 'Who is an AI Engineer?'}], }); +// 3. Print the stream for await (const chunk of stream) { - process.stdout.write(chunk.choices[0]?.delta?.content || ''); + // Streaming text part — a single word or several. + const textPart = chunk.choices[0]?.delta?.content || ''; + + // Demo: Print the stream — you can use it however. + process.stdout.write(textPart); } ```