Skip to content

Commit

Permalink
Example: Per user logic using per request modifier (#368)
Browse files Browse the repository at this point in the history
Add an example that shows how to implement per user logic using per request modifier.
  • Loading branch information
eyurtsev committed Jan 4, 2024
1 parent b3dd8a6 commit 6ab86af
Show file tree
Hide file tree
Showing 2 changed files with 443 additions and 0 deletions.
207 changes: 207 additions & 0 deletions examples/auth/per_req_config_modifier/client.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Client\n",
"\n",
"This is an example client that interacts with the server that has \"auth\".\n",
"\n",
"Please reference appropriate documentation in the server code and in FastAPI to actually make this secure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Login as Alice"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import requests\n",
"\n",
"response = requests.post(\"http://localhost:8000/token\", data={\"username\": \"alice\", \"password\": \"secret1\"})\n",
"result = response.json()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"token = result['access_token']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"inputs = {\"input\": \"hello\"}\n",
"response = requests.post(\"http://localhost:8000/invoke\", \n",
" json={\n",
" 'input': 'hello',\n",
" },\n",
" headers={\n",
" 'Authorization': f\"Bearer {token}\"\n",
" }\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"{'output': [{'page_content': 'cats like mice',\n",
" 'metadata': {'owner_id': 'alice'},\n",
" 'type': 'Document'},\n",
" {'page_content': 'cats like cheese',\n",
" 'metadata': {'owner_id': 'alice'},\n",
" 'type': 'Document'}],\n",
" 'callback_events': [],\n",
" 'metadata': {'run_id': '00000000-0000-0000-0000-000000000000'}}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.json()"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"You can also interact with this via the RemoteRunnable interface (to use in other chains)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langserve import RemoteRunnable\n",
"\n",
"remote_runnable = RemoteRunnable(\"http://localhost:8000/\", headers={\"Authorization\": f\"Bearer {token}\"})"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[Document(page_content='cats like mice', metadata={'owner_id': 'alice'}),\n",
" Document(page_content='cats like cheese', metadata={'owner_id': 'alice'})]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await remote_runnable.ainvoke(\"cat\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Login as John"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import requests\n",
"\n",
"response = requests.post(\"http://localhost:8000/token\", data={\"username\": \"john\", \"password\": \"secret2\"})\n",
"token = response.json()['access_token']\n",
"remote_runnable = RemoteRunnable(\"http://localhost:8000/\", headers={\"Authorization\": f\"Bearer {token}\"})"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[Document(page_content='i like walks by the ocean', metadata={'owner_id': 'john'}),\n",
" Document(page_content='dogs like sticks', metadata={'owner_id': 'john'}),\n",
" Document(page_content='my favorite food is cheese', metadata={'owner_id': 'john'})]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await remote_runnable.ainvoke(\"water\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 6ab86af

Please sign in to comment.