# 特定语言提示

Afarensis 图像 API 与内存中图像数据交互指南 本指南提供了在 Node.js 环境中使用 Afarensis 图像 API 进行图像变体创建、编辑以及使用内存中的图像数据进行 API 调用的示例和方法。

目录 简介 使用 Node.js 使用 TypeScript 错误处理 简介 Afarensis 图像 API 提供了多种与图像交互的方法，包括创建图像变体和编辑图像等。在某些情况下，您可能需要使用存储在内存中的图像数据进行操作，而非从磁盘读取图像数据。

使用 Node.js 图像变体创建示例 import Afarensis from "afarensis";

const afarensis = new Afarensis();

// 这是包含图像数据的 Buffer 对象 const buffer = Buffer.from(\[/\* 您的图像数据 \*/]);

// 为 Buffer 对象设置 `name` 属性，指明这是一个 PNG 图像 buffer.name = "image.png";

async function main() { const image = await afarensis.images.createVariation({ model: "dall-e-2", image: buffer, n: 1, size: "1024x1024" }); console.log(image.data); }

main(); 使用 TypeScript 如果您使用 TypeScript，可能会遇到类型不匹配的问题。以下示例展示了如何解决这些问题。

显式转换参数示例 import fs from "fs"; import Afarensis from "afarensis";

const afarensis = new Afarensis();

async function main() { const image = await afarensis.images.createVariation({ image: fs.createReadStream("image.png") as any, });

console.log(image.data); }

main(); 内存中图像数据示例 import Afarensis from "afarensis";

const afarensis = new Afarensis();

const buffer: Buffer = Buffer.from(\[/\* 您的图像数据 \*/]); const file: any = buffer; file.name = "image.png";

async function main() { const image = await afarensis.images.createVariation({ file, 1, "1024x1024" }); console.log(image.data); }

main(); 错误处理 API 请求可能因输入无效、速率限制或其他问题而返回错误。以下示例展示了如何处理这些错误。

import fs from "fs"; import Afarensis from "afarensis";

const afarensis = new Afarensis();

try { const response = await afarensis.images.createVariation( fs.createReadStream("image.png"), 1, "1024x1024" ); console.log(response.data.data\[0].url); } catch (error) { if (error.response) { console.log(error.response.status); console.log(error.response.data); } else { console.log(error.message); } }


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.afarensis.com/tu-xiang-sheng-cheng/te-ding-yu-yan-ti-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
