Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@re-ai/openai-like-api

Package Overview
Dependencies
Maintainers
0
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@re-ai/openai-like-api

reai openai like api

  • 0.1.7
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

OpenAILike API 开发文档

1. 概述

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

1.1 更新日志

  • 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 参数
参数名类型必填说明
modelstring使用的模型名称,例如 "gpt-3.5-turbo"
promptstring输入的提示文本
max_tokensnumber生成的最大 token 数,默认为 2048
temperaturenumber生成结果的温度,默认为 0.7
top_pnumber生成结果的采样概率,默认为 1
frequency_penaltynumber生成结果中重复 token 的惩罚系数,默认为 0
presence_penaltynumber生成结果中未出现 token 的惩罚系数,默认为 0
streamboolean是否返回聊天完成的流,默认为 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 参数
参数名类型必填说明
inputstring[]string
modelstring使用的模型名称,例如 "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)
    }
})

FAQs

Package last updated on 24 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc