Skip to content

Commit

Permalink
👌 IMPROVE: Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadawais committed Jul 27, 2024
1 parent d70f76f commit a317144
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions examples/everything/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {Pipe} from 'langbase';
console.log('STREAM-OFF: generateText()');

// 1. Initiate the Pipe.
const pipeStreamOff = new Pipe({
const pipe = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY!,
});

// 3. Generate the text by asking a question.
const result = await pipeStreamOff.generateText({
const result = await pipe.generateText({
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
});

Expand Down
13 changes: 5 additions & 8 deletions examples/everything/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@ import {Pipe} from 'langbase';
console.log('STREAM-OFF: generateText()');

// 1. Initiate the Pipe.
const pipeStreamOff = new Pipe({
const pipe = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY!,
});

// 3. Generate the text by asking a question.
const result = await pipeStreamOff.generateText({
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
console.log('STREAM-ON: streamText()');

// 1. Initiate the Pipe.
const pipeStreaming = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY_STREAM!,
});

// 2. Generate a stream by asking a question
const stream = await pipeStreaming.streamText({
const stream = await pipe.streamText({
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
});

Expand Down
4 changes: 2 additions & 2 deletions examples/everything/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {Pipe} from 'langbase';
console.log('STREAM-ON: streamText()');

// 1. Initiate the Pipe.
const pipeStreaming = new Pipe({
const pipe = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY!,
});

// 2. Generate a stream by asking a question
const stream = await pipeStreaming.streamText({
const stream = await pipe.streamText({
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
});

Expand Down
8 changes: 4 additions & 4 deletions examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import GenerateTextRouteHandler from '@/components/langbase/generate-text-route-handler';
import StreamTextRouteHandler from '@/components/langbase/stream-text-route-handler';
import GenerateTextExample from '@/components/langbase/generate-text';
import StreamTextExample from '@/components/langbase/stream-text';

export default function Home() {
return (
Expand All @@ -13,8 +13,8 @@ export default function Home() {
An AI agent that responds to your prompts.
</p>
</div>
<GenerateTextRouteHandler />
<StreamTextRouteHandler />
<GenerateTextExample />
<StreamTextExample />
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Button} from '@/components/ui/button';
import {Input} from '@/components/ui/input';
import {useState} from 'react';

export default function GenerateTextRouteHandler() {
export default function GenerateTextExample() {
const [prompt, setPrompt] = useState('');
const [completion, setCompletion] = useState('');
const [loading, setLoading] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import {Button} from '@/components/ui/button';
import {Input} from '@/components/ui/input';
import {useState} from 'react';
import {fromReadableStream} from './../../../../packages/langbase/src/lib/browser/stream';
import {fromReadableStream} from '../../../../packages/langbase/src/lib/browser/stream';

export default function StreamTextRouteHandler() {
export default function StreamTextExample() {
const [prompt, setPrompt] = useState('');
const [completion, setCompletion] = useState('');
const [loading, setLoading] = useState(false);
Expand All @@ -26,6 +26,7 @@ export default function StreamTextRouteHandler() {

if (response.body) {
const stream = fromReadableStream(response.body);
console.log('✨ ~ stream:', stream);

// Method #1 to get all of the chunk.
for await (const chunk of stream) {
Expand All @@ -34,7 +35,7 @@ export default function StreamTextRouteHandler() {
content && setCompletion(prev => prev + content);
}

// Method #2 to get only the chunk's content as delta of the chunks
// // Method #2 to get only the chunk's content as delta of the chunks
// stream.on('content', content => {
// setCompletion(prev => prev + content);
// });
Expand Down
2 changes: 1 addition & 1 deletion packages/langbase/src/pipes/pipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Pipe {
return this.request.post<GenerateStreamResponse>({
endpoint: '/beta/generate',
body: {...options, stream: true},
stream: true,
stream: true, // TODO: @ahmadbilaldev - why we need to add here as well?
});
}
}

0 comments on commit a317144

Please sign in to comment.