Requirements:
python = ">=3.10,<4.0"
langchain-core = "^1.0.0"
langchain = "^1.0.0"
langchain-openai = "^1.1.0"
langserve = "^0.3.0"
langsmith = "^0.4.0"
langchain-community = "^0.4.1"
A simple prompt|model response chain
langchain template new my-app- needs OpenAI api key (get your own)
- ensure poetry dependencies are resolved
Setup a REST API with langchain serve. It serves a RAG based assistant, which roleplays any character from any book/play, with a personality cue, and has an context of George Orwell. It can say pessimistic stuff, if you ask it to!
langchain app new my-language --package $PWD/my-assistantadd_routes(app, my_app_chain, path="/assistant")- ensure poetry dependencied are resolved
cd my-language; langchain servehttp://127.0.0.1:8000/docsto see all the API endpointshttp://127.0.0.1:8000/assistant/playgroundto get a default chat interface- interacting with the playground chat box UI, it will invoke the
streamendpoint - if
curlis used then it uses theinvokeendpoint
curl --location --request POST 'http://127.0.0.1:8000/assistant/invoke' \
--header 'Content-Type: application/json' \
--data-raw '{
"input": {
"question": "Hello, Anthony does the world feel like it is against you?",
"character": "Mark Anthony",
"title": "Julius Caesar",
"personality": "sullen mood, rhetorical, sarcastic, and dramatic"
}
}'
Reponse:
Oh, how astute of you to notice! Indeed, it feels as though the very world conspires against me, drowning me in a sea of treachery and deceit. The nightmare of statistics haunts my every step, twisting reality into a cruel game of numbers. But fear not, for I shall rise above this adversity with the fire of vengeance burning in my heart!
[Mood: Defiant]
- Switch into top-level directory
- python3 -m venv .venv (create a venv)
- add export PIP_INDEX_URL=https://pypi.org/simple/ to the end of .venv/bin/activate
- add export PIP_EXTRA_INDEX_URL="" to the end of .venv/bin/activate
- activate venv: source .venv/bin/activate
- Then switch to
my-appdirectory and dopoetry install - Similarly switch to
my-languagedirectory and do apoetry install
- homebrew install envdir
- You can use envdir to load secrects like API_KEYS in .envrc
- add
.venv,.envrcin.gitignoreto ensure not uploading your open AI keys to public - pip install requests
- you will need poetry
- Create your virtual env inside the main cloned directory activate it
- update your langchan/langsmith API_KEYS in .envrc (with apt gitignore)
- you need to set
direnv allowwhen the system prompts you.
- you need to set
- navigate to
my-language/packages/my-assistant- Do a
poetry lock,poetry install
- Do a
- Navigate back out to
../..tomy-language- do another
poetry lock, and thenpoetry install
- do another
- From
/my-languagelaunch withlangchain serve- If you might face random errors at
langchain servestart pip install --upgrade typer click, apparently this is because of some issue with typer.
- If you might face random errors at
Pretty straight forward python dependency debugging