from openai import OpenAI
# 配置(支持所有 OpenAI 格式的平台)
client = OpenAI(
api_key="sk-***************************************", # 你的密钥
base_url="https://ai.hcrzx.com/v1/" # 中转平台地址
)
def get_embedding(text: str) -> list[float]:
"""
获取文本的向量(Embedding)
返回:浮点数数组(向量)
"""
# 调用 embedding 接口
response = client.embeddings.create(
model="jina-embeddings-v4", # 通用向量模型
input=text,
encoding_format="float"
)
# 返回向量
return response.data[0].embedding
# ==================== 测试 ====================
if __name__ == "__main__":
content = "我想获取这句话的向量"
vector = get_embedding(content)
print("向量长度:", len(vector))
print("向量前10个值:", vector[:10])向量长度: 2048
向量前10个值: [0.00878906, -0.0390625, -0.00144958, -0.00436401, 0.00126648, -0.01403809, 0.00448608, 0.00256348, -0.01696777, 0.06103516]