Ollama Jupyter 笔记本¶
此示例在如 Google Colab 的 Jupyter 实例中下载并安装 Ollama。它将启动 Ollama 服务,并使用 ngrok
暴露一个端点,可以用来远程与 Ollama 实例进行通信。
为了获得最佳效果,请使用带有 GPU 加速器的实例。
源码¶
ollama.ipynb¶
"# Download and run the Ollama Linux install script\n",
"!curl -fsSL https://ollama.com/install.sh | sh\n",
"!command -v systemctl >/dev/null && sudo systemctl stop ollama"
!pip install aiohttp pyngrok
import os
import asyncio
from aiohttp import ClientSession
# Set LD_LIBRARY_PATH so the system NVIDIA library becomes preferred
# over the built-in library. This is particularly important for
# Google Colab which installs older drivers
os.environ.update({'LD_LIBRARY_PATH': '/usr/lib64-nvidia'})
async def run(cmd):
'''
run is a helper function to run subcommands asynchronously.
'''
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),
)
await asyncio.gather(
run(['ollama', 'serve']),
run(['ngrok', 'http', '--log', 'stderr', '11434']),
)
The previous cell starts two processes, ollama
and ngrok
. The log output will show a line like the following which describes the external address."
t=2023-11-12T22:55:56+0000 lvl=info msg=\"started tunnel\" obj=tunnels name=command_line addr=http://localhost:11434 url=https://8249-34-125-179-11.ngrok.io\n
The external address in this case is https://8249-34-125-179-11.ngrok.io
which can be passed into OLLAMA_HOST
to access this instance.