Sure, the simplified Chinese translation of "Easy ChatGPT API" while keeping HTML structure intact would be: ```html 简易ChatGPT API ```

Certainly! Here is the translated text in simplified Chinese while keeping the HTML structure intact: ```html OpenAIHelper 用于无缝 ChatGPT API 交互 ```

在本文中,我们将探讨一种用 TypeScript 与 OpenAI API 交互的自定义辅助类。OpenAIHelper 类简化了对 OpenAI API 的请求,包括提示文本和图像输入。

Sure, here is the translation of "Introduction to OpenAIHelper" in simplified Chinese while keeping the HTML structure: ```html

OpenAIHelper简介

```

Sure, here's the translation of the text into simplified Chinese while keeping the HTML structure: ```html

The OpenAIHelper class is designed to streamline the process of interacting with OpenAI's API. It handles API initialization, parameter validation, and response parsing, making it easier to integrate OpenAI's powerful models into your applications.

``` Translated to simplified Chinese: ```html

OpenAIHelper 类被设计用来简化与 OpenAI API 的交互过程。它处理 API 初始化、参数验证和响应解析,使得将 OpenAI 强大的模型集成到您的应用程序中变得更加容易。

``` This maintains the structure of the HTML while providing the translated text.

Sure, the translation of "Implementation" into simplified Chinese, while keeping the HTML structure, would look like this: ```html 实施 ``` This HTML code embeds the Chinese translation "实施" within a `` tag and specifies the language as simplified Chinese using the `lang="zh-CN"` attribute.

在保持HTML结构的情况下,将以下英文文本翻译为简体中文: 首先,让我们来看看OpenAIHelper类的实现。

// file: OpenAIHelper.ts
import OpenAI, { ClientOptions } from "openai";

/**
* Parameters for the OpenAIHelper class.
*/
export type OpenAIHelperParams = {
prompt: string;
maxTokens?: number;
images?: string[];
model?: string;
};

/**
* Helper class for interacting with the OpenAI API.
*/
export class OpenAIHelper {
private openai: OpenAI;

/**
* Constructs a new instance of the OpenAIHelper class.
* @param options The client options for the OpenAI API.
* @throws Error if apiKey is not provided in the options.
*/
constructor(options: ClientOptions) {
if (!options.apiKey) {
throw new Error("apiKey is required");
}
this.openai = new OpenAI(options);
}

/**
* Sends a chat-based prompt to the OpenAI API and retrieves the response.
* @param params The parameters for the chat prompt.
* @returns The response from the OpenAI API.
*/
public askChatGPT = async ({
prompt,
maxTokens,
images,
model = "gpt-4o",
}: OpenAIHelperParams) => {
const response = await this.openai.chat.completions.create({
model,
max_tokens: maxTokens,
messages: [
{
role: "user",
content: [
{
type: "text",
text: prompt,
},
...(images?.map((image: string) => ({
type: "image_url" as any,
image_url: {
url: image,
},
})) || []),
],
},
],
response_format: {
type: "json_object",
},
});
const data = JSON.parse(response.choices[0]?.message.content || "{}");
return { ...response, data };
};
}

Sure, the simplified Chinese translation of "How to Use OpenAIHelper" while keeping the HTML structure intact would be: ```html 如何使用OpenAI助手 ```

现在,让我们通过几个示例来演示如何在您的项目中使用 OpenAIHelper 类。

To translate "Identifying a Dog in an Image" into simplified Chinese within an HTML structure, you can use the following: ```html

Example 1: 识别图像中的狗

``` This HTML snippet maintains the structure while displaying the translated text "识别图像中的狗" (Identifying a Dog in an Image) in simplified Chinese.

在这个例子中,我们将要求模型识别一个给定的图像是否是一只狗。

import { OpenAIHelper } from "./OpenAIHelper";
const askChatGpt = async (prompt: string) => {
const openAIHelper = new OpenAIHelper({
apiKey: process.env.OPENAI_API_KEY as string,
dangerouslyAllowBrowser: true,
});
const { data } = await openAIHelper.askChatGPT({
prompt,
images: [
"https://cdn.pixabay.com/photo/2023/08/18/15/02/dog-8198719_640.jpg", // dog image
],
});
console.log("data", data);
};

askChatGpt(
`tell me with a json if this image represents dog, json format: { "isDog": boolean }`
);
// output: { isDog: true }

Certainly! Here's how you can structure your HTML to include the translated text in simplified Chinese: ```html Example 2: Generating Translations

Example 2: Generating Translations

在HTML结构中保留,将以下英文文本翻译为简体中文:

``` In this HTML structure: - `` specifies the title of the document. - `<h1>` is used for the main heading. - `</h1> <p>` contains the translated text.</p> <p>在这个例子中,我们将要求模型为给定的文本值生成翻译。</p> <pre><span id="d34f" class="pb mu gt os b bf pc pd l pe pf">// file: generateTranslations.ts<br>import { OpenAIHelper } from "./OpenAIHelper";<br><br>const askChatGpt = async (prompt: string) => {<br> const openAIHelper = new OpenAIHelper({<br> apiKey: process.env.OPENAI_API_KEY as string,<br> dangerouslyAllowBrowser: true,<br> });<br> const { data } = await openAIHelper.askChatGPT({<br> prompt,<br> });<br> console.log("data", data);<br>};<br><br>askChatGpt(<br> `generate a json with values translated in italian { "Welcome": "Welcome", "Bye": "Bye" }`<br>);<br>// output: { "Welcome": "Benvenuto", "Bye": "Arrivederci" }</span></pre> <p>在这个脚本中,我们使用askChatGPT方法,并使用提示来将文本值翻译成意大利语。模型返回一个包含翻译数值的JSON对象。</p> <h1>In simplified Chinese, "Conclusion" translates to "结论".</h1> <p>```html </p> <p>OpenAIHelper 类简化了与 OpenAI API 交互的过程,让您可以专注于构建应用程序的逻辑。通过处理 API 请求和响应的复杂性,这个辅助类使得将 OpenAI 强大的模型集成到您的项目中变得更加简单。使用提供的示例作为探索 OpenAIHelper 功能的起点,通过 AI 驱动的特性增强您的应用程序。</p> ```

2024-07-11 04:57:02 AI中文站翻译自原文