Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@re-ai/openai-like-api
Advanced tools
OpenAILike 是一个模拟 OpenAI API 的接口,提供了聊天完成(Chat Completion)、嵌入创建(Embedding Creation)和图像生成(Image Generation)的功能。本开发文档将详细介绍如何使用这个接口。
npm i @re-ai/openai-like-api
使用 chatComplete
方法创建聊天会话。该方法接收一个 ChatCompletionCreateParams
类型的参数,并返回一个 Promise 对象。该 Promise 对象解析为 ChatCompletionStream<ChatCompletionChunk>
或 ChatCompletion
类型,表示聊天完成的流或单个聊天完成结果。
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
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 |
stream
参数为 true
,则返回一个 ChatCompletionStream<ChatCompletionChunk>
类型。该类型是一个可读的流,每次调用 next()
方法会返回下一个聊天完成块(ChatCompletionChunk
)。stream
参数为 false
,则返回一个 ChatCompletion
类型。该类型表示单个聊天完成结果,包含了生成的文本和其他相关信息。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)
}
})
使用 embeddings
方法创建嵌入。该方法接收一个 EmbeddingCreateParams
类型的参数,并返回一个 Promise 对象。该 Promise 对象解析为 CreateEmbeddingResponse
类型,表示嵌入创建的响应结果。
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
input | string[] | string | 是 |
model | string | 是 | 使用的模型名称,例如 "text-embedding-ada-001" |
返回一个 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)
})
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)
// })
})
FAQs
reai openai like api
The npm package @re-ai/openai-like-api receives a total of 24 weekly downloads. As such, @re-ai/openai-like-api popularity was classified as not popular.
We found that @re-ai/openai-like-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.