jina-embeddings-v3 / demo /urllib_demo.py
sanbo
update sth. at 2025-01-16 23:44:43
e397647
raw
history blame
882 Bytes
from urllib.request import Request, urlopen
import json
def embeddings_run(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
}
# 创建请求
request = Request(
url,
headers=headers,
data=json.dumps(data).encode('utf-8'),
method='POST'
)
# 发送请求并获取响应
try:
with urlopen(request) as response:
if response.status == 200:
return json.loads(response.read().decode('utf-8'))
except Exception as e:
raise Exception(f"Request failed: {str(e)}")
if __name__ == "__main__":
input_text = "Your text string goes here"
print(f"---{embeddings_run(input_text)}")