当前位置:   article > 正文

解决ollama无法长时间保存在内存中的问题_ollama过一段时间就释放模型

ollama过一段时间就释放模型

一、每次发出请求加载模型时,定义一个keep_alive变量,说明要存在多长时间。

  1. curl http://localhost:11434/api/generate -d '{
  2. "model": "llama2",
  3. "prompt": "Why is the sky blue?",
  4. "stream": false,
  5. "keep_alive": "24h"
  6. }'

二、又或者可以每280秒加载一次模型,因为模型每五分钟自动删除,由于加载模型只需1ms,所以可以选择这种方案:
 

  1. import requests
  2. import time
  3. from datetime import datetime
  4. import pytz
  5. def get_bj_time():
  6. beijing_tz = pytz.timezone('Asia/Shanghai')
  7. return datetime.now(beijing_tz).strftime("%Y-%m-%d %H:%M:%S")
  8. while True:
  9. data = {"model": "qwen:7b", "keep_alive": "5m"}
  10. headers = {'Content-Type': 'application/json'}
  11. high_precision_time = time.perf_counter()
  12. response = requests.post('http://localhost:11434/api/generate', json=data, headers=headers)
  13. high_precision_time_end = time.perf_counter()
  14. time1 = high_precision_time_end-high_precision_time
  15. print(f"高精度时间(精确到微秒): {time1*1000:.6f}")
  16. jsonResponse = response.content.decode('utf-8') # 将 bytes 转换为字符串以便打印
  17. print(jsonResponse)
  18. print(f"当前北京时间:{get_bj_time()}")
  19. time.sleep(280) # 暂停280秒后再次执行
  20. '''
  21. 7b初次加载模型时间:3.867187177s, 第二次加载模型时间:0.766666ms
  22. 14b初次加载模型时间:5.180146173s , 第二次加载模型时间:0.753414ms
  23. 72b初次加载模型时间:16.991763358s,第二次加载模型时间:1.358505ms

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号