Skip to content

Commit 956d894

Browse files
committed
👌 IMPROVE: Examples and Types
1 parent fc2340b commit 956d894

3 files changed

Lines changed: 35 additions & 19 deletions

File tree

examples/everything/generate-text.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
/**
2+
* Generates a text completion using `generateText()`
3+
*
4+
* @docs: https://langbase.com/docs/langbase-sdk/generate-text
5+
*/
6+
17
import 'dotenv/config';
28
import {Pipe} from 'langbase';
39

4-
// STREAM: OFF
5-
console.log('STREAM-OFF: generateText()');
6-
710
// 1. Initiate the Pipe.
811
const pipe = new Pipe({
912
apiKey: process.env.PIPE_LESS_WORDY!,

examples/everything/stream-text.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
/**
2+
* Generates a text completion using `streamText()`
3+
*
4+
* @docs: https://langbase.com/docs/langbase-sdk/stream-text
5+
*/
6+
17
import 'dotenv/config';
28
import {Pipe} from 'langbase';
39

4-
// STREAM: ON
5-
console.log('STREAM-ON: streamText()');
6-
710
// 1. Initiate the Pipe.
811
const pipe = new Pipe({
912
apiKey: process.env.PIPE_LESS_WORDY!,

packages/langbase/src/pipes/pipes.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
import {Request} from '../common/request';
22
import {Stream} from '../common/stream';
33

4+
export type Role = 'user' | 'assistant' | 'system' | 'tool';
5+
46
export interface Message {
5-
role: 'user' | 'assistant' | 'system';
7+
role: Role;
68
content: string;
79
}
810

911
export interface GenerateOptions {
1012
messages: Message[];
11-
model?: string;
12-
temperature?: number;
13-
max_tokens?: number;
1413
}
1514

16-
export interface Choice {
15+
interface ChoiceNonStream {
1716
index: number;
18-
delta?: {
19-
content?: string;
20-
};
21-
message?: Message;
17+
message: Message;
18+
logprobs: boolean | null;
19+
finish_reason: string;
20+
}
21+
22+
interface ChoiceStream {
23+
index: number;
24+
delta: Delta;
25+
logprobs: boolean | null;
26+
finish_reason: string;
27+
}
28+
29+
interface Delta {
30+
role?: Role;
31+
content?: string;
2232
}
2333

2434
export interface Usage {
@@ -34,22 +44,22 @@ export interface GenerateNonStreamResponse {
3444
object: string;
3545
created: number;
3646
model: string;
37-
choices: Choice[];
47+
choices: ChoiceNonStream[];
3848
usage: Usage;
3949
system_fingerprint: string | null;
4050
};
4151
}
4252

53+
export type GenerateStreamResponse = Stream<GenerateStreamChunk>;
54+
4355
export interface GenerateStreamChunk {
4456
id: string;
4557
object: string;
4658
created: number;
4759
model: string;
48-
choices: Choice[];
60+
choices: ChoiceStream[];
4961
}
5062

51-
export type GenerateStreamResponse = Stream<GenerateStreamChunk>;
52-
5363
export interface PipeOptions {
5464
apiKey: string;
5565
baseUrl?: string;

0 commit comments

Comments
 (0)