当前位置:   article > 正文

python调用ollamaAPI 模型名称【llama2-chinese:latest】_ollama python调用

ollama python调用

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)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/632730
推荐阅读
相关标签
  

闽ICP备14008679号