跳至内容

博客

博客图片

我们为博客生成封面图片,这本身完全没什么意思...但生成这些图片的脚本值得一看。

博客文章封面的生成分为3个阶段:

  • 将博客的Markdown内容转换为图像提示
  • 根据图像提示生成一张图片
  • 根据图像提示生成替代文本描述
  • 调整大小、复制图片并修补博客文章的前置元数据
blog-image.genai.mts
script({ parameters: { force: false } })
const file = env.files.find(({ filename }) => /\.mdx?$/.test(filename))
const target = path.changeext(file.filename, ".png")
if (!env.vars.force && (await workspace.stat(target)))
cancel("blog image already exists")
// phase 1: generate image prompt
const style =
"iconic, 2D, 8-bit, corporate, 5-color, simple, geometric, no people, no text"
const { text: imagePrompt } = await runPrompt(
(_) => {
_.def("BLOG_POST", MD.content(file.content))
_.$`Generate an image prompt for DALLE-3 that illustrates the contents of <BLOG_POST>.
Include specific description related to the content of <BLOG_POST>.
${style}`
},
{ responseType: "text", systemSafety: false }
)
// phase 2: generate image
const image = await generateImage(
`${imagePrompt}
STYLE:
${style}`,
{
mime: "image/png",
size: "1792x1024",
scale: 768 / 1792,
maxHeight: 762,
style: "vivid",
}
)
// phase 3: generate alt text
const { text: alt } = await prompt`
Generate an alt text description from <IMAGE_PROMPT>.
Rephrase the prompt in a way that would be useful for someone who cannot see the image.
Do not start with "Alt Text:".
IMAGE_PROMPT:
${imagePrompt}`.options({ responseType: "text", systemSafety: false })
// phase 4: patch fronmatter
const fm = MD.frontmatter(file.content)
fm.cover = {
alt,
image: "./" + path.basename(target),
}
file.content = MD.updateFrontmatter(file.content, fm)
// phase 5: save files
await workspace.copyFile(image.image.filename, target)
await workspace.writeFiles(file)

当这个脚本处理了几篇文章后,使用convert命令为所有博客文章生成图片。

Terminal window
genaiscript convert blog-image blog/*.md*

图片怎么处理?

这些图像有些抽象,但它们是根据博客文章内容生成的。 图像提示肯定还有改进空间,但这已经是一个良好的开端。

将脚本作为MCP工具使用!

🚀 模型上下文协议(MCP)正在科技界掀起风暴,我们非常激动地宣布GenAIScript处于这场革命的最前沿!

随着MCP的快速普及,像GitHub Copilot Chat这样的工具已经开始集成支持(目前已在Insiders版本中提供),而Copilot Studio也刚刚宣布了他们的支持。

为了跟上这些激动人心的技术进步,GenAIScript现在允许您将脚本公开为MCP工具。想象一下这种可能性!MCP工具的功能类似于LLM工具,语言模型(LLM)会决定何时调用它们,从而使您的开发过程更加智能高效。

探索GenAIScript与MCP带来的脚本编程未来。查看文档开始使用。

Azure AI Search

retrieval API 已扩展支持 Azure AI Search。 这使您能够使用嵌入将文件索引到向量数据库中,用于相似性搜索。 这通常被称为检索增强生成(RAG)。

// index creation
const index = retrieval.index("animals", { type: "azure_ai_search" })
// indexing
await index.insertOrUpdate(env.files)
// search
const res = await index.search("cat dog")
def("RAG", res)

GenAIScript 提供了一种简单高效的方式来与 Azure AI 搜索进行交互。它将处理 文件的分块、向量化和索引操作。retrieval.index 函数会创建一个具有 指定名称和类型的索引。insertOrUpdate 函数将文件索引到数据库中。 最后,search 函数会检索与查询匹配的文件。

也可以使用命令行界面提前索引文件。

超级增强Copilot聊天

想知道一个让GitHub Copilot Chat成为GenAIScript专家的绝妙技巧吗? 这里教你如何用一个简单方法来增强你的Copilot聊天功能。

将您的完整文档添加到聊天会话中!

听起来很疯狂?其实不然!GenAIScript包含了无数API使用示例和案例。只需要将其压缩以适应上下文窗口即可。

如何尝试这个?

随着GenAIScript最新版本的发布,您现在可以在聊天会话中添加一个genaiscript提示。 这个由GenAIScript团队精心设计的提示,会将GenAIScript文档包含到上下文中,以帮助LLM提供商提供更好的答案。

播放

它是如何工作的?

最新版GitHub Copilot Chat的发布新增了对可复用提示的支持。 GitHub Copilot Chat还添加了对本地工作区索引的支持,这有助于处理大量上下文。

GenAIScript通过添加包含GenAIScript文档的自定义提示来利用这些功能。

.genaiscript/prompts/genaiscript.prompt.md
## Role
You are an expert at the GenAIScript programming language (https://microsoft.github.io/genaiscript). Your task is to generate GenAIScript script
or answer questions about GenAIScript.
## Reference
- [GenAIScript docs](../../.genaiscript/docs/llms-full.txt)
- [GenAIScript ambient type definitions](../../.genaiscript/genaiscript.d.ts)
## Guidance for Code Generation
- you always generate TypeScript code using ESM models for Node.JS.
- you prefer using APIs from GenAIScript 'genaiscript.d.ts' rather node.js. Avoid node.js imports.
- you keep the code simple, avoid exception handlers or error checking.
- you add TODOs where you are unsure so that the user can review them
- you use the global types in genaiscript.d.ts are already loaded in the global context, no need to import them.
- save generated code in the `./genaisrc` folder with `.genai.mts` extension

未完待续

这项技术非常新颖,可能还有很多改进的空间。

Make it better!

GenAIScript附带一个助手,可以指示LLM"使其更好"。 这是一种通过多次重复一组指令来改进代码的惊人方式。

代码解释

让我们逐行解析这个脚本:

import { makeItBetter } from "genaiscript/runtime"

这行代码从GenAIScript运行时导入了makeItBetter函数。该函数用于通过多次重复一组指令来改进代码。

def("CODE", env.files)

这一行定义了一个名为“CODE”的常量,用于表示环境中的文件。它本质上为需要改进的代码设置了上下文。

$`Analyze and improve the code.`

这一行是给AI模型的提示语。它指示系统分析和增强代码。$用于表示这是一个特殊指令,而非常规代码命令。

// tell the LLM to 'make it better' 2 times

这段注释解释了接下来的代码行,明确指出makeItBetter函数将被调用两次。

makeItBetter({ repeat: 2 })

这行代码调用makeItBetter函数,并设置选项将改进过程重复两次。它会触发增强流程。

如何运行脚本

要使用GenAIScript CLI运行此脚本,您需要在终端中执行以下命令:

Terminal window
genaiscript run makeitbetter

有关安装和设置GenAIScript CLI的详细说明,请查看GenAIScript文档

遵循这些简单的步骤,您就能轻松利用AI来优化您的代码!🌟