@re-ai/openai-like-api
TypeScript icon, indicating that this package has built-in type declarations

0.4.5 • Public • Published

OpenAILike API 开发文档

1. 概述

OpenAILike 是一个模拟 OpenAI API 的接口,提供了聊天完成(Chat Completion)、嵌入创建(Embedding Creation)和图像生成(Image Generation)的功能。本开发文档将详细介绍如何使用这个接口。

1.1 更新日志

  • 2025.02.17
    • 添加腾讯云大模型知识引擎的deepseek接口
  • 2025.02.14
    • 添加非多模态参数支持:params参数添加isNotMultiMode
  • 2025.02.13
    • 处理deepseek不支持多模态messages的问题
  • 2025.01.06
    • 添加了gemini api接口
  • 2024.12.18
    • 添加豆包文生图接口
  • 2024.12.11
    • 添加glm4v-flash模型
  • 2024.11.20
    • 添加x.ai接口,openai的npm包更新
  • 2024.09.19
    • 更新0.3.3版本,添加了通义万象的生图接口
  • 2024.09.12
    • 更新0.3.0版本,升级openai的npm包,支持response_format.json_scheme
    • 更新azure的npm包,使用openai的AzureOpenAI类,key的格式改成"azure:【apiKey】:【deployment】:【endpoint】"
    • 更新其他模型的models
  • 2024.08.06
    • 添加ZhipuAI视频生成接口
  • 2024.08.05
    • chat接口适配DeepSeek
    • 文生图适配 腾讯混元, 百度云
  • 2024.08.04
    • 添加视觉接口 VisionInterface, 生成图片,文档。目前适配了 openai, Flux.1(fal.ai接口),glm,
  • 2024.07.26:
    • 适配azure,ollama的 tools 和 image(图像识别)
    • 测试 functioCall(tools),基本都支持了,有识别图像能力的大模型,可以传入图片,测试ok
    • 添加了hunyuan的生图方法
    • 添加了一个单独方法,处理流里面的toolCall数据
    • 不推荐使用miniMax,接口很烂,不适配functionCall了
  • 2024.07.25: 测试OpenAI, AzureOpenAI 接口,句子切断单独作为一个方法
  • 2024.07.18: 添加商汤日日新
  • 2024.07.17: 添加阿里dashscope,百川豆包moonshot,零一万物YI,AzureOpenAI
  • 2024.07.16: 添加讯飞星火,腾讯混元,智谱清言

1.2 安装

npm i @re-ai/openai-like-api

2. 聊天完成(Chat Completion)

2.1 创建聊天会话

使用 chatComplete 方法创建聊天会话。该方法接收一个 ChatCompletionCreateParams 类型的参数,并返回一个 Promise 对象。该 Promise 对象解析为 ChatCompletionStream<ChatCompletionChunk>ChatCompletion 类型,表示聊天完成的流或单个聊天完成结果。

2.1.1 ChatCompletionCreateParams 参数

参数名 类型 必填 说明
model string 使用的模型名称,例如 "gpt-3.5-turbo"
prompt string 输入的提示文本
max_tokens number 生成的最大 token 数,默认为 2048
temperature number 生成结果的温度,默认为 0.7
top_p number 生成结果的采样概率,默认为 1
frequency_penalty number 生成结果中重复 token 的惩罚系数,默认为 0
presence_penalty number 生成结果中未出现 token 的惩罚系数,默认为 0
response_format object 生成结果的格式,默认为 { "type": "text" } , type 取值 text 或 json_object 或 json_schema,gpt-4o/gpt-4o-mini 支持 json_schema
stream boolean 是否返回聊天完成的流,默认为 false

2.1.2 返回值

  • 如果 stream 参数为 true,则返回一个 ChatCompletionStream<ChatCompletionChunk> 类型。该类型是一个可读的流,每次调用 next() 方法会返回下一个聊天完成块(ChatCompletionChunk)。
  • 如果 stream 参数为 false,则返回一个 ChatCompletion 类型。该类型表示单个聊天完成结果,包含了生成的文本和其他相关信息。

2.2 使用示例

import { ChatCompletionStream, ReAIOpenAILikeAPI, ChatCompletionChunk } from "@re-ai/openai-like-api";

// 本地ollama为例
const api = ReAIOpenAILikeAPI({
    host: "http://localhost:11434",
    apiKey: "ollama"
})

api.chatComplete({
    "model": "qwen2:1.5b",
    "messages": [
        {
            "role": "user",
            "content": "你好吗"
        }
    ],
    "stream": true
}).then(async (stream) => {
    const response = stream as ChatCompletionStream<ChatCompletionChunk>;
    for await (const iterator of response) {
        console.log(iterator)
    }
})

3. 嵌入创建(Embedding Creation)

使用 embeddings 方法创建嵌入。该方法接收一个 EmbeddingCreateParams 类型的参数,并返回一个 Promise 对象。该 Promise 对象解析为 CreateEmbeddingResponse 类型,表示嵌入创建的响应结果。

3.1 EmbeddingCreateParams 参数

参数名 类型 必填 说明
input string[] string
model string 使用的模型名称,例如 "text-embedding-ada-001"

3.2 返回值

返回一个 CreateEmbeddingResponse 类型,包含了创建的嵌入数据和相关信息。

3.3 使用示例

import { ReAIOpenAILikeAPI } from "@re-ai/openai-like-api";

const api = ReAIOpenAILikeAPI({
    host: "http://localhost:11434",
    apiKey: "ollama"
})

api.embeddings({
    "model": "milkey/gte:large-zh-f16",
    "input": "你好吗"
}).then(res => {
    console.log(res)
})

4. 不同模型支持

文心一言

import { ChatCompletionStream, ReAIOpenAILikeAPI, ChatCompletionChunk } from "@re-ai/openai-like-api";
import { ErnieModels } from "../src/types/ernie";

const api = ReAIOpenAILikeAPI({
    apiKey: "erine:client_id:client_secret"
})

api.chatComplete({
    "model": ErnieModels.SPEED_128k,
    "messages": [
        {
            "role": "user",
            "content": "你好吗"
        }
    ],
    "stream": true
}).then(async (stream) => {
    const response = stream as ChatCompletionStream<ChatCompletionChunk>;
    for await (const iterator of response) {
        console.log(iterator)
    }

    // 处理句子可以参照下面
    // StreamUtils.cut(response, (chuck) => {
    //     console.log(chuck)
    // })
})

Readme

Keywords

none

Package Sidebar

Install

npm i @re-ai/openai-like-api

Weekly Downloads

13

Version

0.4.5

License

ISC

Unpacked Size

323 kB

Total Files

175

Last publish

Collaborators

  • niklauslu