本地环境:ollama3.2(理论上ollama3.1以上就支持tool功能,但是笔者没有试过)
windows11
要运行这个代码首先需要安装对应的ollama python库,安装命令如下:
pip install ollama
代码如下:
import json
import ollama
import asyncio
# 模拟控制灯的API
def control_light(action: str) -> str:
if action.lower() not in ["on", "off"]:
return json.dumps({'error': 'Invalid action. Use "on" or "off".'})
light_status = f"The light is now {'on' if action.lower() == 'on' else 'off'}."
return json.dumps({'status': light_status})
async def run(model: str):
client = ollama.AsyncClient()
# 设置系统角色描述