gpt4 book ai didi

typescript - OpenAI 速率限制 429 Bug

转载 作者:行者123 更新时间:2023-12-02 22:48:15 24 4
gpt4 key购买 nike

我正在尝试使用this使用 OpenAI + Pinecone 为 YouTube 视频创建语义搜索的存储库,但我在此步骤中遇到 429 错误 - “运行命令 npx tsx src/bin/process-yt-playlist.ts 来预处理转录本并从中获取嵌入OpenAI,然后将它们插入到 Pinecone 搜索索引中。”

感谢任何帮助!!

附件是我的 openai.ts 文件

import pMap from 'p-map'
import unescape from 'unescape'

import * as config from '@/lib/config'

import * as types from './types'

import pMemoize from 'p-memoize'
import pRetry from 'p-retry'
import pThrottle from 'p-throttle'

// TODO: enforce max OPENAI_EMBEDDING_CTX_LENGTH of 8191

// https://platform.openai.com/docs/guides/rate-limits/what-are-the-rate-limits-for-our-api
// TODO: enforce TPM
const throttleRPM = pThrottle({
// 3k per minute instead of 3.5k per minute to add padding
limit: 3000,
interval: 60 * 1000,
strict: true
})

type PineconeCaptionVectorPending = {
id: string
input: string
metadata: types.PineconeCaptionMetadata
}

export async function getEmbeddingsForVideoTranscript({
transcript,
title,
openai,
model = config.openaiEmbeddingModel,
maxInputTokens = 100, // TODO???
concurrency = 1
}: {
transcript: types.Transcript
title: string
openai: types.OpenAIApi
model?: string
maxInputTokens?: number
concurrency?: number
}) {
const { videoId } = transcript

let pendingVectors: PineconeCaptionVectorPending[] = []
let currentStart = ''
let currentNumTokensEstimate = 0
let currentInput = ''
let currentPartIndex = 0
let currentVectorIndex = 0
let isDone = false

// const createEmbedding = pMemoize(throttleRPM(createEmbeddingImpl))

// Pre-compute the embedding inputs, making sure none of them are too long
do {
isDone = currentPartIndex >= transcript.parts.length

const part = transcript.parts[currentPartIndex]
const text = unescape(part?.text)
.replaceAll('[Music]', '')
.replaceAll(/[\t\n]/g, ' ')
.replaceAll(' ', ' ')
.trim()
const numTokens = getNumTokensEstimate(text)

if (!isDone && currentNumTokensEstimate + numTokens < maxInputTokens) {
if (!currentStart) {
currentStart = part.start
}

currentNumTokensEstimate += numTokens
currentInput = `${currentInput} ${text}`

++currentPartIndex
} else {
currentInput = currentInput.trim()
if (isDone && !currentInput) {
break
}

const currentVector: PineconeCaptionVectorPending = {
id: `${videoId}:${currentVectorIndex++}`,
input: currentInput,
metadata: {
title,
videoId,
text: currentInput,
start: currentStart
}
}

pendingVectors.push(currentVector)

// reset current batch
currentNumTokensEstimate = 0
currentStart = ''
currentInput = ''
}
} while (!isDone)
let index = 0;

console.log("Entering embeddings calculation")
// Evaluate all embeddings with a max concurrency
// const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const vectors: types.PineconeCaptionVector[] = await pMap(
pendingVectors,
async (pendingVector) => {
// await delay(6000); // add a delay of 1 second before each iteration
console.log(pendingVector.input + " " + model)


// const { data: embed } = await openai.createEmbedding({
// input: pendingVector.input,
// model
// })

async function createEmbeddingImpl({
input = pendingVector.input,
model = 'text-embedding-ada-002'
}: {
input: string
model?: string
}): Promise<number[]> {
const res = await pRetry(
() =>
openai.createEmbedding({
input,
model
}),
{
retries: 4,
minTimeout: 1000,
factor: 2.5
}
)

return res.data.data[0].embedding
}

const embedding = await pMemoize(throttleRPM(createEmbeddingImpl));


const vector: types.PineconeCaptionVector = {
id: pendingVector.id,
metadata: pendingVector.metadata,
values: await embedding(pendingVector)
}
console.log(index + " THIS IS THE NUMBER OF CALLS TO OPENAI Embedding: " + embedding)
index++;
return vector
},
{
concurrency
}
)

return vectors
}

function getNumTokensEstimate(input: string): number {
const numTokens = (input || '')
.split(/\s/)
.map((token) => token.trim())
.filter(Boolean).length

return numTokens
}

我尝试将 api 调用之间的时间间隔增加到远低于限制,但不知何故我仍然遇到相同的错误。

最佳答案

如果您没有任何积分,OpenAI 会发送 429 Rate Limit 错误。我一直在使用 3 个月后过期的免费积分。您可以在使用页面上查看您的可用积分:

https://platform.openai.com/account/usage

旁注:一旦我将信用卡存档,大约需要 5 分钟时间限制才会消失

关于typescript - OpenAI 速率限制 429 Bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75763453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com