Skip to content

Commit 3dc9601

Browse files
authored
New Components - hathr_ai (#16592)
* new components * pnpm-lock.yaml
1 parent a58e7e0 commit 3dc9601

File tree

8 files changed

+246
-7
lines changed

8 files changed

+246
-7
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import hathrAi from "../../hathr_ai.app.mjs";
2+
3+
export default {
4+
key: "hathr_ai-chat",
5+
name: "Send Chat Message",
6+
description: "Sends a chat message using Hathr AI. [See the documentation](https://drive.google.com/drive/folders/1jtoSXqzhe-iwf9kfUwTCVQBu4iXVJO2x?usp=sharing)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
hathrAi,
11+
message: {
12+
type: "string",
13+
label: "Message",
14+
description: "The message to send in the chat request",
15+
},
16+
documents: {
17+
propDefinition: [
18+
hathrAi,
19+
"documents",
20+
],
21+
},
22+
temperature: {
23+
type: "string",
24+
label: "Temperature",
25+
description: "Controls randomness (Optional, default: 0.2, range: 0-2.0)",
26+
optional: true,
27+
},
28+
topP: {
29+
type: "string",
30+
label: "Top P",
31+
description: "Controls diversity (Optional, default: 1.0, range: 0-1.0)",
32+
optional: true,
33+
},
34+
},
35+
async run({ $ }) {
36+
const opts = {
37+
$,
38+
data: {
39+
messages: [
40+
{
41+
role: "user",
42+
text: this.message,
43+
},
44+
],
45+
temperature: this.temperature,
46+
topP: this.topP,
47+
},
48+
};
49+
const { response } = this.documents
50+
? await this.hathrAi.chatWithDocuments({
51+
...opts,
52+
data: {
53+
...opts.data,
54+
documents: this.documents,
55+
},
56+
})
57+
: await this.hathrAi.chat(opts);
58+
59+
$.export("$summary", `Chat request sent successfully with message: "${this.message}"`);
60+
61+
return response;
62+
},
63+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import hathrAi from "../../hathr_ai.app.mjs";
2+
3+
export default {
4+
key: "hathr_ai-list-documents",
5+
name: "List Documents",
6+
description: "Retrieves a list of all available documents. [See the documentation](https://drive.google.com/drive/folders/1jtoSXqzhe-iwf9kfUwTCVQBu4iXVJO2x?usp=sharing)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
hathrAi,
11+
},
12+
async run({ $ }) {
13+
const { response: { documents } } = await this.hathrAi.listDocuments({
14+
$,
15+
});
16+
$.export("$summary", `Successfully retrieved ${documents.length} document${documents.length === 1
17+
? ""
18+
: "s"}`);
19+
return documents;
20+
},
21+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import hathrAi from "../../hathr_ai.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
import { checkTmp } from "../../common/utils.mjs";
4+
import mime from "mime-types";
5+
import fs from "fs";
6+
7+
export default {
8+
key: "hathr_ai-upload-document",
9+
name: "Upload Document",
10+
description: "Uploads a document that can be used in future chat requests. [See the documentation](https://drive.google.com/drive/folders/1jtoSXqzhe-iwf9kfUwTCVQBu4iXVJO2x?usp=sharing)",
11+
version: "0.0.1",
12+
type: "action",
13+
props: {
14+
hathrAi,
15+
filePath: {
16+
type: "string",
17+
label: "File Path",
18+
description: "The path to a file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
19+
},
20+
filename: {
21+
type: "string",
22+
label: "Filename",
23+
description: "The name of the file to be uploaded",
24+
},
25+
},
26+
async run({ $ }) {
27+
const filePath = checkTmp(this.filePath);
28+
const fileBuffer = fs.readFileSync(filePath);
29+
const mimeType = mime.lookup(filePath);
30+
31+
const { response: { signedUrl } } = await this.hathrAi.getUploadUrl({
32+
$,
33+
data: {
34+
filename: this.filename,
35+
type: mimeType,
36+
},
37+
});
38+
39+
await axios($, {
40+
method: "PUT",
41+
url: signedUrl,
42+
data: fileBuffer,
43+
headers: {
44+
"Content-Type": mimeType,
45+
},
46+
});
47+
48+
$.export("$summary", "Successfully uploaded document.");
49+
return signedUrl;
50+
},
51+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const checkTmp = (filename) => {
2+
if (!filename.startsWith("/tmp")) {
3+
return `/tmp/${filename}`;
4+
}
5+
return filename;
6+
};
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,61 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "hathr_ai",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
documents: {
8+
type: "string[]",
9+
label: "Documents",
10+
description: "Array of document names to use as context",
11+
optional: true,
12+
async options() {
13+
const { response: { documents } } = await this.listDocuments();
14+
return documents?.map(({ name }) => name ) || [];
15+
},
16+
},
17+
},
518
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
19+
_baseUrl() {
20+
return "https://api.hathr.ai/v1";
21+
},
22+
_makeRequest({
23+
$ = this, path, ...otherOpts
24+
}) {
25+
return axios($, {
26+
...otherOpts,
27+
url: `${this._baseUrl()}${path}`,
28+
headers: {
29+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
30+
},
31+
});
32+
},
33+
listDocuments(opts = {}) {
34+
return this._makeRequest({
35+
path: "/document/list",
36+
...opts,
37+
});
38+
},
39+
chat(opts = {}) {
40+
return this._makeRequest({
41+
method: "POST",
42+
path: "/chat",
43+
...opts,
44+
});
45+
},
46+
chatWithDocuments(opts = {}) {
47+
return this._makeRequest({
48+
method: "POST",
49+
path: "/document/chat",
50+
...opts,
51+
});
52+
},
53+
getUploadUrl(opts = {}) {
54+
return this._makeRequest({
55+
method: "POST",
56+
path: "/document/upload",
57+
...opts,
58+
});
959
},
1060
},
1161
};

components/hathr_ai/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/hathr_ai",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Hathr AI Components",
55
"main": "hathr_ai.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"mime-types": "^3.0.1"
1418
}
15-
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import hathrAi from "../../hathr_ai.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
key: "hathr_ai-new-document-created",
6+
name: "New Document Created",
7+
description: "Emit new event when a new document is created. [See the documentation](https://drive.google.com/drive/folders/1jtoSXqzhe-iwf9kfUwTCVQBu4iXVJO2x?usp=sharing)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
hathrAi,
13+
db: "$.service.db",
14+
timer: {
15+
type: "$.interface.timer",
16+
default: {
17+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
18+
},
19+
},
20+
},
21+
methods: {
22+
generateMeta(doc) {
23+
return {
24+
id: doc.name,
25+
summary: `New Document Created: ${doc.name}`,
26+
ts: Date.now(),
27+
};
28+
},
29+
},
30+
async run() {
31+
const { response: { documents } } = await this.hathrAi.listDocuments();
32+
for (const doc of documents) {
33+
const meta = this.generateMeta(doc);
34+
this.$emit(doc, meta);
35+
}
36+
},
37+
};

pnpm-lock.yaml

Lines changed: 8 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)