Skip to content

Commit

Permalink
📖 DOC: Readme docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadawais committed Jul 27, 2024
1 parent 93d2a05 commit ba61afc
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions packages/langbase/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```

0 comments on commit ba61afc

Please sign in to comment.