赞
踩
import requests
import json
def send_message_to_ollama_chat(message, port=11434):
url = f"http://localhost:{port}/api/chat"
payload = {
“model”: “llama2-chinese:latest
”,
“messages”: [{“role”: “user”, “content”: message}]
}
response = requests.post(url, json=payload)
if response.status_code == 200:
response_content = “”
for line in response.iter_lines():
if line:
response_content += json.loads(line)[“message”][“content”]
return response_content
else:
return f"Error: {response.status_code} - {response.text}"
def send_message_to_ollama(message, port=11434):
url = f"http://localhost:{port}/api/generate"
payload = {
“model”: “llama2-chinese:latest”,
“prompt”: message
}
response = requests.post(url, json=payload)
if response.status_code == 200:
response_content = “”
for line in response.iter_lines():
if line:
response_content += json.loads(line)[“response”]
return response_content
else:
return f"Error: {response.status_code} - {response.text}"
if name == “main”:
user_input = “why is the sky blue?”
response = send_message_to_ollama(user_input)
print(“Ollama’s response:”)
print(response)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。