MRobot/ai.py
2025-07-25 16:39:32 +08:00

21 lines
627 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
url = "http://154.37.215.220:11434/api/generate"
payload = {
"model": "qwen3:0.6b",
"prompt": "你好,介绍一下你自己"
}
response = requests.post(url, json=payload, stream=True)
for line in response.iter_lines():
if line:
try:
data = json.loads(line.decode('utf-8'))
# 只输出 response 字段内容
print(data.get("response", ""), end="", flush=True)
# 如果 done 为 True则换行
if data.get("done", False):
print()
except Exception as e:
pass # 忽略解析异常