汇总多个文档
假设我有一个包含多个.pdf
(或其他类型)文件的目录,我想对所有文件运行GenAIScript。
在这个例子中,我正在为每个文档生成一条吸引人的推文,并希望将推文保存在另一个文件中。
开发
使用命令面板中的
> GenAIScript: Create new script...
命令来创建新脚本。这是一个简单的脚本。假设脚本将文件作为参数,您可以在
env.files
中引用该参数,并告诉LLM如何处理它:gen-tweet.genai.mjs script({ title: "gen-tweet" })def("FILE", env.files)$`Given the paper in FILE, write a 140 character summary of the paperthat makes the paper sound exciting and encourages readers to look at it.`在VS Code资源管理器中右键点击文档(可以是
.pdf
、.docx
或.md
文件,因为def
知道如何读取和解析所有这些文件类型)。选择运行GenAIScript,然后选择你刚写的gen-tweet
脚本。假设我们给GenAIScript提供一篇描述GenAIScript的论文,输出将显示在新的文档标签页中。
探索GenAIScript:一种革命性的脚本语言,集成AI来自动化复杂任务,让编程对所有人触手可及!#AI #编程未来因为我们没有告诉大语言模型将输出写入文件,默认情况下它会输出到标准输出。
自动化
我们可以从命令行运行脚本:
终端窗口 npx genaiscript run gen-tweet example1.pdf输出将显示在终端中。
既然我们已经让脚本能够处理单个文件,现在就可以使用命令行将其应用到文件列表。假设您从
ex1.pdf
文件开始,希望将输出保存到新文件ex1.tweet.md
中。具体操作方式取决于您偏好的shell脚本。for file in *.pdf; donewfile="${file%.pdf}.tweet.md"; # foo.pdf -> foo.tweet.mdif [ ! -f "$newfile" ]; then # skip if already existsnpx genaiscript run gen-tweet $file > $newfilefidoneGet-ChildItem -Filter *.pdf | ForEach-Object {$newName = $_.BaseName + ".tweet.md"if (-not (Test-Path $newName)) {npx genaiscript run gen-tweet $_.FullName | Set-Content "$newName"}}import subprocess, sys, osfor input_file in sys.argv[1:]:output_file = os.path.splitext(input_file)[0] + '.tweet.md'if not os.path.exists(output_file):with open(output_file, 'w') as outfile:result = subprocess.check_output(["npx", "genaiscript", "run", "gen-tweet",input_file], universal_newlines=True)outfile.write(result)#!/usr/bin/env zximport "zx/globals"const files = await glob("*.pdf")for (const file of files) {const out = file.replace(/\.pdf$/i, ".tweet.md") // foo.pdf -> foo.tweet.mdif (!(await fs.exists(out)))// don't regenerate if it already existsawait $`genaiscript run gen-tweet ${file} > ${out}`}该脚本需要 zx。