Skip to content

Commit b79566c

Browse files
authored
📦 NEW: Web search tool support (#79)
* 📦 NEW: Web search tool support * 📦 NEW: Docs * 👌 IMPROVE: Readme * 👌 IMPROVE: Next.js example
1 parent e0cb723 commit b79566c

File tree

9 files changed

+132
-85
lines changed

9 files changed

+132
-85
lines changed

README.md

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ yarn add langbase
3232

3333
### Usage
3434

35-
You can [`generateText`](https://langbase.com/docs/langbase-sdk/generate-text) or [`streamText`](https://langbase.com/docs/langbase-sdk/stream-text) based on the type of a pipe.
35+
You can [`langbase.pipe.run()`](https://langbase.com/docs/sdk/pipe/run) to generate or stream from a Pipe.
3636

37-
Check our [SDK documentation](https://langbase.com/docs/langbase-sdk/overview) for more details.
37+
Check our [SDK documentation](https://langbase.com/docs/sdk) for more details.
3838

3939
### Example projects
4040

4141
Check the following examples:
4242

43-
- [Node: Generate Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/generate-text.ts)
44-
- [Node: Stream Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/stream-text.ts)
43+
- [Node: Generate Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/nodejs/examples/pipes/pipe.run.ts)
44+
- [Node: Stream Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/nodejs/examples/pipes/pipe.run.stream.ts)
4545
- [Next.js Example](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs)
4646
- TypeScript code
4747
- [React component](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/components/langbase) to display the response
@@ -52,69 +52,85 @@ Check the following examples:
5252

5353
## Node.js Examples
5454

55-
### Add a `.env` file with your Pipe API key
55+
### Add a `.env` file with your LANGBASE API key
5656

5757
```bash
5858
# Add your Pipe API key here.
59-
LANGBASE_PIPE_API_KEY="pipe_12345`"
59+
LANGBASE_API_KEY="your-api-key"
6060
```
6161

6262
---
6363

64-
### Generate text [`generateText()`](https://langbase.com/docs/langbase-sdk/generate-text)
64+
### Generate text [`langbase.pipe.run()`](https://langbase.com/docs/sdk/pipe/run)
6565

66-
For more check the API reference of [`generateText()`](https://langbase.com/docs/langbase-sdk/generate-text)
66+
Set the `stream` to `false`. For more, check the API reference of [`langbase.pipe.run()`](https://langbase.com/docs/langbase-sdk/generate-text)
6767

6868
```ts
6969
import 'dotenv/config';
70-
import {Pipe} from 'langbase';
70+
import {Langbase} from 'langbase';
7171

72-
// 1. Initiate the Pipe.
73-
const pipe = new Pipe({
74-
// Make sure you have a .env file with any pipe you wanna use.
75-
// As a demo we're using a pipe that has less wordy responses.
76-
apiKey: process.env.LANGBASE_PIPE_API_KEY!,
72+
// 1. Initiate the Langbase.
73+
const langbase = new Langbase({
74+
// Make sure you have a .env file with LANGBASE_API_KEY.
75+
apiKey: process.env.LANGBASE_API_KEY!,
7776
});
7877

79-
// 3. Generate the text by asking a question.
80-
const result = await pipe.generateText({
81-
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
82-
});
78+
async function main() {
79+
// 2. Run the pipe with a question.
80+
const response = await langbase.pipe.run({
81+
stream: false,
82+
name: 'summary' // pipe name to run
83+
messages: [
84+
{
85+
role: 'user',
86+
content: 'Who is an AI Engineer?',
87+
},
88+
],
89+
});
90+
91+
// 3. Print the response.
92+
console.log('response: ', response);
93+
}
8394

84-
// 4. Done: You got the generated completion.
85-
console.log(result.completion);
95+
main();
8696
```
8797

8898
---
8999

90-
### Stream text [`streamText()`](https://langbase.com/docs/langbase-sdk/stream-text)
100+
### Stream text [`langbase.pipe.run()`](https://langbase.com/docs/sdk/pipe/run)
91101

92-
For more check the API reference of [`streamText()`](https://langbase.com/docs/langbase-sdk/stream-text)
102+
Set the `stream` to `true`. For more, check the API reference of [`langbase.pipe.run()`](https://langbase.com/docs/langbase-sdk/generate-text)
93103

94104
```ts
95105
import 'dotenv/config';
96-
import {Pipe} from 'langbase';
106+
import {getRunner, Langbase} from 'langbase';
97107

98-
// 1. Initiate the Pipe.
99-
const pipe = new Pipe({
100-
// Make sure you have a .env file with any pipe you wanna use.
101-
// As a demo we're using a pipe that has less wordy responses.
102-
apiKey: process.env.LANGBASE_PIPE_API_KEY!,
108+
// 1. Initiate the Langbase.
109+
const langbase = new Langbase({
110+
// Make sure you have a .env file with LANGBASE_API_KEY.
111+
apiKey: process.env.LANGBASE_API_KEY!,
103112
});
104113

105-
// 2. Generate a stream by asking a question
106-
const stream = await pipe.streamText({
107-
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
108-
});
114+
async function main() {
115+
const userMsg = 'Who is an AI Engineer?';
109116

110-
// 3. Print the stream
111-
for await (const chunk of stream) {
112-
// Streaming text part — a single word or several.
113-
const textPart = chunk.choices[0]?.delta?.content || '';
117+
// 2. Run the pipe with a question.
118+
const {stream, threadId, rawResponse} = await langbase.pipe.run({
119+
stream: true,
120+
name: 'summary', // pipe name to run
121+
messages: [{role: 'user', content: userMsg}],
122+
});
114123

115-
// Demo: Print the stream — you can use it however.
116-
process.stdout.write(textPart);
124+
// 3. Get the runner and listen to the content.
125+
const runner = getRunner(stream);
126+
127+
// 4. Print the response.
128+
runner.on('content', content => {
129+
process.stdout.write(content);
130+
});
117131
}
132+
133+
main();
118134
```
119135

120-
Check out [more examples in the docs](https://langbase.com/docs/langbase-sdk/examples) →
136+
Check out [more examples in the docs](https://langbase.com/docs/sdk/examples)

examples/nextjs/app/langbase/pipe/generate-text/route.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/nextjs/app/langbase/pipe/stream-text/route.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/nextjs/app/page.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import GenerateTextExample from '@/components/langbase/generate-text';
21
import RunNonStreamExample from '@/components/langbase/run';
32
import RunStreamExample from '@/components/langbase/run-stream';
4-
import StreamTextExample from '@/components/langbase/stream-text';
53

64
export default function Home() {
75
return (
@@ -17,8 +15,6 @@ export default function Home() {
1715
</div>
1816
<RunStreamExample />
1917
<RunNonStreamExample />
20-
<GenerateTextExample />
21-
<StreamTextExample />
2218
</div>
2319
</div>
2420
);

examples/nodejs/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
PIPE_LESS_WORDY=""
33
LANGBASE_SDK_GENERATE_PIPE=""
44
LANGBASE_SDK_CHAT_PIPE=""
5+
LANGBASE_API_KEY=""
6+
WEB_SEARCH_KEY=""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const results = await langbase.tool.webSearch({
10+
query: 'AI Engineer',
11+
apiKey: process.env.WEB_SEARCH_KEY,
12+
});
13+
14+
console.log(results);
15+
}
16+
17+
main();

examples/nodejs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"generate-text-chat-pipe": "npx tsx ./examples/pipes/generate-text-chat-pipe.ts",
2828
"stream-text": "npx tsx ./examples/pipes/stream-text.ts",
2929
"stream-text-generate-pipe": "npx tsx ./examples/pipes/stream-text-generate-pipe.ts",
30-
"stream-text-chat-pipe": "npx tsx ./examples/pipes/stream-text-chat-pipe.ts"
30+
"stream-text-chat-pipe": "npx tsx ./examples/pipes/stream-text-chat-pipe.ts",
31+
"tools.web-search": "npx tsx ./examples/tools/web-search.ts"
3132
},
3233
"keywords": [],
3334
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",

packages/langbase/src/langbase/langbase.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ export interface LangbaseOptions {
248248
apiKey: string;
249249
}
250250

251+
export interface ToolWebSearchOptions {
252+
query: string;
253+
total_results?: number;
254+
domains?: string[];
255+
apiKey?: string;
256+
}
257+
258+
export interface ToolWebSearchResponse {
259+
url: string;
260+
content: string;
261+
}
262+
251263
export class Langbase {
252264
private request: Request;
253265
private apiKey: string;
@@ -283,6 +295,12 @@ export class Langbase {
283295
};
284296
};
285297

298+
public tool: {
299+
webSearch: (
300+
options: ToolWebSearchOptions,
301+
) => Promise<ToolWebSearchResponse[]>;
302+
};
303+
286304
constructor(options?: LangbaseOptions) {
287305
const baseUrl = 'https://api.langbase.com';
288306
this.apiKey = options?.apiKey ?? '';
@@ -311,6 +329,10 @@ export class Langbase {
311329
},
312330
},
313331
};
332+
333+
this.tool = {
334+
webSearch: this.webSearch.bind(this),
335+
};
314336
}
315337

316338
private async runPipe(
@@ -526,4 +548,25 @@ export class Langbase {
526548
endpoint: `/v1/memory/${options.memoryName}/documents/${options.documentName}/embeddings/retry`,
527549
});
528550
}
551+
552+
/**
553+
* Performs a web search using the Langbase API.
554+
*
555+
* @param options - Web search configuration options
556+
* @param options.apiKey - Optional API key for web search authentication
557+
* @returns Promise that resolves to an array of web search results
558+
*/
559+
private async webSearch(
560+
options: ToolWebSearchOptions,
561+
): Promise<ToolWebSearchResponse[]> {
562+
return this.request.post({
563+
endpoint: '/v1/tools/web-search',
564+
body: options,
565+
headers: {
566+
...(options.apiKey && {
567+
'LB-WEB-SEARCH-KEY': options.apiKey,
568+
}),
569+
},
570+
});
571+
}
529572
}

packages/langbase/src/pipes/pipes.ts

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

5-
65
export interface GenerateOptions {
76
messages?: Message[];
87
variables?: Variable[];
@@ -84,13 +83,25 @@ export class Pipe {
8483
this.request = new Request({apiKey: options.apiKey, baseUrl});
8584
}
8685

86+
/**
87+
* @deprecated This method is deprecated and will be removed in a future version.
88+
*
89+
* Please use `langbase.pipe.run()` instead
90+
* @see https://langbase.com/docs/sdk/pipe/run
91+
*/
8792
async generateText(options: GenerateOptions): Promise<GenerateResponse> {
8893
return this.request.post<GenerateResponse>({
8994
endpoint: options.chat ? '/beta/chat' : '/beta/generate',
9095
body: {...options, stream: false},
9196
});
9297
}
9398

99+
/**
100+
* @deprecated This method is deprecated and will be removed in a future version.
101+
*
102+
* Please use `langbase.pipe.run()` instead
103+
* @see https://langbase.com/docs/sdk/pipe/run
104+
*/
94105
async streamText(options: StreamOptions): Promise<StreamResponse> {
95106
return this.request.post<StreamResponse>({
96107
endpoint: options.chat ? '/beta/chat' : '/beta/generate',

0 commit comments

Comments
 (0)