pdf_code/zzb_data/test1029.py

34 lines
978 B
Python

import time
from http import HTTPStatus
import dashscope
#dashscope.api_key='sk-63c02fbb9b7d4b0494a3200bec1ae286'
dashscope.api_key='sk-d50028f8067a42b5b5b9e157276a1817'
def embed_with_str(input):
retry = 0
max_retry = 5
t = 0.1
while retry < max_retry:
#阿里接口限流
time.sleep(t)
resp = dashscope.TextEmbedding.call(
model=dashscope.TextEmbedding.Models.text_embedding_v2,
input=input)
if resp.status_code == HTTPStatus.OK:
return resp
elif resp.status_code == 429:
print(f'触发限流,等待{t}秒后重试')
retry += 1
t+=0.1
else:
print(f'请求失败,状态码:{resp.status_code}')
return None
print('重试超过上限')
#print(output["embeddings"][0]["embedding"])
return None
vector_obj = embed_with_str('测试')
vector = vector_obj.output["embeddings"][0]["embedding"]
print(vector)