dart-openai-sdk
Version:
OpenAI api-reference sdk
159 lines (115 loc) • 5 kB
Markdown
<div align="center">
<br>
<p>OpenAI API SDK库</p>
<a href="https://packagephobia.now.sh/result?p=dart-openai-sdk"><img src="https://badgen.net/packagephobia/install/dart-openai-sdk" alt="Current version"></a>
<a href="https://www.npmjs.com/package/dart-openai-sdk"><img src="https://img.shields.io/npm/v/dart-openai-sdk" alt="Install size"></a><br/>
<a href="https://www.npmjs.com/package/dart-openai-sdk"><img src="https://nodei.co/npm/dart-openai-sdk.png" alt="Base Info"></a>
<br>
</div>
<!-- TOC -->
- [安装 (Introduction)](
- [加载此模块 (Loading the module)](
- [常用APIs (Common Usage)](
- [列出所有模型 (models)](
- [查询模型信息 (retrieveModel)](
- [聊天 (chat)](
- [流式聊天 (chatByStream)](
- [生成图片 (image)](
<!-- /TOC -->
安装 (Introduction)
```sh
npm install dart-openai-sdk
```
Tips:
* 注意此项目只用于Node后端使用
* 使用可以参考test.js中的代码进行使用
加载此模块 (Loading the module)
```js
// CommonJS
const OpenAI = require('dart-openai-sdk');
const AI = new OpenAI('Your API KEY','Your API Organization');
// ES Module
import OpenAI from 'dart-openai-sdk';
const AI = new OpenAI('Your API KEY','Your API Organization');
```
new OpenAI() 参数说明:
* @param {string} key OpenAI API KEY (Find Key: https://platform.openai.com/account/api-keys)
* @param {string} organization OpenAI API Organization (Find Key: https://platform.openai.com/account/api-keys)
* @param {string} api_url https://api.openai.com/v1
* @param {number} max_cache_num 对话缓存的最大条数
* @param {boolean} debug 是否调试模式,会打印出一些日志信息
常用APIs (Common Usage)
```sh
列出并描述API中可用的各种模型。你可以参考模型文档来了解有哪些模型可用以及它们之间的区别。
List and describe the various models available in the API. You can refer to the Models documentation to understand what models are available and the differences between them.
```
```js
import OpenAI from 'dart-openai-sdk';
const AI = new OpenAI('Your API KEY');
let response = await AI.models();
console.log(JSON.stringify(response, null, 2));
```
```sh
检索一个模型实例,提供关于模型的基本信息,如所有者和许可。
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
```
```js
import OpenAI from 'dart-openai-sdk';
const AI = new OpenAI('Your API KEY');
let response = await AI.retrieveModel('Model ID');
console.log(JSON.stringify(response, null, 2));
```
参数说明:
* @param {string} model 这个请求要使用的模型的ID。可以通过models查询所有的模型列表。
```sh
创建一个聊天会话
Creates a completion for the chat message
```
```js
import OpenAI from 'dart-openai-sdk';
const AI = new OpenAI('Your API KEY');
let response = await AI.chat('Your Message','Your User ID','Your Model ID');
console.log(JSON.stringify(response, null, 2));
```
参数说明:
* @param {string} message 对话消息,你输入的消息
* @param {string} user 用户ID(同一个用户的对话信息使用同一个UserID)
* @param {string} model (非必要) 要使用的模型ID (默认使用: gpt-3.5-turbo)
```sh
创建一个聊天会话,通过流式获得返回数据
Creates a completion for the chat message
```
```js
import OpenAI from 'dart-openai-sdk';
const AI = new OpenAI('Your API KEY');
let response = await AI.chatByStream('Your Message','Your User ID','Your Model ID');
console.log(JSON.stringify(response, null, 2));
```
参数说明:
* @param {string} message 对话消息,你输入的消息
* @param {string} user 用户ID(同一个用户的对话信息使用同一个UserID)
* @param {string} model (非必要) 要使用的模型ID (默认使用: gpt-3.5-turbo)
```sh
根据提示词生成图片
Creates an image given a prompt.
```
```js
import OpenAI from 'dart-openai-sdk';
const AI = new OpenAI('Your API KEY');
let response = await AI.image('Female wearing cybernetic exoskeleton character design, concept design sheet, white background, style of yoji shinkawa', '1', 1, '512x512', 'url');
console.log(JSON.stringify(response, null, 2));
```
参数说明:
* @param {string} prompt 对所需图像的文字描述。最大长度为1000个字符。
* @param {string} user 用户ID
* @param {number} number [1 ~ 10] 要生成的图像的数量。必须在1到10之间。
* @param {string} size 生成图像的大小。必须是其中之一 ['256x256','512x512','1024x1024']
* @param {string} response_format 返回生成的图像的格式。必须是以下之一 ['url','b64_json']