Skip to content

Commit 92e6176

Browse files
committed
📦 NEW: pipe.run() support in SDK
1 parent 4b54228 commit 92e6176

4 files changed

Lines changed: 130 additions & 56 deletions

File tree

examples/nextjs/app/langbase/pipe/run/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export async function POST(req: NextRequest) {
1313
const result = await langbase.pipe.run({
1414
messages: [{role: 'user', content: prompt}],
1515
name: 'summary',
16+
stream: false
1617
});
1718

1819
// 3. Done, return the stream in a readable stream format.

packages/langbase/src/common/request.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,23 @@ export class Request {
5757
await this.handleErrorResponse({response});
5858
}
5959

60-
if(!options.body) {
60+
if (!options.body) {
6161
return this.handleGenerateResponse({
6262
response,
6363
isChat: false,
6464
threadId: null,
65-
})
65+
});
6666
}
6767

6868
const threadId = response.headers.get('lb-thread-id');
6969

70+
if (options.body?.stream && url.includes('run')) {
71+
return this.handleRunResponseStream({
72+
response,
73+
rawResponse: options.body.rawResponse,
74+
}) as T;
75+
}
76+
7077
if (options.body.stream) {
7178
return this.handleStreamResponse({response}) as T;
7279
}
@@ -135,6 +142,41 @@ export class Request {
135142
return {stream, threadId: response.headers.get('lb-thread-id')};
136143
}
137144

145+
private handleRunResponseStream({
146+
response,
147+
rawResponse,
148+
}: {
149+
response: Response;
150+
rawResponse?: boolean;
151+
}): {
152+
stream: any;
153+
threadId: string | null;
154+
rawResponse?: {
155+
headers: Record<string, string>;
156+
};
157+
} {
158+
const controller = new AbortController();
159+
const streamSSE = Stream.fromSSEResponse(response, controller);
160+
const stream = streamSSE.toReadableStream();
161+
162+
const result: {
163+
stream: ReadableStream<any>;
164+
threadId: string | null;
165+
rawResponse?: {
166+
headers: Record<string, string>;
167+
};
168+
} = {
169+
stream,
170+
threadId: response.headers.get('lb-thread-id'),
171+
};
172+
if (rawResponse) {
173+
result.rawResponse = {
174+
headers: Object.fromEntries(response.headers.entries()),
175+
};
176+
}
177+
return result;
178+
}
179+
138180
private async handleGenerateResponse({
139181
response,
140182
isChat,

packages/langbase/src/langbase/langbase.ts

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,74 @@
11
import {Request} from '../common/request';
2-
import {
3-
Pipe as PipeBaseAI,
4-
RunOptions as RunOptionsT,
5-
RunOptionsStream as RunOptionsStreamT,
6-
RunResponse,
7-
RunResponseStream,
8-
} from '@baseai/core';
92

103
export type Role = 'user' | 'assistant' | 'system' | 'tool';
114

12-
// Base types without name and apiKey
13-
type BaseRunOptions = Omit<RunOptionsT, 'name' | 'apiKey'> & {
5+
export interface RunOptionsBase {
6+
messages?: Message[];
7+
variables?: Variable[];
8+
threadId?: string;
9+
rawResponse?: boolean;
10+
runTools?: boolean;
11+
tools?: Tools[];
12+
name?: string; // Pipe name for SDK,
13+
apiKey?: string; // pipe level key for SDK
14+
llmKey?: string; // LLM API key
15+
}
16+
17+
export interface RunOptionsT extends RunOptionsBase {
18+
stream?: false;
19+
}
20+
21+
export interface RunOptionsStreamT extends RunOptionsBase {
22+
stream: true;
23+
}
24+
25+
interface ChoiceGenerate {
26+
index: number;
27+
message: Message;
28+
logprobs: boolean | null;
29+
finish_reason: string;
30+
}
31+
32+
export interface Usage {
33+
prompt_tokens: number;
34+
completion_tokens: number;
35+
total_tokens: number;
36+
}
37+
38+
export interface RunResponse {
39+
completion: string;
40+
threadId?: string;
41+
id: string;
42+
object: string;
43+
created: number;
44+
model: string;
45+
choices: ChoiceGenerate[];
46+
usage: Usage;
47+
system_fingerprint: string | null;
48+
rawResponse?: {
49+
headers: Record<string, string>;
50+
};
1451
messages: Message[];
1552
llmKey?: string;
16-
};
53+
name?: string;
54+
}
55+
56+
export interface RunResponseStream {
57+
stream: ReadableStream<any>;
58+
threadId: string | null;
59+
rawResponse?: {
60+
headers: Record<string, string>;
61+
};
62+
}
1763

1864
// Union type for RunOptions
1965
export type RunOptions =
20-
| (BaseRunOptions & {name: string; apiKey?: never})
21-
| (BaseRunOptions & {name?: never; apiKey: string});
22-
23-
// Similar structure for RunOptionsStream
24-
type BaseRunOptionsStream = Omit<RunOptionsStreamT, 'name' | 'apiKey'> & {
25-
messages: Message[];
26-
llmKey?: string;
27-
};
66+
| (RunOptionsT & {name: string; apiKey?: never})
67+
| (RunOptionsT & {name?: never; apiKey: string});
2868

2969
export type RunOptionsStream =
30-
| (BaseRunOptionsStream & {name: string; apiKey?: never})
31-
| (BaseRunOptionsStream & {name?: never; apiKey: string});
70+
| (RunOptionsStreamT & {name: string; apiKey?: never})
71+
| (RunOptionsStreamT & {name?: never; apiKey: string});
3272

3373
export interface Function {
3474
name: string;
@@ -59,6 +99,15 @@ interface ToolChoice {
5999
function: {name: string};
60100
}
61101

102+
interface Tools {
103+
type: 'function';
104+
function: {
105+
name: string;
106+
description?: string;
107+
parameters?: Record<string, any>;
108+
};
109+
}
110+
62111
interface PipeBaseOptions {
63112
name: string;
64113
description?: string;
@@ -75,14 +124,7 @@ interface PipeBaseOptions {
75124
presence_penalty?: number;
76125
frequency_penalty?: number;
77126
stop?: string[];
78-
tools?: {
79-
type: 'function';
80-
function: {
81-
name: string;
82-
description?: string;
83-
parameters?: Record<string, any>;
84-
};
85-
}[];
127+
tools?: Tools[];
86128
tool_choice?: 'auto' | 'required' | ToolChoice;
87129
parallel_tool_calls?: boolean;
88130
messages?: Message[];
@@ -113,16 +155,7 @@ export interface PipeListResponse {
113155
parallel_tool_calls: boolean;
114156
messages: Message[];
115157
variables: Variable[] | [];
116-
tools:
117-
| {
118-
type: 'function';
119-
function: {
120-
name: string;
121-
description?: string;
122-
parameters?: Record<string, any>;
123-
};
124-
}[]
125-
| [];
158+
tools: Tools[] | [];
126159
memory:
127160
| {
128161
name: string;
@@ -373,16 +406,20 @@ export class Langbase {
373406
);
374407
}
375408

376-
const pipe = new PipeBaseAI({
377-
apiKey: options.apiKey ?? this.apiKey,
378-
name: options.name?.trim() || '', // Pipe name
379-
prod: true,
380-
// default values
381-
model: 'openai:gpt-4o-mini',
382-
tools: [],
383-
} as any);
409+
// Remove stream property if it's not set to true
410+
if (typeof options.stream === 'undefined') {
411+
delete options.stream;
412+
}
384413

385-
return await pipe.run({...options, runTools: false});
414+
return this.request.post({
415+
endpoint: '/v1/pipes/run',
416+
body: options,
417+
headers: {
418+
...(options.llmKey && {
419+
'LB-LLM-KEY': options.llmKey,
420+
}),
421+
},
422+
});
386423
}
387424

388425
/**

packages/langbase/src/pipes/pipes.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Message, Role, ToolCall, Variable} from '@/langbase/langbase';
1+
import {Message, Role, ToolCall, Usage, Variable} from '@/langbase/langbase';
22
import {Request} from '../common/request';
33
import {Stream} from '../common/stream';
44

@@ -36,12 +36,6 @@ interface Delta {
3636
tool_calls?: ToolCall[];
3737
}
3838

39-
export interface Usage {
40-
prompt_tokens: number;
41-
completion_tokens: number;
42-
total_tokens: number;
43-
}
44-
4539
export interface GenerateResponse {
4640
completion: string;
4741
threadId?: string;

0 commit comments

Comments
 (0)