如何在Colab部署大模型

简介

介绍如何通过 Ollama 在 Colab 上部署大模型

  1. 注册 Colab
  2. 注册 ngrok

步骤

  1. 新建 notebook

    File->new notebook in Drive

  2. 修改运行时类型

    Connect-> Change runtime type

    • 运行时类型选择 Python3

    • 硬件加速器选择 T4GPU

    连接->连接到托管的运行时:T4

  3. 在新的 notebook 中添加两段 Code,点击最侧的执行按钮

    第一段

    # 安装Ollama
    !curl https://ollama.ai/install.sh | sh
    

    第二段

    # 安装python库和导入库
    !pip install aiohttp pyngrok
    
    import os
    import asyncio
    
    # 设置 GPU 库,让Ollma能够使用GPU进行推理
    os.environ.update({'LD_LIBRARY_PATH':'/usr/lib64-nvidia'})
    
    async def run_process(cmd):
      print('>>> starting', *cmd)
      p = await asyncio.subprocess.create_subprocess_exec(
          *cmd,
          stdout=asyncio.subprocess.PIPE,
          stderr=asyncio.subprocess.PIPE,
      )
    
      async def pipe(lines):
        async for line in lines:
          print(line.strip().decode('utf-8'))
    
      await asyncio.gather(
          pipe(p.stdout),
          pipe(p.stderr),
      )
    
    #注册一个 ngrok 账户,获取Authtoken,替换下面的 ngrok-authtoken
    await asyncio.gather(
        run_process(['ngrok', 'config', 'add-authtoken','<ngrok-authtoken>'])
    )
    
    await asyncio.gather(
        run_process(['ollama', 'serve']),
        run_process(['ngrok', 'http', '11434', '--host-header', 'localhost:11434'])
    )
    
  4. 看到 starting ngrok http 11434 --host-header localhost:11434 等日志,表示部署成功

  5. ngrok 官网 endpoints 查看公网URL,点击 URL->view site,显示 Ollama is running,表示代理成功

  6. 在本地修改 OLLAMA_HOST 环境变量为刚才的公网URL:https://4xxb-3x-1xx-1x1-1x4.ngrok-free.app

    此时在本地客户端输入 ollama 的命令会在 colab 上执行

    # 下载并启动大模型
    ollama run qwen2:1.5b
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值