gpt4 book ai didi

python - 如何使用 OpenAI 的 API 使用批量嵌入?

转载 作者:行者123 更新时间:2023-12-02 22:45:01 51 4
gpt4 key购买 nike

我正在使用 OpenAI API 获取一堆句子的嵌入。一堆句子,我的意思是一堆句子,比如几千个。有没有办法让它更快或让它同时进行嵌入或其他什么?

我尝试循环遍历并为每个句子发送一个请求,但这非常慢,但发送句子列表也是如此。对于这两种情况,我都使用了这段代码:'''

response = requests.post(
"https://api.openai.com/v1/embeddings",
json={
"model": "text-embedding-ada-002",
"input": ["text:This is a test", "text:This is another test", "text:This is a third test", "text:This is a fourth test", "text:This is a fifth test", "text:This is a sixth test", "text:This is a seventh test", "text:This is a eighth test", "text:This is a ninth test", "text:This is a tenth test", "text:This is a eleventh test", "text:This is a twelfth test", "text:This is a thirteenth test", "text:This is a fourteenth test", "text:This is a fifteenth test", "text:This is a sixteenth test", "text:This is a seventeenth test", "text:This is a eighteenth test", "text:This is a nineteenth test", "text:This is a twentieth test", "text:This is a twenty first test", "text:This is a twenty second test", "text:This is a twenty third test", "text:This is a twenty fourth test", "text:This is a twenty fifth test", "text:This is a twenty sixth test", "text:This is a twenty seventh test", "text:This is a twenty eighth test", "text:This is a twenty ninth test", "text:This is a thirtieth test", "text:This is a thirty first test", "text:This is a thirty second test", "text:This is a thirty third test", "text:This is a thirty fourth test", "text:This is a thirty fifth test", "text:This is a thirty sixth test", "text:This is a thirty seventh test", "text:This is a thirty eighth test", "text:This is a thirty ninth test", "text:This is a fourtieth test", "text:This is a forty first test", "text:This is a forty second test", "text:This is a forty third test", "text:This is a forty fourth test", "text:This is a forty fifth test", "text:This is a forty sixth test", "text:This is a forty seventh test", "text:This is a forty eighth test", "text:This is a forty ninth test", "text:This is a fiftieth test", "text:This is a fifty first test", "text:This is a fifty second test", "text:This is a fifty third test"],
},
headers={
"Authorization": f"Bearer {key}"
}
)

对于第一个测试,我一个一个地做了一堆这样的请求,第二个我发送了一个列表。我应该并行发送单独的请求吗?那会有帮助吗?谢谢!

最佳答案

根据 OpenAi 的 Create Embeddings API,你应该能够做到这一点:

To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays. Each input must not exceed 8192 tokens in length.

https://beta.openai.com/docs/api-reference/embeddings/create

关于python - 如何使用 OpenAI 的 API 使用批量嵌入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74907244/

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