diff --git a/components/needle/actions/search-collection/search-collection.mjs b/components/needle/actions/search-collection/search-collection.mjs new file mode 100644 index 0000000000000..5f92ee0ffbd12 --- /dev/null +++ b/components/needle/actions/search-collection/search-collection.mjs @@ -0,0 +1,69 @@ +import app from "../../needle.app.mjs"; + +export default { + key: "needle-search-collection", + name: "Search Collection", + 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/).", + version: "0.0.1", + type: "action", + props: { + app, + collectionId: { + propDefinition: [ + app, + "collectionId", + ], + }, + text: { + type: "string", + label: "Search Query", + description: "The text to search for in the collection.", + }, + maxDistance: { + type: "string", + label: "Maximum Similarity Distance", + description: "Maximum similarity distance for the search results, between `0` and `1`. Eg. `0.65`.", + optional: true, + }, + topK: { + type: "integer", + label: "Maximum Number Of Results", + description: "The maximum number of search results to return.", + optional: true, + }, + }, + methods: { + searchCollection({ + collectionId, ...args + } = {}) { + return this.app.post({ + subdomain: "search.", + path: `/collections/${collectionId}/search`, + ...args, + }); + }, + }, + async run({ $ }) { + const { + searchCollection, + collectionId, + text, + maxDistance, + topK, + } = this; + + const response = await searchCollection({ + $, + collectionId, + data: { + text, + max_distance: maxDistance, + top_k: topK, + }, + }); + + $.export("$summary", "Successfully searched the collection."); + + return response; + }, +}; diff --git a/components/needle/common/constants.mjs b/components/needle/common/constants.mjs new file mode 100644 index 0000000000000..8f2c7eb9ecffc --- /dev/null +++ b/components/needle/common/constants.mjs @@ -0,0 +1,9 @@ +const SUBDOMAIN_PLACEHOLDER = "{subdomain}"; +const BASE_URL = `https://${SUBDOMAIN_PLACEHOLDER}needle-ai.com`; +const VERSION_PATH = "/api/v1"; + +export default { + SUBDOMAIN_PLACEHOLDER, + BASE_URL, + VERSION_PATH, +}; diff --git a/components/needle/needle.app.mjs b/components/needle/needle.app.mjs index 87bcd275b16a8..973547b51a1d4 100644 --- a/components/needle/needle.app.mjs +++ b/components/needle/needle.app.mjs @@ -1,11 +1,62 @@ +import { axios } from "@pipedream/platform"; +import constants from "./common/constants.mjs"; + export default { type: "app", app: "needle", - propDefinitions: {}, + propDefinitions: { + collectionId: { + type: "string", + label: "Collection ID", + description: "The ID of the collection to search in.", + async options() { + const { result: collections } = await this.listCollections(); + return collections.map(({ + id: value, + name: label, + }) => ({ + label, + value, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + getUrl(path, subdomain = "") { + const baseUrl = constants.BASE_URL.replace( + constants.SUBDOMAIN_PLACEHOLDER, + subdomain, + ); + return `${baseUrl}${constants.VERSION_PATH}${path}`; + }, + getHeaders(headers) { + return { + "Content-Type": "application/json", + "Accept": "application/json", + "x-api-key": this.$auth.api_key, + ...headers, + }; + }, + makeRequest({ + $ = this, path, headers, subdomain, ...args + } = {}) { + return axios($, { + ...args, + url: this.getUrl(path, subdomain), + headers: this.getHeaders(headers), + }); + }, + post(args) { + return this.makeRequest({ + method: "POST", + ...args, + }); + }, + listCollections(args = {}) { + return this.makeRequest({ + path: "/collections", + ...args, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/needle/package.json b/components/needle/package.json index f7d060d131030..a9f0d83c2fb56 100644 --- a/components/needle/package.json +++ b/components/needle/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/needle", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Needle Components", "main": "needle.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8d657cda35aec..014bce97b1f8c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8234,7 +8234,11 @@ importers: specifier: ^1.1.1 version: 1.6.6 - components/needle: {} + components/needle: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/neetoinvoice: dependencies: