langchainを使用して、ローカルllmを動かすメモ

環境
macbook m2

参考サイト
ollamaのインストール

初期設定

  1. フォルダ作成

    1. mkdir langchain

  2. 仮想環境に入る

    1. source langchain_env/bin/activate

  3. pipアップグレード

    1. python -m pip install --upgrade pip

  4. ipykernelインストール

    1. pip install ipykernel

  5. langchain_kernelという名前のカーネルを作成

    1. python -m ipykernel install --user --name=langchain_kernel

  6. jupyterlabインストール

    1. pip install jupyterlab

  7. notebookインストール

    1. pip install notebook

  8. jupyter notebook起動

    1. jupyter notebook

  9. カーネル選択

    1. Jupyter Notebookを開いたら、langchain_kernelを選択

  10. 他のターミナルを開いて、ollama serve

    1. 新しいターミナルを開き、そこでollama serveコマンドを実行

右上のデバッグマークの横のカーネル選択で作成したカーネルを選択する。


次回以降

  1. ollama serve

  2. cd ~/langchain

  3. source langchain_env/bin/activate

  4. jupyter notebook

ソースコード

pip install langchain
from langchain_community.llms import Ollama
llm = Ollama(model="llama3")
llm.invoke("how can langsmith help with testing?")

出力結果

"Langsmith, a language model training platform, can indeed assist with testing in various ways:\n\n1. **Automated Testing**: Langsmith's API allows you to automate testing of your NLP models by generating input prompts and evaluating the outputs against expected results.\n2. **Error Analysis**: By analyzing the errors generated by your model, Langsmith can help identify areas that need improvement, such as misclassified examples or out-of-vocabulary words.\n3. **Model Comparison**: You can use Langsmith to test different NLP models (e.g., language translation, sentiment analysis) and compare their performance on a common dataset.\n4. **Data Augmentation**: Langsmith's data augmentation capabilities can help generate more diverse and challenging testing datasets, making your model more robust.\n5. **Human Evaluation**: Langsmith allows you to collect human ratings for your NLP models' outputs, providing valuable feedback on the quality of their responses.\n6. **Integration with Testing Frameworks**: Langsmith supports integration with popular testing frameworks like Pytest or Unittest, enabling seamless testing and validation of your NLP code.\n\nTo get started with using Langsmith for testing, you can:\n\n* Explore Langsmith's API documentation to learn how to integrate it with your preferred programming language.\n* Check out Langsmith's pre-built datasets and benchmarks to test your models against established baselines.\n* Reach out to the Langsmith community or support team for guidance on best practices and troubleshooting.\n\nBy leveraging Langsmith for testing, you can ensure that your NLP models are robust, accurate, and reliable, which is crucial for real-world applications."

この記事が気に入ったらサポートをしてみませんか?