Learn the entire LangChain tech-stack fast πββοΈ
- Install Python (3.11.6 used in video).
- Install Poetry.
- Install DirEnv (or use another secret manager).
- Get your OpenAI API key.
- Sign up for LangSmith and get your LangSmith API key (https://smith.langchain.com/ and access API keys via the π icon on the bottom left).
-
If using a secret file (e.g.:
.envrc
), create a.gitignore
file similar to the one in this repository, to prevent accidentally sharing your API keys. -
Create a
.envrc
file and populate it with this template:
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
export LANGCHAIN_API_KEY=<your-langsmith-api-key>
export LANGCHAIN_PROJECT=langchain-quickstart # if not specified, defaults to "default"
export OPENAI_API_KEY=<your-openai-api-key>
- Open a new terminal and run these commands:
poetry init -n --python=3.11.6 && poetry add langchain-cli
source ./.venv/bin/activate
poetry run langchain app new api --package pirate-speak
Type 'Y' when prompted to install pirate-speak as a mirrored module
- Replace the
NotImplemented
route inapi/app/server.py
with a route for your component chain:
from pirate_speak.chain import chain as pirate_speak_chain
add_routes(app, pirate_speak_chain, path="/pirate-speak")
- Update the project
name
inapi/pyproject.toml
:
name = "api"
This can be any name other than __app_name__
- Open a new terminal in
/api
and run these commands:
poetry install
source ./.venv/bin/activate
poetry run langchain serve
-
Visit http://127.0.0.1:8000/pirate-speak/playground/ in your web browser and play with your chain.
-
Open LangSmith (https://smith.langchain.com/) and visit the project page. View one of your traces (created when you tested the playground demo) and use it to create a dataset.
-
View the dataset and click
New Test Run
to get a code snippet:
client = langsmith.Client()
chain_results = client.run_on_dataset(
dataset_name="quickstart-dataset", # this will change if you use a different dataset name.
llm_or_chain_factory=chain,
project_name="test-formal-project-4",
concurrency_level=5,
verbose=True,
)
- Create a new PyTest file
test_chain.py
inapi/packages/pirate-speak/tests/
:
from pirate_assistant.chain import chain
import langsmith
from datetime import datetime # import datetime module to get a timestamp
def test_chain():
client = langsmith.Client()
chain_results = client.run_on_dataset(
dataset_name="quickstart-dataset",
llm_or_chain_factory=chain,
project_name=f"quickstart-dataset-test-{int(datetime.now().strftime('%Y%m%d%H%M%S'))}", # use a timestamped unique project name each re-run
concurrency_level=5,
verbose=True,
)
To enable PyTest re-runs, we want to use a timestamped unique project name to store each successive test result.
- Open a terminal in
/api
and run these commands:
poetry add pytest --group=dev
poetry run python -m pytest -s .
-
View your dataset test runs, and add the trace to a new annotation queue.
-
View your annotation queue and explore the review interface.
ππ Congratulations! ππ You've completed this tutorial and now have a complete LangChain project. If you need more help with navigating the LangSmith interface, go view the video version of this tutorial on Youtube.
MIT