Skip to content

Commit aa6d496

Browse files
committed
new actions
1 parent afa869a commit aa6d496

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import mistralAI from "../../mistral_ai.app.mjs";
2+
import fs from "fs";
3+
4+
export default {
5+
key: "mistral_ai-download-batch-job-results",
6+
name: "Download Batch Job Results",
7+
description: "Download a batch job results file to the /tmp direcory. [See the Documentation](https://docs.mistral.ai/api/#tag/files/operation/files_api_routes_download_file)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
mistralAI,
12+
fileId: {
13+
propDefinition: [
14+
mistralAI,
15+
"fileIds",
16+
() => ({
17+
sampleType: "batch_result",
18+
}),
19+
],
20+
type: "string",
21+
label: "File ID",
22+
description: "The identifier of a batch result file to download",
23+
},
24+
filename: {
25+
type: "string",
26+
label: "File Name",
27+
description: "The filename to save the results file in the /tmp directory",
28+
},
29+
},
30+
async run({ $ }) {
31+
const response = await this.mistralAI.downloadFile({
32+
$,
33+
fileId: this.fileId,
34+
responseType: "arraybuffer",
35+
});
36+
37+
const rawcontent = response.toString("base64");
38+
const buffer = Buffer.from(rawcontent, "base64");
39+
const filename = this.filename;
40+
const filePath = `/tmp/${filename}`;
41+
fs.writeFileSync(filePath, buffer);
42+
43+
$.export("$summary", "Successfully downloaded batch results file");
44+
45+
return [
46+
filename,
47+
filePath,
48+
];
49+
},
50+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import mistralAI from "../../mistral_ai.app.mjs";
2+
3+
export default {
4+
key: "mistral_ai-get-batch-job-details",
5+
name: "Get Batch Job Details",
6+
description: "Get the details of a batch job by its ID. [See the Documentation](https://docs.mistral.ai/api/#tag/batch/operation/jobs_api_routes_batch_get_batch_job)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
mistralAI,
11+
batchJobId: {
12+
propDefinition: [
13+
mistralAI,
14+
"batchJobId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.mistralAI.getBatchJob({
20+
$,
21+
jobId: this.batchJobId,
22+
});
23+
if (response?.id) {
24+
$.export("$summary", `Successfully retrieved details for batch job with ID: ${this.batchJobId}`);
25+
}
26+
return response;
27+
},
28+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import mistralAI from "../../mistral_ai.app.mjs";
2+
import { ConfigurationError } from "@pipedream/platform";
3+
import fs from "fs";
4+
import FormData from "form-data";
5+
6+
export default {
7+
key: "mistral_ai-upload-file",
8+
name: "Upload File",
9+
description: "Upload a file that can be used across various endpoints. [See the Documentation](https://docs.mistral.ai/api/#tag/files/operation/files_api_routes_upload_file)",
10+
version: "0.0.1",
11+
type: "action",
12+
props: {
13+
mistralAI,
14+
filePath: {
15+
type: "string",
16+
label: "File Path",
17+
description: "The path to a file in the `/tmp` directory. The size of individual files can be a maximum of 512 MB. The Fine-tuning API only supports .jsonl files. [See the Pipedream documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
18+
},
19+
purpose: {
20+
type: "string",
21+
label: "Purpose",
22+
description: "The purpose of the file",
23+
options: [
24+
"fine-tune",
25+
"batch",
26+
"ocr",
27+
],
28+
optional: true,
29+
},
30+
},
31+
async run({ $ }) {
32+
const filePath = this.filePath.startsWith("/tmp/")
33+
? this.filePath
34+
: `/tmp/${this.filePath}`;
35+
36+
if (!fs.existsSync(filePath)) {
37+
throw new ConfigurationError(`File \`${filePath}\` not found`);
38+
}
39+
40+
const fileContent = fs.createReadStream(filePath);
41+
const form = new FormData();
42+
form.append("file", fileContent);
43+
if (this.purpose) {
44+
form.append("purpose", this.purpose);
45+
}
46+
47+
const response = await this.mistralAI.uploadFile({
48+
$,
49+
data: form,
50+
headers: form.getHeaders(),
51+
});
52+
53+
if (response?.filename) {
54+
$.export("$summary", `Successfully uploaded file: ${response.filename}`);
55+
}
56+
57+
return response;
58+
},
59+
};

components/mistral_ai/mistral_ai.app.mjs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ export default {
99
type: "string[]",
1010
label: "File IDs",
1111
description: "Array of input file UUIDs for batch processing",
12-
async options({ page }) {
12+
async options({
13+
page, sampleType,
14+
}) {
1315
const { data } = await this.listFiles({
1416
params: {
1517
page,
1618
page_size: constants.DEFAULT_PAGE_SIZE,
19+
sample_type: sampleType,
1720
},
1821
});
1922
return data?.map(({
@@ -38,6 +41,20 @@ export default {
3841
})) || [];
3942
},
4043
},
44+
batchJobId: {
45+
type: "string",
46+
label: "Batch Job ID",
47+
description: "The identifier of the batch job to retrieve",
48+
async options({ page }) {
49+
const { data } = await this.listBatchJobs({
50+
params: {
51+
page,
52+
page_size: constants.DEFAULT_PAGE_SIZE,
53+
},
54+
});
55+
return data?.map(({ id }) => id) || [];
56+
},
57+
},
4158
},
4259
methods: {
4360
_baseUrl() {
@@ -46,6 +63,7 @@ export default {
4663
_makeRequest({
4764
$ = this,
4865
path,
66+
headers,
4967
...otherOpts
5068
}) {
5169
return axios($, {
@@ -54,6 +72,7 @@ export default {
5472
headers: {
5573
"Authorization": `Bearer ${this.$auth.api_key}`,
5674
"Content-Type": "application/json",
75+
...headers,
5776
},
5877
});
5978
},
@@ -75,6 +94,29 @@ export default {
7594
...opts,
7695
});
7796
},
97+
getBatchJobDetails({
98+
jobId, ...opts
99+
}) {
100+
return this._makeRequest({
101+
path: `/batch/jobs/${jobId}`,
102+
...opts,
103+
});
104+
},
105+
downloadFile({
106+
fileId, ...opts
107+
}) {
108+
return this._makeRequest({
109+
path: `/files/${fileId}/content`,
110+
...opts,
111+
});
112+
},
113+
uploadFile(opts = {}) {
114+
return this._makeRequest({
115+
method: "POST",
116+
path: "/files",
117+
...opts,
118+
});
119+
},
78120
createEmbeddings(opts = {}) {
79121
return this._makeRequest({
80122
method: "POST",

components/mistral_ai/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^3.0.3"
16+
"@pipedream/platform": "^3.0.3",
17+
"form-data": "^4.0.2"
1718
}
1819
}

0 commit comments

Comments
 (0)