jina-embeddings-v3 / demo /aiohttp_demo.py
sanbo
update sth. at 2025-01-16 23:44:43
e397647
raw
history blame
1.38 kB
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())