├── .gitignore ├── .gitmodules ├── .tool-versions ├── LICENSE ├── README.md ├── learning_langchain ├── 0.requests.py ├── 1.models.py ├── 2.prompts.py ├── 3.chain.py ├── 3.chain2.py ├── 4.indexes.py ├── 5.memory.py ├── 6.agents.py └── __init__.py ├── poetry.lock ├── pyproject.toml └── tests └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "langchain"] 2 | path = langchain 3 | url = git@github.com:hwchase17/langchain.git 4 | -------------------------------------------------------------------------------- /.tool-versions: -------------------------------------------------------------------------------- 1 | python 3.11.2 2 | poetry 1.4.1 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 os1ma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # learning-langchain 2 | 3 | ## 依存関係 4 | 5 | - Python 6 | - Poetry 7 | 8 | バージョンは [.tool-verisons](.tool-versions) に書かれています。 9 | 10 | > **Note** 11 | > .tool-verisons は [asdf](https://asdf-vm.com/) の設定ファイルです。 12 | 13 | ## 実行手順 14 | 15 | Python のパッケージは Poetry で管理しています。 16 | 以下のコマンドでインストールしてください。 17 | 18 | ```console 19 | poetry install 20 | ``` 21 | 22 | 各サンプルコードは以下のコマンドで実行できます。 23 | 24 | ```console 25 | poetry run python learning_langchain/<ファイル名> 26 | ``` 27 | 28 | ### 4.indexes.py を実行する場合 29 | 30 | 4.indexes.py は、langchain のドキュメントを読み込んで使う例となっています。 31 | 32 | 以下のコマンドで langchain のドキュメントを準備してから実行してください。 33 | 34 | ```console 35 | git submodule update --init 36 | cd langchain 37 | poetry install 38 | make docs_build 39 | ``` 40 | -------------------------------------------------------------------------------- /learning_langchain/0.requests.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import os 3 | import json 4 | 5 | url = 'https://api.openai.com/v1/completions' 6 | headers = { 7 | 'Content-Type': 'application/json', 8 | 'Authorization': 'Bearer ' + os.environ['OPENAI_API_KEY'] 9 | } 10 | data = { 11 | 'model': 'text-davinci-003', 12 | 'prompt': 'hello!', 13 | 'temperature': 0, 14 | } 15 | 16 | response = requests.post(url=url, headers=headers, json=data) 17 | print(json.dumps(response.json(), indent=2)) 18 | -------------------------------------------------------------------------------- /learning_langchain/1.models.py: -------------------------------------------------------------------------------- 1 | from langchain.llms import OpenAI 2 | 3 | 4 | llm = OpenAI(model_name="text-davinci-003", temperature=0) 5 | result = llm("GPTとして自己紹介してください。日本語で2文程度でお願いします。") 6 | print(result) 7 | -------------------------------------------------------------------------------- /learning_langchain/2.prompts.py: -------------------------------------------------------------------------------- 1 | from langchain.prompts import PromptTemplate 2 | 3 | 4 | template = """ 5 | 次のコマンドの概要を説明してください。 6 | 7 | コマンド: {command} 8 | """ 9 | 10 | prompt = PromptTemplate( 11 | input_variables=["command"], 12 | template=template, 13 | ) 14 | 15 | result = prompt.format(command="echo") 16 | print(result) 17 | -------------------------------------------------------------------------------- /learning_langchain/3.chain.py: -------------------------------------------------------------------------------- 1 | from langchain.chains import LLMChain 2 | from langchain.prompts import PromptTemplate 3 | from langchain.llms import OpenAI 4 | import langchain 5 | 6 | 7 | langchain.verbose = True 8 | 9 | # Model を用意 10 | llm = OpenAI(model_name="text-davinci-003", temperature=0) 11 | 12 | # Prompt を用意 13 | template = """ 14 | 次のコマンドの概要を説明してください。 15 | 16 | コマンド: {command} 17 | """ 18 | prompt = PromptTemplate( 19 | input_variables=["command"], 20 | template=template, 21 | ) 22 | 23 | # Chain を作成 24 | chain = LLMChain(llm=llm, prompt=prompt) 25 | 26 | # 実行 27 | result = chain.run("echo") 28 | print(result) 29 | -------------------------------------------------------------------------------- /learning_langchain/3.chain2.py: -------------------------------------------------------------------------------- 1 | from langchain.chains import SimpleSequentialChain 2 | from langchain.chains import LLMChain 3 | from langchain.prompts import PromptTemplate 4 | from langchain.llms import OpenAI 5 | import langchain 6 | 7 | 8 | langchain.verbose = True 9 | 10 | # Model を用意 11 | llm = OpenAI(model_name="text-davinci-003", temperature=0) 12 | 13 | # 1 つ目の Prompt と Chain を用意 14 | cot_template = """ 15 | 以下の質問に回答してください。 16 | 17 | ### 質問 ### 18 | {question} 19 | ### 質問終了 ### 20 | 21 | ステップバイステップで考えましょう。 22 | """ 23 | cot_prompt = PromptTemplate( 24 | input_variables=["question"], 25 | template=cot_template, 26 | ) 27 | cot_chain = LLMChain(llm=llm, prompt=cot_prompt) 28 | 29 | # 2 つ目の Prompt と Chain を用意 30 | summarize_template = """ 31 | 入力を結論だけ一言に要約してください。 32 | 33 | ### 入力 ### 34 | {input} 35 | ### 入力終了 ### 36 | """ 37 | summarize_prompt = PromptTemplate( 38 | input_variables=["input"], 39 | template=summarize_template, 40 | ) 41 | summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt) 42 | 43 | # 2 つの Chain を直列に繋ぐ 44 | cot_summarize_chain = SimpleSequentialChain( 45 | chains=[cot_chain, summarize_chain]) 46 | 47 | # 実行 48 | result = cot_summarize_chain( 49 | "私は市場に行って10個のリンゴを買いました。隣人に2つ、修理工に2つ渡しました。それから5つのリンゴを買って1つ食べました。残りは何個ですか?") 50 | print(result['output']) 51 | -------------------------------------------------------------------------------- /learning_langchain/4.indexes.py: -------------------------------------------------------------------------------- 1 | from langchain.document_loaders import DirectoryLoader 2 | from langchain.indexes import VectorstoreIndexCreator 3 | import langchain 4 | 5 | 6 | langchain.verbose = True 7 | 8 | loader = DirectoryLoader("./langchain/docs/_build/html/", glob="**/*.html") 9 | index = VectorstoreIndexCreator().from_loaders([loader]) 10 | print("index created") 11 | 12 | result = index.query("LangChainの概要を1文で説明してください。") 13 | print(f"result: {result}") 14 | -------------------------------------------------------------------------------- /learning_langchain/5.memory.py: -------------------------------------------------------------------------------- 1 | from langchain.chains import ConversationChain 2 | from langchain.llms import OpenAI 3 | from langchain.memory import ConversationBufferMemory 4 | import langchain 5 | 6 | 7 | langchain.verbose = True 8 | 9 | llm = OpenAI(model_name="text-davinci-003", temperature=0) 10 | conversation = ConversationChain( 11 | llm=llm, 12 | memory=ConversationBufferMemory() 13 | ) 14 | 15 | while True: 16 | user_message = input("You: ") 17 | ai_message = conversation.predict(input=user_message) 18 | print(f"AI: {ai_message}") 19 | -------------------------------------------------------------------------------- /learning_langchain/6.agents.py: -------------------------------------------------------------------------------- 1 | from langchain.agents import load_tools 2 | from langchain.agents import initialize_agent 3 | from langchain.llms import OpenAI 4 | import langchain 5 | 6 | 7 | langchain.verbose = True 8 | 9 | llm = OpenAI(model_name="text-davinci-003", temperature=0) 10 | tools = load_tools(["terminal"], llm=llm) 11 | agent_executor = initialize_agent( 12 | tools, llm, agent="zero-shot-react-description") 13 | 14 | result = agent_executor.run("現在のディレクトリにあるファイルの一覧を教えてください。") 15 | print(result) 16 | -------------------------------------------------------------------------------- /learning_langchain/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/os1ma/learning-langchain/051bb1ad461f475840b68a7ab084e9b0be973ab0/learning_langchain/__init__.py -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "learning-langchain" 3 | version = "0.1.0" 4 | description = "" 5 | authors = ["Yuki Oshima <39944763+os1ma@users.noreply.github.com>"] 6 | readme = "README.md" 7 | packages = [{include = "learning_langchain"}] 8 | 9 | [tool.poetry.dependencies] 10 | python = "^3.11" 11 | langchain = "^0.0.144" 12 | openai = "^0.27.2" 13 | chromadb = "^0.3.14" 14 | unstructured = "^0.5.7" 15 | bs4 = "^0.0.1" 16 | requests = "^2.28.2" 17 | markdown = "^3.4.3" 18 | tiktoken = "^0.3.3" 19 | 20 | 21 | [tool.poetry.group.dev.dependencies] 22 | autopep8 = "^2.0.2" 23 | 24 | [build-system] 25 | requires = ["poetry-core"] 26 | build-backend = "poetry.core.masonry.api" 27 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/os1ma/learning-langchain/051bb1ad461f475840b68a7ab084e9b0be973ab0/tests/__init__.py --------------------------------------------------------------------------------