Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chottokun/515bb525ffb8d0c9a59ba2fb73539c47 to your computer and use it in GitHub Desktop.
Save chottokun/515bb525ffb8d0c9a59ba2fb73539c47 to your computer and use it in GitHub Desktop.
ollama_lucas2024_oumuamua-v2.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4",
"collapsed_sections": [
"k8yY0-fnIRpP",
"KJhb4op_KGIQ",
"h8aJAR8xHM1h"
],
"authorship_tag": "ABX9TyN3H6lRDEeSYBqiIVTFRNCb",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/chottokun/515bb525ffb8d0c9a59ba2fb73539c47/ollama_lucas2024_oumuamua-v2-ipynb.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"# 感謝とリファレンス\n",
"# https://github.com/HawkClaws/oyama\n",
"# https://ollama.com/lucas2024/oumuamua-v2"
],
"metadata": {
"id": "V0xWAUncPLyo"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# ollama+oyamaで起動"
],
"metadata": {
"id": "Jr5QqUjdH2EG"
}
},
{
"cell_type": "code",
"source": [
"#@title ollama、huggingfaceのggufモデル選択\n",
"model_path = \"lucas2024/oumuamua-v2:f16\" # @param {type:\"string\"}\n"
],
"metadata": {
"cellView": "form",
"id": "8qonLAr4IpHb"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## apt & pip"
],
"metadata": {
"id": "k8yY0-fnIRpP"
}
},
{
"cell_type": "code",
"source": [
"# aptとか読み込みとか"
],
"metadata": {
"id": "Z_DS4Oc4H8Iv"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!apt-get -y install pciutils"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gjQWeyAYO86q",
"outputId": "92c21fc2-9545-4db7-ea81-5cde0cf212cc"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Reading package lists... Done\n",
"Building dependency tree... Done\n",
"Reading state information... Done\n",
"pciutils is already the newest version (1:3.7.0-6).\n",
"0 upgraded, 0 newly installed, 0 to remove and 45 not upgraded.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"!pip install -q git+https://github.com/HawkClaws/oyama.git ollama"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9b6iQFXBMTIN",
"outputId": "224f8a8a-e316-4826-9e84-18e7c995f99d"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"!pip install -q langchain-community tiktoken"
],
"metadata": {
"id": "zJSjnk17Mirg"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## モデルの読み込みと動作確認"
],
"metadata": {
"id": "y4ODQIzlIKRf"
}
},
{
"cell_type": "code",
"source": [
"from oyama import oyama\n",
"import ollama\n",
"\n",
"# Model Path\n",
"model_name = oyama.run(model_path)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FPJYTdEpMaS9",
"outputId": "970deb37-e1f0-4931-b25e-499ac2b0c6b7"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"command:=ollama --version\n",
"Output: Warning: could not connect to a running Ollama instance\n",
"Warning: client version is 0.1.44\n",
"Warning: could not connect to a running Ollama instance\n",
"Warning: client version is 0.1.44\n",
"command:=ollama serve\n",
"Server is not ready yet. Retrying...\n",
"Server is ready.\n",
"command:=ollama pull lucas2024/oumuamua-v2:f16\n",
"Output: \u001b[?25lpulling manifest ⠋ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠙ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠹ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠸ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠼ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest \n",
"pulling 6766700a63c1... 100% ▕████████████████▏ 14 GB \n",
"pulling 08dc6a72e6a2... 100% ▕████████████████▏ 60 B \n",
"pulling 45fef882b9aa... 100% ▕████████████████▏ 72 B \n",
"pulling 735ee3975077... 100% ▕████████████████▏ 124 B \n",
"pulling 4691cb8b439c... 100% ▕████████████████▏ 484 B \n",
"verifying sha256 digest \n",
"writing manifest \n",
"removing any unused layers \n",
"success \u001b[?25h\n",
"\u001b[?25lpulling manifest ⠋ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠙ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠹ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠸ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠼ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest \n",
"pulling 6766700a63c1... 100% ▕████████████████▏ 14 GB \n",
"pulling 08dc6a72e6a2... 100% ▕████████████████▏ 60 B \n",
"pulling 45fef882b9aa... 100% ▕████████████████▏ 72 B \n",
"pulling 735ee3975077... 100% ▕████████████████▏ 124 B \n",
"pulling 4691cb8b439c... 100% ▕████████████████▏ 484 B \n",
"verifying sha256 digest \n",
"writing manifest \n",
"removing any unused layers \n",
"success \u001b[?25h\n",
"Enable Model:lucas2024/oumuamua-v2:f16\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"response = ollama.chat(model=model_name, messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': '日本でお薦めの観光地を5つあげてください。',\n",
" },\n",
"])\n",
"print(response['message']['content'])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "h4MP20MNDhfj",
"outputId": "5f38a4cf-8342-4153-aec8-2d4635711e9b"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" 1. 富士山\n",
"富士山は、日本一の標高を誇る活火山であり、世界遺産にも登録されています。四季折々の姿が美しく、山頂からの眺めや五合目までの登山道など、様々な楽しみ方ができます。\n",
"2. 京都市\n",
"京都市は、千年以上の歴史と文化を持つ古都です。市内には、金閣寺、銀閣寺、清水寺など、有名な寺社が多数あります。また、京料理や抹茶、和菓子など、食文化も豊かで、観光客から高い人気を集めています。\n",
"3. 大阪市\n",
"大阪市は、活気に満ちた関西の大都市です。市内には、USJ(ユニバーサル・スタジオ・ジャパン)、道頓堀、心斎橋筋商店街など、楽しいスポットが数多くあります。また、たこ焼き、お好み焼き、串かつなど、食文化も独特で、観光客を魅了しています。\n",
"4. 沖縄県\n",
"沖縄県は、美しい海と白砂ビーチ、独自の文化と歴史が魅力的な南国の楽園です。有名な観光地には、那覇市の国際通りや首里城跡、石垣島や宮古島などの離島も含まれます。また、琉球料理や泡盛などの食文化も豊かで、多くの観光客を喜ばせています。\n",
"5. 北海道\n",
"北海道は、広大な自然と食文化が魅力的な日本の北部地域です。有名な観光地には、札幌市の大通公園や小樽運河、函館市の五稜郭や函館山からの夜景などが含まれます。また、北海道のグルメも好評で、海鮮料理、ジンギスカン、ラーメンなど、様々な美味しい食べ物を堪能できます。\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# 質問に答えてもらいます。"
],
"metadata": {
"id": "bw253pGUDiiB"
}
},
{
"cell_type": "code",
"source": [
"%%time\n",
"response = ollama.chat(model=model_name, messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': '日本でお薦めの観光地を5つあげてください。',\n",
" },\n",
"])\n",
"print(response['message']['content'])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "L8F01aS8MiWh",
"outputId": "4ef8498a-cb7d-4781-e648-725e93faae2a"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" もちろんです!日本にはたくさんの魅力的な観光地がありますが、ここでは特におすすめの場所を5つご紹介します。\n",
"1.京都府京都市:寺社仏閣や風情ある町並みが残る古都京都。金閣寺、銀閣寺、清水寺など有名なスポットが多数あります。\n",
"2.東京都港区:世界的に有名な都市で、最新のトレンドやグルメを楽しめます。渋谷区には原宿、表参道、代官山、恵比寿など人気エリアがたくさんあります。\n",
"3.大阪府大阪市:「食の街」として知られており、お好み焼き、たこ焼き、串カツなどの美味しいグルメを味わえます。大阪城や道頓堀など観光スポットも豊富です。\n",
"4.奈良県奈良市:世界遺産に登録された古寺が多数あり、特に法隆寺は有名です。平城宮跡や若草山など、歴史と自然を両方楽しめるエリアです。\n",
"5.沖縄県沖縄市:美しい海と白い砂浜が広がる人気のリゾート地です。シュノーケリングやダイビング、海中観光船「 submarine Explorer」など、さまざまなマリンアクティビティが楽しめます。\n",
"以上、日本でおすすめの観光地5つをご紹介しました。それぞれの場所には、様々な魅力と楽しみ方があるので、ぜひ足を運んでみてください!\n",
"CPU times: user 76.1 ms, sys: 14.4 ms, total: 90.5 ms\n",
"Wall time: 20.3 s\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%time\n",
"response = ollama.chat(model=model_name, messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': 'まどか☆マギカで一番かわいいのは?',\n",
" },\n",
"])\n",
"print(response['message']['content'])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "htFWenBFOTlT",
"outputId": "58ecbf28-5ea5-44a9-f346-b8215a216266"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" 「まどか☆マギカ」に登場するキャラクターの中で、「一番かわいい」と感じるかどうかは主観的な要素が強いため、明確な答えを出すことは難しいです。\n",
"しかし、多くのファンから支持されているかわいいキャラクターとしては、鹿目まどかや、美樹さやかが挙げられます。これらのキャラクターはそれぞれ違った魅力を持っていて、ファンの間で「一番かわいい」という議論を誘発しています。\n",
"CPU times: user 35.5 ms, sys: 4 ms, total: 39.5 ms\n",
"Wall time: 8.03 s\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%time\n",
"query = \"\"\"日本で二番目に高い山を検討して答えてください。\n",
"\"\"\"\n",
"response = ollama.chat(model=model_name, messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': query,\n",
" },\n",
"])\n",
"print(response['message']['content'])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "K5Bi0gbBcvNs",
"outputId": "0d702c70-7b20-49d5-b033-653a3a1af2c4"
},
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" 日本で二番目に高い山は、「富士山」です。高さは約39メートルありますが、二番目に高い独立峰という点でご回答いたしました。なお、日本一の高さを誇る山はエベレストで、世界中の山で最高峰です。\n",
"CPU times: user 24.2 ms, sys: 1.55 ms, total: 25.7 ms\n",
"Wall time: 4.9 s\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%time\n",
"query = \"\"\"\n",
"以下を簡単にまとめてください。\n",
"\n",
"ウィキペディア(英: Wikipedia)は、世界中のボランティアの共同作業によって執筆及び作成されるフリーの多言語[6]インターネット百科事典[7]。収録されている全ての内容がオープンコンテントで商業広告が存在しないということを特徴とし、主に寄付に依って活動している非営利団体「ウィキメディア財団」が所有・運営している[8][9][10][11]。「ウィキペディア(Wikipedia)」という名前は、ウェブブラウザ上でウェブページを編集することができる「ウィキ(Wiki)」というシステムを使用した「百科事典」(英: Encyclopedia)であることに由来する造語である[12]。設立者の1人であるラリー・サンガーにより命名された[13][14]。\n",
"\"\"\"\n",
"response = ollama.chat(model=model_name, messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': query,\n",
" },\n",
"])\n",
"print(response['message']['content'])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yuoewzZkdS8f",
"outputId": "45f8d27f-b75d-4d4e-8133-b5f988e65c9c"
},
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" ウィキペディアは、世界中のボランティアが執筆し、フリーの多言語インターネット百科事典として運営されている。すべての内容がオープンコンテントであり、商業広告もない。この活動は主に寄付に依っており、非営利団体「ウィキメディア財団」によって所有・運営されている。名前の由来は、ウェブブラウザ上でウェブページを編集できる「ウィキ」というシステムを使用した百科事典であることから来ている造語である。ラリー・サンガーにより命名された。\n",
"CPU times: user 42.1 ms, sys: 3.6 ms, total: 45.7 ms\n",
"Wall time: 9.41 s\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%time\n",
"query = \"\"\"\n",
"1+1*2+3/2+2^10を計算してください。計算が終わったら検算をしてください。最後に最終的な計算経過と結果を答えてください。\n",
"\"\"\"\n",
"response = ollama.chat(model=model_name, messages=[\n",
" {\n",
" 'role': 'user',\n",
" 'content': query,\n",
" },\n",
"])\n",
"print(response['message']['content'])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HWZrbaPH-VAt",
"outputId": "0b040385-e334-48ae-fdbc-597439571a2b"
},
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" まずは、与えられた式を順序通りに計算します。\n",
"1 + 1 * 2 + 3 / 2 + 2^10\n",
"= 1 + 2 + 1.5 + 4096\n",
"= 1 + 2 + 1.5 + 4096\n",
"= 1 + (2 + 1.5) + 4096\n",
"= 1 + 3.5 + 4096\n",
"= 1 + 3.5 + 4096\n",
"= 1 + (3.5 + 40000)\n",
"= 1 + 40003.5\n",
"= 40094.5\n",
"次に、検算を行います。\n",
"式を分解し、それぞれの計算を再確認します。\n",
"すべての計算が正しいことを確認できたため、検算は完了です。\n",
"最後に、最終的な計算経過と結果を述べます。\n",
"与えられた式 1+1*2+3/2+2^10 を順序通りに計算したところ、最終結果は 400094.5 となりました。検算も正しく行われたため、この結果が正しいことを確認できています。\n",
"CPU times: user 84.7 ms, sys: 9.73 ms, total: 94.5 ms\n",
"Wall time: 20.8 s\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# langchainで使ってみます。"
],
"metadata": {
"id": "3_edPDrVDsBb"
}
},
{
"cell_type": "markdown",
"source": [
"## 動作確認"
],
"metadata": {
"id": "zRPu7fqHKKyg"
}
},
{
"cell_type": "code",
"source": [
"from langchain_community.llms import Ollama\n",
"\n",
"llm = Ollama(\n",
" model=model_path,\n",
" temperature=0,\n",
")\n",
"\n",
"print(llm.invoke(\"面白いジョークを言ってください。\"))"
],
"metadata": {
"id": "irIGA8DuQb7c",
"outputId": "38904931-8013-4c74-b2d5-aeec0b68c1f8",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" もちろん、ジョークをお届けします!\n",
"なぜ猫はパソコンの上に乗るのか?\n",
"それは、猫が「キーボード猫」だからです!\n",
"(キーボードに見える猫の絵文字を想像してみてください)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from langchain.chains.summarize import load_summarize_chain\n",
"from langchain_community.document_loaders import WebBaseLoader\n",
"\n",
"loader = WebBaseLoader(\"https://www.aozora.gr.jp/cards/000879/files/127_15260.html\")\n",
"docs = loader.load()\n",
"chain = load_summarize_chain(llm, chain_type=\"stuff\")\n",
"result = chain.invoke(docs)\n",
"print(result[\"output_text\"])"
],
"metadata": {
"id": "JYAl9aIfQVi4",
"outputId": "b140a8a2-2898-457c-d698-d773ad1a99f0",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:langchain_community.utils.user_agent:USER_AGENT environment variable not set, consider setting it to identify your requests.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
" This text is a portion of \"The Adventures of Monsieur Satan\" by Ryūnosuke Umetsu. It describes an encounter between the protagonist, a man named Shigekuni Takenaka, and an old woman. The old woman is described as being in poor health, possibly suffering from malnutrition or starvation. The protagonist, Takenaka, initially approaches the old woman with suspicion and hostility, but later shows courage and empathy towards her. The text ends with Takenaka leaving the scene, and the old woman's fate is unknown.\n",
"\n",
"This summary provides a concise overview of the events described in the original text. It highlights key points such as the protagonist's initial suspicion and hostility towards the old woman, his later display of courage and empathy, and the ultimate uncertainty surrounding the old woman's fate. Overall, this summary effectively captures the essence of the original text while maintaining brevity and clarity.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## 要約させてみます。"
],
"metadata": {
"id": "PrCoHVTDKTD8"
}
},
{
"cell_type": "code",
"source": [
"# WebLoader\n",
"loader = WebBaseLoader(\"https://ja.wikipedia.org/wiki/%E5%8C%97%E5%B2%B3\")"
],
"metadata": {
"id": "LpP1Z36FQX3W"
},
"execution_count": 16,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%time\n",
"docs = loader.load()\n",
"chain = load_summarize_chain(llm, chain_type=\"stuff\")\n",
"result = chain.invoke(docs)\n",
"print(result[\"output_text\"])"
],
"metadata": {
"id": "AU2CsdpdQlHu",
"outputId": "f782737c-6780-47c7-f2b6-af08eb4de506",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"北岳は、南アルプスの山であり、日本百名山の一つです。標高は30フィート(約11メートル)で、南アルプス市に位置します。Wikipediaより取得。\"\n",
"CPU times: user 834 ms, sys: 12.4 ms, total: 846 ms\n",
"Wall time: 5.7 s\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%time\n",
"docs = loader.load()\n",
"chain = load_summarize_chain(llm, chain_type=\"refine\")\n",
"result = chain.invoke(docs)\n",
"print(result[\"output_text\"])"
],
"metadata": {
"id": "5y4lrTyLQnW8",
"outputId": "d488a2ac-758c-4466-ac6b-7996378e5262",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"北岳は、南アルプスの山であり、日本百名山の一つです。標高は30フィート(約11メートル)で、南アルプス市に位置します。Wikipediaより取得。\"\n",
"CPU times: user 1.28 s, sys: 8.71 ms, total: 1.29 s\n",
"Wall time: 5.02 s\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%time\n",
"from langchain.chains.combine_documents.stuff import StuffDocumentsChain\n",
"from langchain_core.prompts import PromptTemplate\n",
"\n",
"prompt_template = \"\"\"以下のテキストの簡潔な要約をします。:\n",
"\"{text}\"\n",
"要約:\"\"\"\n",
"prompt = PromptTemplate.from_template(prompt_template)\n",
"\n",
"# LLMChain\n",
"# llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
"\n",
"# StuffDocumentsChain\n",
"stuff_chain = prompt | llm\n",
"\n",
"# loader\n",
"docs = loader.load()\n",
"# generate\n",
"print(stuff_chain.invoke({\"text\": docs}))"
],
"metadata": {
"id": "pJC9AA-PVTUq",
"outputId": "594da66c-f0b6-4900-f5d7-8c94b18642a0",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" 北岳は山梨県南アルプス市に位置する山で、日本百名山の一つです。この記事では、北岳の地理的な情報や、周辺の山々について説明されています。また、外部リンクが提供されており、ウィキデータに関するOSMリレーションも言及されています。\n",
"CPU times: user 821 ms, sys: 10.5 ms, total: 831 ms\n",
"Wall time: 7.63 s\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# チャット形式遊ぶ"
],
"metadata": {
"id": "klIe7eoRDxNf"
}
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "R5Gg8glIHWgH"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## mesop"
],
"metadata": {
"id": "h8aJAR8xHM1h"
}
},
{
"cell_type": "code",
"source": [
"!pip install mesop"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8r-jfnqE_fZI",
"outputId": "23899bac-0bb7-4867-8ec6-d532200c2408"
},
"execution_count": 20,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: mesop in /usr/local/lib/python3.10/dist-packages (0.8.0)\n",
"Requirement already satisfied: absl-py in /usr/local/lib/python3.10/dist-packages (from mesop) (1.4.0)\n",
"Requirement already satisfied: deepdiff==6.7.1 in /usr/local/lib/python3.10/dist-packages (from mesop) (6.7.1)\n",
"Requirement already satisfied: flask in /usr/local/lib/python3.10/dist-packages (from mesop) (2.2.5)\n",
"Requirement already satisfied: libcst==1.1.0 in /usr/local/lib/python3.10/dist-packages (from mesop) (1.1.0)\n",
"Requirement already satisfied: markdown in /usr/local/lib/python3.10/dist-packages (from mesop) (3.6)\n",
"Requirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from mesop) (3.20.3)\n",
"Requirement already satisfied: pydantic==1.10.13 in /usr/local/lib/python3.10/dist-packages (from mesop) (1.10.13)\n",
"Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from mesop) (2.16.1)\n",
"Requirement already satisfied: watchdog in /usr/local/lib/python3.10/dist-packages (from mesop) (4.0.1)\n",
"Requirement already satisfied: ordered-set<4.2.0,>=4.0.2 in /usr/local/lib/python3.10/dist-packages (from deepdiff==6.7.1->mesop) (4.1.0)\n",
"Requirement already satisfied: typing-extensions>=3.7.4.2 in /usr/local/lib/python3.10/dist-packages (from libcst==1.1.0->mesop) (4.12.2)\n",
"Requirement already satisfied: typing-inspect>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from libcst==1.1.0->mesop) (0.9.0)\n",
"Requirement already satisfied: pyyaml>=5.2 in /usr/local/lib/python3.10/dist-packages (from libcst==1.1.0->mesop) (6.0.1)\n",
"Requirement already satisfied: Werkzeug>=2.2.2 in /usr/local/lib/python3.10/dist-packages (from flask->mesop) (3.0.3)\n",
"Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.10/dist-packages (from flask->mesop) (3.1.4)\n",
"Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.10/dist-packages (from flask->mesop) (2.2.0)\n",
"Requirement already satisfied: click>=8.0 in /usr/local/lib/python3.10/dist-packages (from flask->mesop) (8.1.7)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from Jinja2>=3.0->flask->mesop) (2.1.5)\n",
"Requirement already satisfied: mypy-extensions>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from typing-inspect>=0.4.0->libcst==1.1.0->mesop) (1.0.0)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import mesop as me\n",
"import mesop.labs as mel\n",
"\n",
"me.colab_run()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2gI8-eKo_VP7",
"outputId": "ffeed158-2d19-4bba-fed5-85ca9dc4468a"
},
"execution_count": 21,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"\u001b[32mRunning server on: http://localhost:32123\u001b[0m\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"@me.page(\n",
" path=\"/chat\",\n",
" title=\"Chat demo\",\n",
")\n",
"def page():\n",
" mel.chat(transform, title=\"Demo Chat\", bot_user=model_path)\n",
"\n",
"def transform(input: str, history: list[mel.ChatMessage]) -> str:\n",
"\n",
" messages = []\n",
" for his in history:\n",
" messages.extend([{'role': his.role, 'content': his.content}])\n",
"\n",
" last_message = [\n",
" {\n",
" 'role': 'user',\n",
" 'content': input,\n",
" },\n",
" ]\n",
"\n",
" messages.extend(last_message)\n",
"\n",
" res = ollama.chat(model=model_name,\n",
" messages=messages)\n",
"\n",
" res = res['message']['content']\n",
" return res"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "h9BjdHfu_Xis",
"outputId": "59fa8c81-0624-488e-fccc-e436888fefe2"
},
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" * Serving Flask app 'mesop.server.server'\n",
" * Debug mode: off\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"INFO:werkzeug:\u001b[31m\u001b[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\u001b[0m\n",
" * Running on all addresses (::)\n",
" * Running on http://[::1]:32123\n",
" * Running on http://[::1]:32123\n",
"INFO:werkzeug:\u001b[33mPress CTRL+C to quit\u001b[0m\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## チャット"
],
"metadata": {
"id": "SNdeF8V2HP3Y"
}
},
{
"cell_type": "code",
"source": [
"me.colab_show(path=\"/chat\", height=600)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 621
},
"id": "1pYXJQkB_blP",
"outputId": "40c0fa00-defa-43f2-ee35-c1f34318f788"
},
"execution_count": 23,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"(async (port, path, width, height, cache, element) => {\n",
" if (!google.colab.kernel.accessAllowed && !cache) {\n",
" return;\n",
" }\n",
" element.appendChild(document.createTextNode(''));\n",
" const url = await google.colab.kernel.proxyPort(port, {cache});\n",
" const iframe = document.createElement('iframe');\n",
" iframe.src = new URL(path, url).toString();\n",
" iframe.height = height;\n",
" iframe.width = width;\n",
" iframe.style.border = 0;\n",
" iframe.allow = [\n",
" 'accelerometer',\n",
" 'autoplay',\n",
" 'camera',\n",
" 'clipboard-read',\n",
" 'clipboard-write',\n",
" 'gyroscope',\n",
" 'magnetometer',\n",
" 'microphone',\n",
" 'serial',\n",
" 'usb',\n",
" 'xr-spatial-tracking',\n",
" ].join('; ');\n",
" element.appendChild(iframe);\n",
" })(32123, \"/chat\", \"100%\", 600, false, window.element)"
]
},
"metadata": {}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment