41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import requests
|
||
import json
|
||
|
||
# API 地址
|
||
API_URL = "http://0.0.0.0:5163/embedding/"
|
||
|
||
# 要发送的数据(JSON 格式)
|
||
data = {
|
||
"sentences": "土豆" # 单句示例,可改为 ["土豆", "西红柿"] 处理多句
|
||
}
|
||
|
||
# 发送 POST 请求
|
||
try:
|
||
# 设置请求头,明确指定内容类型为 JSON
|
||
headers = {"Content-Type": "application/json"}
|
||
|
||
# 使用 POST 方法发送请求,data 需要序列化为 JSON 字符串
|
||
response = requests.post(
|
||
url=API_URL,
|
||
data=json.dumps(data), # 将字典转换为 JSON 字符串
|
||
headers=headers
|
||
)
|
||
|
||
# 检查响应状态码
|
||
if response.status_code == 200:
|
||
# 解析返回的 JSON 数据
|
||
result = response.json()
|
||
print("API Response:", result)
|
||
|
||
# 根据返回的 code 判断结果
|
||
if result.get("code") == 200:
|
||
embeddings = result.get("data")
|
||
print("Embeddings:", embeddings)
|
||
else:
|
||
print("Error from API:", result.get("msg"))
|
||
else:
|
||
print(f"Request failed with status code: {response.status_code}")
|
||
print("Response text:", response.text)
|
||
|
||
except requests.exceptions.RequestException as e:
|
||
print(f"Error connecting to API: {e}") |