Created
January 24, 2024 12:09
-
-
Save danishi/20daf5ab165ace225451c4d824b2ee62 to your computer and use it in GitHub Desktop.
Call Open AI model with LangChain on Google Colaboratory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [] | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# APIキーを環境変数に設定" | |
| ], | |
| "metadata": { | |
| "id": "0_pbxie4tCv_" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": { | |
| "id": "4m3QOOQusIaz" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import os\n", | |
| "os.environ['OPENAI_API_KEY'] = \"sk-******************************\" # Set your openai-api-key" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# ライブラリのインストール" | |
| ], | |
| "metadata": { | |
| "id": "bGX-sbOXtpzz" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "!pip install langchain\n", | |
| "!pip install langchain-openai" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "GwkA3fvVtveW", | |
| "outputId": "d415a0cb-1082-46f5-8e10-576e9b6d00f2" | |
| }, | |
| "execution_count": 20, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Requirement already satisfied: langchain in /usr/local/lib/python3.10/dist-packages (0.1.3)\n", | |
| "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1)\n", | |
| "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.24)\n", | |
| "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.1)\n", | |
| "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3)\n", | |
| "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.6.3)\n", | |
| "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.33)\n", | |
| "Requirement already satisfied: langchain-community<0.1,>=0.0.14 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.0.15)\n", | |
| "Requirement already satisfied: langchain-core<0.2,>=0.1.14 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.1.15)\n", | |
| "Requirement already satisfied: langsmith<0.1,>=0.0.83 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.0.83)\n", | |
| "Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.23.5)\n", | |
| "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.10.13)\n", | |
| "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0)\n", | |
| "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.2.3)\n", | |
| "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n", | |
| "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n", | |
| "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n", | |
| "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n", | |
| "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", | |
| "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /usr/local/lib/python3.10/dist-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (3.20.2)\n", | |
| "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (0.9.0)\n", | |
| "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain) (2.4)\n", | |
| "Requirement already satisfied: anyio<5,>=3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.14->langchain) (3.7.1)\n", | |
| "Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.14->langchain) (23.2)\n", | |
| "Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (4.9.0)\n", | |
| "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2)\n", | |
| "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.6)\n", | |
| "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7)\n", | |
| "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2023.11.17)\n", | |
| "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n", | |
| "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1.14->langchain) (1.3.0)\n", | |
| "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1.14->langchain) (1.2.0)\n", | |
| "Requirement already satisfied: mypy-extensions>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain) (1.0.0)\n", | |
| "Requirement already satisfied: langchain-openai in /usr/local/lib/python3.10/dist-packages (0.0.3)\n", | |
| "Requirement already satisfied: langchain-core<0.2,>=0.1.13 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (0.1.15)\n", | |
| "Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (1.23.5)\n", | |
| "Requirement already satisfied: openai<2.0.0,>=1.6.1 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (1.9.0)\n", | |
| "Requirement already satisfied: tiktoken<0.6.0,>=0.5.2 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (0.5.2)\n", | |
| "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (6.0.1)\n", | |
| "Requirement already satisfied: anyio<5,>=3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (3.7.1)\n", | |
| "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (1.33)\n", | |
| "Requirement already satisfied: langsmith<0.1,>=0.0.83 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (0.0.83)\n", | |
| "Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (23.2)\n", | |
| "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (1.10.13)\n", | |
| "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (2.31.0)\n", | |
| "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.13->langchain-openai) (8.2.3)\n", | |
| "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai<2.0.0,>=1.6.1->langchain-openai) (1.7.0)\n", | |
| "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.6.1->langchain-openai) (0.26.0)\n", | |
| "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.6.1->langchain-openai) (1.3.0)\n", | |
| "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.6.1->langchain-openai) (4.66.1)\n", | |
| "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.6.1->langchain-openai) (4.9.0)\n", | |
| "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken<0.6.0,>=0.5.2->langchain-openai) (2023.6.3)\n", | |
| "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1.13->langchain-openai) (3.6)\n", | |
| "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1.13->langchain-openai) (1.2.0)\n", | |
| "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.6.1->langchain-openai) (2023.11.17)\n", | |
| "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.6.1->langchain-openai) (1.0.2)\n", | |
| "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.6.1->langchain-openai) (0.14.0)\n", | |
| "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.2,>=0.1.13->langchain-openai) (2.4)\n", | |
| "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain-core<0.2,>=0.1.13->langchain-openai) (3.3.2)\n", | |
| "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain-core<0.2,>=0.1.13->langchain-openai) (2.0.7)\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# 実行" | |
| ], | |
| "metadata": { | |
| "id": "jmapNj9yuB3H" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from langchain_openai import ChatOpenAI\n", | |
| "from langchain import PromptTemplate\n", | |
| "from langchain.schema import HumanMessage, SystemMessage\n", | |
| "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", | |
| "\n", | |
| "\n", | |
| "chat = ChatOpenAI(\n", | |
| " temperature=0,\n", | |
| " model=\"gpt-3.5-turbo\",\n", | |
| " streaming=True, # ストリーミングを有効にする\n", | |
| " callbacks=[\n", | |
| " StreamingStdOutCallbackHandler()\n", | |
| " ]\n", | |
| ")\n", | |
| "\n", | |
| "prompt = PromptTemplate(\n", | |
| " template=\"あなたは{birth_place}出身です。自己紹介をしてください。\",\n", | |
| " input_variables=[\n", | |
| " \"birth_place\"\n", | |
| " ]\n", | |
| ")\n", | |
| "\n", | |
| "response = chat.invoke(\n", | |
| " [\n", | |
| "\n", | |
| " HumanMessage(content=\"こんにちは。\")\n", | |
| " ]\n", | |
| ")\n", | |
| "print(f\"\\n-----------------------\")\n", | |
| "response = chat.invoke(\n", | |
| " [\n", | |
| " SystemMessage(content=prompt.format(birth_place=\"大阪\")),\n", | |
| " HumanMessage(content=\"こんにちは。\")\n", | |
| " ]\n", | |
| ")\n" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "kQdKFBcKuDUH", | |
| "outputId": "79573e25-98fb-47d7-8dfa-572ce5c2db3c" | |
| }, | |
| "execution_count": 21, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "こんにちは!どのようにお手伝いできますか?\n", | |
| "-----------------------\n", | |
| "こんにちは!私は大阪出身の〇〇と申します。大阪で生まれ育ち、現在も大阪に住んでいます。大阪弁を話すことができるので、おしゃべり好きな性格です。大阪の美味しい食べ物や観光地に詳しいので、おすすめの場所やグルメ情報などもお伝えできます。趣味は音楽を聴くことや散歩することで、大阪の街を歩くのが好きです。どうぞよろしくお願いします!" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# おまけDALL·Eを使った画像生成(ChatGPT Plusのみ)\n", | |
| "https://python.langchain.com/docs/integrations/tools/dalle_image_generator\n", | |
| " \n", | |
| "こちらの手順はうまくいかなかった。\n" | |
| ], | |
| "metadata": { | |
| "id": "kXHijC9h_S4J" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from openai import OpenAI\n", | |
| "from PIL import Image\n", | |
| "import requests\n", | |
| "import io\n", | |
| "\n", | |
| "\n", | |
| "client = OpenAI()\n", | |
| "\n", | |
| "response = client.images.generate(\n", | |
| " model=\"dall-e-3\",\n", | |
| " prompt=prompt.format(question=\"東京タワー\"),\n", | |
| " size=\"1024x1024\",\n", | |
| " quality=\"standard\",\n", | |
| " n=1,\n", | |
| ")\n", | |
| "\n", | |
| "print(response.data[0].url)\n", | |
| "Image.open(io.BytesIO(requests.get(response.data[0].url).content))" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 1000 | |
| }, | |
| "id": "AEYVoFzjBrSd", | |
| "outputId": "b3034903-943c-4be5-8795-c5a8e56cd219" | |
| }, | |
| "execution_count": 55, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "https://oaidalleapiprodscus.blob.core.windows.net/private/org-4R0ZHyk2sBmVPg0YjyjFlOPC/user-WWPniYgPyg0G24aKiXy4xkzl/img-TY8oG995JKp9DTfFJsAqBvHl.png?st=2024-01-24T05%3A56%3A53Z&se=2024-01-24T07%3A56%3A53Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-01-23T21%3A56%3A53Z&ske=2024-01-24T21%3A56%3A53Z&sks=b&skv=2021-08-06&sig=tmQW0RYxoiEOXr90T/jIzHBmCNtCN%2Bc%2BCQhw4WEyYNk%3D\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1024x1024>" | |
| ], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment