import requests
import json
url = "http://localhost:11434/api/chat"
payload = {
"model": "qwen",
"messages": [
{"role": "user", "content": "why is the sky blue?"}
]
}
headers = {
"Content-Type": "application/json"
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
results = response.content.decode('utf-8').split('\n')
content_parts = []
for result in results:
if result == '':
continue
json_obj = json.loads(result)
msg = json_obj["message"]["content"]
content_parts.append(msg)
full_content = "".join(content_parts)
print(full_content)
ollama run qwen
存这一行为ollama.bat,通过nssm安装这个bat文件为后台服务,使ollama常驻后台。