Python编写接口调用ollama本地部署的大模型,并返回结果
Mac系统,已经通过ollama在本地部署了几个大模型:
通过ollama run qwen:0.5b在后台启动qwen0.5b的模型,然后编写python接口代码,命名为port.py,在里面指定本机接口地址,模型名,输入问题等:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 21 15:30:00 2024
@author: xiaobai
"""
import requests
def llmchat(question):
host="http://localhost"
port="11434"
model = "qwen:0.5b"
url = f"{host}:{port}/api/chat"
headers = {"Content-Type": "application/json"}
data = {
"model": model,
"options": {
"temperature": 0.
},
"stream": False,
"messages": [{
"role": "system",
"content":question
}]
}
response=requests.post(url,json=data,headers=headers,timeout=60)
answer=response.json().get("message").get('content')
print(answer)
if __name__=="__main__":
question = "你是谁?" #在这里输入问题
print('\n')
llmchat(question)
运行port.py程序,大模型返回结果:
作者:福小白