Skip to content

Commit 857cd15

Browse files
committed
[Components] needle - new action component
1 parent 79be8f3 commit 857cd15

File tree

4 files changed

+138
-20
lines changed

4 files changed

+138
-20
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import app from "../../needle.app.mjs";
2+
3+
export default {
4+
key: "needle-search-collection",
5+
name: "Search Collection",
6+
description: "Search a collection for relevant data chunks based on a query. [See the documentation](https://docs.needle-ai.com/docs/api-reference/search-collection/).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
collectionId: {
12+
propDefinition: [
13+
app,
14+
"collectionId",
15+
],
16+
},
17+
text: {
18+
type: "string",
19+
label: "Search Query",
20+
description: "The text to search for in the collection.",
21+
},
22+
maxDistance: {
23+
type: "string",
24+
label: "Maximum Similarity Distance",
25+
description: "Maximum similarity distance for the search results, between `0` and `1`.",
26+
default: "0.65",
27+
optional: true,
28+
},
29+
topK: {
30+
type: "integer",
31+
label: "Maximum Number Of Results",
32+
description: "The maximum number of search results to return.",
33+
default: 5,
34+
optional: true,
35+
},
36+
},
37+
methods: {
38+
searchCollection({
39+
collectionId, ...args
40+
} = {}) {
41+
return this.app.post({
42+
path: `/collections/${collectionId}/search`,
43+
...args,
44+
});
45+
},
46+
},
47+
async run({ $ }) {
48+
const {
49+
searchCollection,
50+
collectionId,
51+
text,
52+
maxDistance,
53+
topK,
54+
} = this;
55+
56+
const response = await searchCollection({
57+
$,
58+
collectionId,
59+
data: {
60+
text,
61+
max_distance: maxDistance,
62+
top_k: topK,
63+
},
64+
});
65+
66+
$.export("$summary", "Successfully searched the collection.");
67+
68+
return response;
69+
},
70+
};

components/needle/needle.app.mjs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,58 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "needle",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
collectionId: {
8+
type: "string",
9+
label: "Collection ID",
10+
description: "The ID of the collection to search in.",
11+
async options() {
12+
const { result: collections } = await this.listCollections();
13+
return collections.map(({
14+
id: value,
15+
name: label,
16+
}) => ({
17+
label,
18+
value,
19+
}));
20+
},
21+
},
22+
},
523
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
24+
getUrl(path) {
25+
return `https://needle-ai.com/api/v1${path}`;
26+
},
27+
getHeaders(headers) {
28+
return {
29+
"Content-Type": "application/json",
30+
"Accept": "application/json",
31+
"x-api-key": this.$auth.api_key,
32+
...headers,
33+
};
34+
},
35+
makeRequest({
36+
$ = this, path, headers, ...args
37+
} = {}) {
38+
return axios($, {
39+
...args,
40+
debug: true,
41+
url: this.getUrl(path),
42+
headers: this.getHeaders(headers),
43+
});
44+
},
45+
post(args) {
46+
return this.makeRequest({
47+
method: "POST",
48+
...args,
49+
});
50+
},
51+
listCollections(args = {}) {
52+
return this.makeRequest({
53+
path: "/collections",
54+
...args,
55+
});
956
},
1057
},
11-
};
58+
};

components/needle/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/needle",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Needle Components",
55
"main": "needle.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
15-
}
18+
}

pnpm-lock.yaml

Lines changed: 11 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)