Skip to content

Commit 108a440

Browse files
authored
[Components] needle - new action component (#15761)
1 parent 6ae6d67 commit 108a440

File tree

5 files changed

+144
-8
lines changed

5 files changed

+144
-8
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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`. Eg. `0.65`.",
26+
optional: true,
27+
},
28+
topK: {
29+
type: "integer",
30+
label: "Maximum Number Of Results",
31+
description: "The maximum number of search results to return.",
32+
optional: true,
33+
},
34+
},
35+
methods: {
36+
searchCollection({
37+
collectionId, ...args
38+
} = {}) {
39+
return this.app.post({
40+
subdomain: "search.",
41+
path: `/collections/${collectionId}/search`,
42+
...args,
43+
});
44+
},
45+
},
46+
async run({ $ }) {
47+
const {
48+
searchCollection,
49+
collectionId,
50+
text,
51+
maxDistance,
52+
topK,
53+
} = this;
54+
55+
const response = await searchCollection({
56+
$,
57+
collectionId,
58+
data: {
59+
text,
60+
max_distance: maxDistance,
61+
top_k: topK,
62+
},
63+
});
64+
65+
$.export("$summary", "Successfully searched the collection.");
66+
67+
return response;
68+
},
69+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const SUBDOMAIN_PLACEHOLDER = "{subdomain}";
2+
const BASE_URL = `https://${SUBDOMAIN_PLACEHOLDER}needle-ai.com`;
3+
const VERSION_PATH = "/api/v1";
4+
5+
export default {
6+
SUBDOMAIN_PLACEHOLDER,
7+
BASE_URL,
8+
VERSION_PATH,
9+
};

components/needle/needle.app.mjs

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

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: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)