forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonTasks.ts
More file actions
68 lines (55 loc) · 1.61 KB
/
pythonTasks.ts
File metadata and controls
68 lines (55 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { logger, schemaTask, task } from "@trigger.dev/sdk/v3";
import { python } from "@trigger.dev/python";
import { z } from "zod";
export const convertUrlToMarkdown = schemaTask({
id: "convert-url-to-markdown",
schema: z.object({
url: z.string().url(),
}),
run: async (payload) => {
const result = await python.runScript("./src/python/html2text_url.py", [payload.url]);
logger.debug("convert-url-to-markdown", {
url: payload.url,
output: result.stdout,
});
const streamingResult = python.stream.runScript("./src/python/html2text_url.py", [payload.url]);
for await (const chunk of streamingResult) {
logger.debug("convert-url-to-markdown", {
url: payload.url,
chunk,
});
}
},
});
export const pythonRunInlineTask = task({
id: "python-run-inline",
run: async () => {
const result = await python.runInline(
`
import os
import html2text as h2t
h = h2t.HTML2Text()
print(h.handle("<p>Hello, <a href='https://www.google.com/earth/'>world</a>!"))
print(f"API Key: {os.environ['OPENAI_API_KEY']}")
`,
{
env: {
OPENAI_API_KEY: "sk-1234567890",
},
}
);
console.log(result.stdout);
const streamingResult = python.stream.runInline(`
import html2text as h2t
h = h2t.HTML2Text()
print(h.handle("<p>Hello, <a href='https://www.google.com/earth/'>world</a>!"))
print(h.handle("<p>Hello, <a href='https://www.google.com/earth/'>world</a>!"))
`);
for await (const chunk of streamingResult) {
logger.debug("python-run-inline", {
chunk,
});
}
return result.stdout;
},
});