npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@re-ai/openai-like-api

v0.3.5

Published

reai openai like api

Downloads

626

Readme

OpenAILike API 开发文档

1. 概述

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

1.1 更新日志

  • 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)
    // })
})