Spaces:
Running
Running
File size: 1,377 Bytes
e397647 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import aiohttp
import asyncio
import json
async def embeddings_run_async(input, url="https://sanbo1200-jina-embeddings-v3.hf.space/api/v1/embeddings", model="jinaai/jina-embeddings-v3"):
headers = {
'Content-Type': 'application/json'
}
data = {
"input": input,
"model": model
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
if response.status == 200:
return await response.json()
else:
response.raise_for_status()
# 示例如何使用这个异步函数
async def main():
input_text = "Your text string goes here"
result = await embeddings_run_async(input_text)
print(f"---{result}")
# 运行异步函数
if __name__ == "__main__":
asyncio.run(main())
# 如果需要批量处理多个请求,可以这样使用:
async def batch_process():
inputs = [
"First text to process",
"Second text to process",
"Third text to process"
]
# 并发执行多个请求
tasks = [embeddings_run_async(text) for text in inputs]
results = await asyncio.gather(*tasks)
for i, result in enumerate(results):
print(f"Result {i+1}: {result}")
# 运行批量处理
if __name__ == "__main__":
asyncio.run(batch_process()) |