File size: 5,621 Bytes
5db25ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46ed764
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/almutareb/InnovationPathfinderAI/blob/agent_notebook/mixtral_ollama_public_api.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hdHmLCO48n-H"
      },
      "outputs": [],
      "source": [
        "# https://github.com/jmorganca/ollama/blob/main/examples/jupyter-notebook/ollama.ipynb"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "o5Q6OmcN0mkS",
        "outputId": "c7a663c4-b1d8-4354-d1f2-fac8f503f4d3"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
            "                                 Dload  Upload   Total   Spent    Left  Speed\n",
            "100  8423    0  8423    0     0   8293      0 --:--:--  0:00:01 --:--:--  8298>>> Downloading ollama...\n",
            "100  8423    0  8423    0     0   7756      0 --:--:--  0:00:01 --:--:--  7763\n",
            "############################################################################################# 100.0%\n",
            ">>> Installing ollama to /usr/local/bin...\n",
            ">>> Creating ollama user...\n",
            ">>> Adding current user to ollama group...\n",
            ">>> Creating ollama systemd service...\n",
            "WARNING: Unable to detect NVIDIA GPU. Install lspci or lshw to automatically detect and install NVIDIA CUDA drivers.\n",
            ">>> The Ollama API is now available at 0.0.0.0:11434.\n",
            ">>> Install complete. Run \"ollama\" from the command line.\n",
            "System has not been booted with systemd as init system (PID 1). Can't operate.\n",
            "Failed to connect to bus: Host is down\n"
          ]
        }
      ],
      "source": [
        "!curl https://ollama.ai/install.sh | sh\n",
        "!command -v systemctl >/dev/null && sudo systemctl stop ollama"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TXtmzrel194r"
      },
      "outputs": [],
      "source": [
        "!pip install aiohttp pyngrok -q"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GLPqSWXT3QBg",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a7542b67-3287-4ce5-8459-2e86d8ec7dfd"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": []
        }
      ],
      "source": [
        "from pyngrok import ngrok\n",
        "from google.colab import userdata\n",
        "\n",
        "# https://dashboard.ngrok.com/get-started/your-authtoken\n",
        "NGROK_TOKEN = userdata.get('NGROK_TOKEN')\n",
        "ngrok.set_auth_token(\"NGROK_TOKEN\")"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "\n",
        "import os\n",
        "import asyncio\n",
        "from aiohttp import ClientSession\n",
        "from google.colab import userdata\n",
        "\n",
        "# Set LD_LIBRARY_PATH so the system NVIDIA library becomes preferred\n",
        "# over the built-in library. This is particularly important for\n",
        "# Google Colab which installs older drivers\n",
        "os.environ.update({'LD_LIBRARY_PATH': '/usr/lib64-nvidia'})\n",
        "NGROK_TOKEN = userdata.get('NGROK_TOKEN')\n",
        "ngrok.set_auth_token(\"NGROK_TOKEN\")\n",
        "\n",
        "async def run(cmd):\n",
        "  '''\n",
        "  run is a helper function to run subcommands asynchronously.\n",
        "  '''\n",
        "  print('>>> starting', *cmd)\n",
        "  p = await asyncio.subprocess.create_subprocess_exec(\n",
        "      *cmd,\n",
        "      stdout=asyncio.subprocess.PIPE,\n",
        "      stderr=asyncio.subprocess.PIPE,\n",
        "  )\n",
        "\n",
        "  async def pipe(lines):\n",
        "    async for line in lines:\n",
        "      print(line.strip().decode('utf-8'))\n",
        "\n",
        "  await asyncio.gather(\n",
        "      pipe(p.stdout),\n",
        "      pipe(p.stderr),\n",
        "  )\n",
        "\n",
        "\n",
        "await asyncio.gather(\n",
        "    run(['ollama', 'serve']),\n",
        "    run(['ngrok', 'config', 'add-authtoken', NGROK_TOKEN]),\n",
        "    run(['ngrok', 'http', '--log', 'stderr', '11434']),\n",
        "    # run([\"ollama\",\"run\",\"llama2\"]),\n",
        "    run([\"ollama\",\"run\",\"mistral\"]),\n",
        ")"
      ],
      "metadata": {
        "id": "u1PG3y83Z-u5"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "machine_shape": "hm",
      "gpuType": "T4",
      "authorship_tag": "ABX9TyMLapRySjkHA++gSoAkRSHn",
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "nbformat": 4,
  "nbformat_minor": 0
}