Skip to content

Commit dfa5909

Browse files
πŸ‘Œ IMPROVE: pipe run for stream and non-stream (#21)
* πŸ“¦ NEW: Update pipe run for streaming * πŸ“¦ NEW: Update pipe.run for non-stream * πŸ‘Œ IMPROVE: pipe run
1 parent 2f0317c commit dfa5909

File tree

15 files changed

+56
-64
lines changed

15 files changed

+56
-64
lines changed

β€Žapps/baseai.dev/content/docs/add/quickstart.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,17 @@ Add the following code to `index.ts` file:
128128

129129
```ts {{ title: 'index.ts' }}
130130
import 'dotenv/config';
131-
import {Pipe, streamText, getRunner} from '@baseai/core';
131+
import {Pipe, getRunner} from '@baseai/core';
132132
import aiTitleGeneratorPipe from './baseai/pipes/ai-title-generator';
133133

134134
const pipe = new Pipe(aiTitleGeneratorPipe());
135135

136136
const userMsg = `Generate 5 blog title ideas for an article about Large Language Models`;
137137

138138
async function main() {
139-
const {stream} = await streamText({
140-
pipe,
139+
const {stream} = await pipe.run({
141140
messages: [{role: 'user', content: userMsg}],
141+
stream: true
142142
});
143143

144144
const runner = getRunner(stream);

β€Žapps/baseai.dev/content/docs/api-reference/get-runner.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ modified: 2024-09-24
1414

1515
A stream is a continuous flow of data that can be consumed piece by piece. The `getRunner()` function is a utility function that converts a readable stream in a runner. This runner can be used to run the stream and consume the data.
1616

17-
The `getRunner()` function can be used together with `pipe.run()` [function](/docs/api-reference/pipe-run) when the stream is `true` or with the `streamText()` [function](/docs/api-reference/stream-text) to consume the stream.
17+
The `getRunner()` function can be used together with `pipe.run()` [function](/docs/api-reference/pipe-run) when the stream is `true` to consume the stream.
1818

1919
The BaseAI core package provides a `getRunner()` function that you can use in your app.
2020

@@ -123,6 +123,7 @@ The BaseAI core package provides a `getRunner()` function that you can use in yo
123123

124124
```bash {{ title: 'Create a new Pipe' }}
125125
npx baseai@latest pipe
126+
# OR
126127
pnpm dlx baseai@latest pipe
127128
```
128129

@@ -132,21 +133,21 @@ The BaseAI core package provides a `getRunner()` function that you can use in yo
132133
OPENAI_API_KEY="<REPLACE-WITH-YOUR-OPENAI-KEY>"
133134
```
134135

135-
### `getRunner()` example with `streamText()`
136+
### `getRunner()` example with `pipe.run()`
136137

137-
<CodeGroup exampleTitle="streamText()" title="streamText()">
138+
<CodeGroup exampleTitle="pipe.run()" title="pipe.run()">
138139

139140
```ts {{ title: 'index.ts' }}
140141
import 'dotenv/config';
141-
import {Pipe, streamText, getRunner} from '@baseai/core';
142+
import {Pipe, getRunner} from '@baseai/core';
142143
import pipeName from './baseai/pipes/agent';
143144

144145
const pipe = new Pipe(pipeName());
145146

146147
async function main() {
147-
const {stream} = await streamText({
148-
pipe,
148+
const {stream} = await pipe.run({
149149
messages: [{role: 'user', content: 'Hello'}],
150+
stream: true
150151
});
151152

152153
// NOTE: This is a Node.js only example.

β€Žapps/baseai.dev/content/docs/api-reference/pipe-run.mdx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ The BaseAI core package provides a `pipe.run()` function that you can use in you
235235
OPENAI_API_KEY="<REPLACE-WITH-YOUR-OPENAI-KEY>"
236236
```
237237

238-
### `pipe.run()` generate example
238+
### `pipe.run()` non-stream example
239239

240240
<CodeGroup exampleTitle="pipe.run()" title="pipe.run()">
241241

@@ -562,7 +562,7 @@ The BaseAI core package provides a `pipe.run()` function that you can use in you
562562
<Property name="choices" type="ChoiceGenerate[]">
563563
A list of chat completion choices. Can contain more than one elements if n is greater than 1.
564564

565-
```ts {{title: 'Choice Object for generateText()'}}
565+
```ts {{title: 'Choice Object for pipe.run() with stream off'}}
566566
interface ChoiceGenerate {
567567
index: number;
568568
message: Message;
@@ -671,7 +671,7 @@ The BaseAI core package provides a `pipe.run()` function that you can use in you
671671

672672
### RunResponseStream Object
673673

674-
Response of `streamText()` is a `Promise<RunResponseStream>`.
674+
Response of `pipe.run()` with `stream: true` is a `Promise<RunResponseStream>`.
675675

676676
```ts {{title: 'RunResponseStream Object'}}
677677
interface RunResponseStream {
@@ -691,10 +691,10 @@ The BaseAI core package provides a `pipe.run()` function that you can use in you
691691
The different headers of the response.
692692
</Property>
693693
<Property name="stream" type="ReadableStream">
694-
Stream is a StreamText object with a streamed sequence of StreamChunk objects.
694+
Stream is an object with a streamed sequence of StreamChunk objects.
695695

696696
```ts {{title: 'StreamResponse Object'}}
697-
type StreamText = ReadableStream<StreamChunk>;
697+
type StreamResponse = ReadableStream<StreamChunk>;
698698
```
699699

700700
### StreamChunk
@@ -734,7 +734,7 @@ The BaseAI core package provides a `pipe.run()` function that you can use in you
734734
<Property name="choices" type="ChoiceStream[]">
735735
A list of chat completion choices. Can contain more than one elements if n is greater than 1.
736736

737-
```js {{title: 'Choice Object for streamText()'}}
737+
```js {{title: 'Choice Object for pipe.run() with stream true'}}
738738
interface ChoiceStream {
739739
index: number;
740740
delta: Delta;
@@ -848,14 +848,13 @@ The BaseAI core package provides a `pipe.run()` function that you can use in you
848848
```
849849

850850
```js {{ title: 'RunResponseStream of pipe.run() with stream true' }}
851-
// Response of a streamText() call is a Promise<StreamResponse>.
852851
{
853852
"threadId": "string-uuid-123",
854-
"stream": StreamText // example of streamed chunks below.
853+
"stream": StreamResponse // example of streamed chunks below.
855854
}
856855
```
857856

858-
```json {{ title: 'StreamText has stream chunks' }}
857+
```json {{ title: 'StreamResponse has stream chunks' }}
859858
// A stream chunk looks like this …
860859
{
861860
"id": "chatcmpl-123",

β€Žapps/baseai.dev/content/docs/guides/using-ollama-embeddings.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ It will create a pipe in your current directory under `baseai/pipes/chat-with-do
127127
In your `index.ts` file, add the following code:
128128

129129
```ts
130-
import { Pipe, streamText, getRunner } from '@baseai/core';
130+
import { Pipe, getRunner } from '@baseai/core';
131131
import chatWithDocsPipeConfig from './baseai/pipes/chat-with-docs-rag';
132132

133133
// Instantiate the pipe
134134
const pipe = new Pipe(chatWithDocsPipeConfig());
135135

136136
async function main() {
137-
const {stream} = await streamText({
138-
pipe,
137+
const {stream} = await pipe.run({
139138
messages: [{role: 'user', content: 'How to create a pipe?'}],
139+
stream: true
140140
});
141141

142142
// Convert the stream to a stream runner.

β€Žapps/baseai.dev/content/docs/memory/quickstart.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ In your `src/index.ts` file, add the following code:
8484

8585

8686
```ts
87-
import { Pipe, streamText, getRunner } from '@baseai/core';
87+
import { Pipe, getRunner } from '@baseai/core';
8888
import chatWithDocsPipeConfig from './baseai/pipes/chat-with-docs-rag';
8989

9090
// Instantiate the pipe
9191
const pipe = new Pipe(chatWithDocsPipeConfig());
9292

9393
async function main() {
94-
const {stream} = await streamText({
95-
pipe,
94+
const {stream} = await pipe.run({
9695
messages: [{role: 'user', content: 'How to create a pipe?'}],
96+
stream: true
9797
});
9898

9999
// Convert the stream to a stream runner.

β€Žapps/baseai.dev/content/docs/tools/quickstart.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,17 @@ Add the following code to `index.ts` file:
225225

226226
```ts {{ title: 'index.ts' }}
227227
import 'dotenv/config';
228-
import {Pipe, generateText} from '@baseai/core';
228+
import {Pipe} from '@baseai/core';
229229
import pipeSummarizer from './baseai/pipes/summarizer';
230230

231231
const pipe = new Pipe(pipeSummarizer());
232232

233233
const userMsg = `What's the weather in San Francisco?`;
234234

235235
async function main() {
236-
const response = await generateText({
237-
pipe,
236+
const response = await pipe.run({
238237
messages: [{role: 'user', content: userMsg}],
238+
stream: false,
239239
});
240240

241241
console.log(response.completion);

β€Žapps/baseai.dev/content/learn/learn/configure-tool.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default pipeSummarizer;
8585

8686
```ts {{ title: 'index.ts' }}
8787
import 'dotenv/config';
88-
import {Pipe, streamText, getRunner} from '@baseai/core';
88+
import {Pipe, getRunner} from '@baseai/core';
8989
import pipeSummarizer from './baseai/pipes/summarizer';
9090

9191
const pipe = new Pipe(pipeSummarizer());
@@ -98,9 +98,9 @@ A complete AI developers platform.
9898
`;
9999

100100
async function main() {
101-
const {stream} = await streamText({
102-
pipe,
101+
const {stream} = await pipe.run({
103102
messages: [{role: 'user', content: userMsg}],
103+
stream: true
104104
});
105105

106106
const runner = getRunner(stream);
@@ -205,7 +205,7 @@ export default pipeSummarizer;
205205

206206
```ts {{ title: 'index.ts' }}
207207
import 'dotenv/config';
208-
import {Pipe, streamText, getRunner} from '@baseai/core';
208+
import {Pipe, getRunner} from '@baseai/core';
209209
import pipeSummarizer from './baseai/pipes/summarizer';
210210

211211
const pipe = new Pipe(pipeSummarizer());
@@ -218,9 +218,9 @@ A complete AI developers platform.
218218
`;
219219

220220
async function main() {
221-
const {stream} = await streamText({
222-
pipe,
221+
const {stream} = await pipe.run({
223222
messages: [{role: 'user', content: userMsg}],
223+
stream: true
224224
});
225225

226226
const runner = getRunner(stream);

β€Žapps/baseai.dev/content/learn/learn/integrate-memory-in-pipe.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ export default pipeSummarizer;
6868

6969
```ts {{ title: 'index.ts' }}
7070
import 'dotenv/config';
71-
import {Pipe, generateText} from '@baseai/core';
71+
import {Pipe} from '@baseai/core';
7272
import pipeSummarizer from './baseai/pipes/summarizer';
7373

7474
const pipe = new Pipe(pipeSummarizer());
7575

7676
const userMsg = `What's the weather in San Francisco?`;
7777

7878
async function main() {
79-
const response = await generateText({
80-
pipe,
79+
const response = await pipe.run({
8180
messages: [{role: 'user', content: userMsg}],
81+
stream: false,
8282
});
8383

8484
console.log(response.completion);

β€Žapps/baseai.dev/content/learn/learn/integrate-pipe.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Add the following code to `index.ts` file:
4747

4848
```ts {{ title: 'index.ts' }}
4949
import 'dotenv/config';
50-
import { Pipe, streamText, getRunner } from '@baseai/core';
50+
import { Pipe, getRunner } from '@baseai/core';
5151
import pipeSummarizer from './baseai/pipes/summarizer';
5252

5353
const pipe = new Pipe(pipeSummarizer());
@@ -60,9 +60,9 @@ A complete AI developers platform.
6060
`;
6161

6262
async function main() {
63-
const { stream } = await streamText({
64-
pipe,
65-
messages: [{ role: 'user', content: userMsg }]
63+
const { stream } = await pipe.run({
64+
messages: [{ role: 'user', content: userMsg }],
65+
stream: true
6666
});
6767

6868
const runner = getRunner(stream);
@@ -125,7 +125,7 @@ export default pipeSummarizer;
125125

126126
</CodeGroup>
127127

128-
We have added a user message to the pipe. We will use this message to test the pipe. We will also use the `streamText` method to stream the text on the terminal.
128+
We have added a user message to the pipe. We will use this message to test the pipe. We will also use the `pipe.run` method with `stream: true` to stream the text in the terminal.
129129

130130
## Step #3: Add OpenAI API key in env
131131

β€Žapps/baseai.dev/content/learn/learn/integrate-tool-in-pipe.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default getCurrentWeatherTool;
9999

100100
```ts {{ title: 'index.ts' }}
101101
import 'dotenv/config';
102-
import {Pipe, streamText, getRunner} from '@baseai/core';
102+
import {Pipe, getRunner} from '@baseai/core';
103103
import pipeSummarizer from './baseai/pipes/summarizer';
104104

105105
const pipe = new Pipe(pipeSummarizer());
@@ -112,9 +112,9 @@ A complete AI developers platform.
112112
`;
113113

114114
async function main() {
115-
const {stream} = await streamText({
116-
pipe,
115+
const {stream} = await pipe.run({
117116
messages: [{role: 'user', content: userMsg}],
117+
stream: true
118118
});
119119

120120
const runner = getRunner(stream);

0 commit comments

Comments
Β (0)