File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed
Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ import 'dotenv/config' ;
2+ import { getRunner , Pipe } from 'langbase' ;
3+
4+ const pipe = new Pipe ( {
5+ apiKey : process . env . LANGBASE_API_KEY ! ,
6+ name : 'summary' ,
7+ } ) ;
8+
9+ async function main ( ) {
10+ const userMsg = 'Who is an AI Engineer?' ;
11+
12+ // Get readable stream
13+ const { stream, threadId, rawResponse} = await pipe . run ( {
14+ messages : [ { role : 'user' , content : userMsg } ] ,
15+ stream : true ,
16+ rawResponse : true ,
17+ } ) ;
18+
19+ // Convert the stream to a stream runner.
20+ const runner = getRunner ( stream ) ;
21+
22+ // Method 1: Using event listeners
23+ runner . on ( 'connect' , ( ) => {
24+ console . log ( 'Stream started.\n' ) ;
25+ } ) ;
26+
27+ runner . on ( 'content' , content => {
28+ process . stdout . write ( content ) ;
29+ } ) ;
30+
31+ runner . on ( 'end' , ( ) => {
32+ console . log ( '\nStream ended.' ) ;
33+ } ) ;
34+
35+ runner . on ( 'error' , error => {
36+ console . error ( 'Error:' , error ) ;
37+ } ) ;
38+ }
39+
40+ main ( ) ;
Original file line number Diff line number Diff line change 1+ import 'dotenv/config' ;
2+ import { Pipe } from 'langbase' ;
3+
4+ const pipe = new Pipe ( {
5+ apiKey : process . env . LANGBASE_API_KEY ! ,
6+ name : 'summary' ,
7+ } ) ;
8+
9+ async function main ( ) {
10+ const userMsg = 'Who is an AI Engineer?' ;
11+
12+ const response = await pipe . run ( {
13+ messages : [
14+ {
15+ role : 'user' ,
16+ content : userMsg ,
17+ } ,
18+ ] ,
19+ stream : false ,
20+ } ) ;
21+ console . log ( 'response: ' , response ) ;
22+ }
23+
24+ main ( ) ;
Original file line number Diff line number Diff line change 77 "main" : " index.js" ,
88 "scripts" : {
99 "generate-text" : " npx tsx ./examples/pipes/generate-text.ts" ,
10+ "pipe.run" : " npx tsx ./examples/pipes/pipe.run.ts" ,
11+ "pipe.run.stream" : " npx tsx ./examples/pipes/pipe.run.stream.ts" ,
1012 "generate-text-generate-pipe" : " npx tsx ./examples/pipes/generate-text-generate-pipe.ts" ,
1113 "generate-text-chat-pipe" : " npx tsx ./examples/pipes/generate-text-chat-pipe.ts" ,
1214 "stream-text" : " npx tsx ./examples/pipes/stream-text.ts" ,
You can’t perform that action at this time.
0 commit comments