From 948027aef230e8ad4dab00203be1ec7deda1696a Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:22:15 -0800 Subject: [PATCH] Notebook style --- docs/docs/tutorials/auth/add_auth_server.md | 7 +- docs/docs/tutorials/auth/getting_started.md | 51 ++++++-------- docs/docs/tutorials/auth/resource_auth.md | 78 ++++++++++----------- 3 files changed, 64 insertions(+), 72 deletions(-) diff --git a/docs/docs/tutorials/auth/add_auth_server.md b/docs/docs/tutorials/auth/add_auth_server.md index fe99803af..8aa4323da 100644 --- a/docs/docs/tutorials/auth/add_auth_server.md +++ b/docs/docs/tutorials/auth/add_auth_server.md @@ -152,7 +152,10 @@ Let's test this with a real user account! ## Testing Authentication Flow -Let's test out our new authentication flow. You can run the following code in a file or notebook. +Let's test out our new authentication flow. You can run the following code in a file or notebook. You will need to provide: +- A valid email address +- A Supabase project URL (from [above](#setup-auth-provider)) +- A Supabase service role key (also from [above](#setup-auth-provider)) ```python import os @@ -199,7 +202,7 @@ Then run the code. !!! tip "About test emails" We'll create two test accounts by adding "+1" and "+2" to your email. For example, if you use "myemail@gmail.com", we'll create "myemail+1@gmail.com" and "myemail+2@gmail.com". All emails will be delivered to your original address. -⚠️ Before continuing: Check your email and click both confirmation links. +⚠️ Before continuing: Check your email and click both confirmation links. Supabase will will reject `/login` requests until after you have confirmed your users' email. Now let's test that users can only see their own data. Make sure the server is running (run `langgraph dev`) before proceeding. The following snippet requires the "anon public" key that you copied from the Supabase dashboard while [setting up the auth provider](#setup-auth-provider) previously. diff --git a/docs/docs/tutorials/auth/getting_started.md b/docs/docs/tutorials/auth/getting_started.md index 31d9f94af..0679f304a 100644 --- a/docs/docs/tutorials/auth/getting_started.md +++ b/docs/docs/tutorials/auth/getting_started.md @@ -105,45 +105,38 @@ langgraph dev --no-browser } ``` -Now let's try to chat with our bot. Create a new file `test_auth.py`: +Now let's try to chat with our bot. Run the following code in a file or notebook: ```python -import asyncio from langgraph_sdk import get_client - -async def test_auth(): - # Try without a token (should fail) - client = get_client(url="http://localhost:2024") - try: - thread = await client.threads.create() - print("❌ Should have failed without token!") - except Exception as e: - print("✅ Correctly blocked access:", e) - - # Try with a valid token - client = get_client( - url="http://localhost:2024", headers={"Authorization": "Bearer user1-token"} - ) - - # Create a thread and chat +# Try without a token (should fail) +client = get_client(url="http://localhost:2024") +try: thread = await client.threads.create() - print(f"✅ Created thread as Alice: {thread['thread_id']}") + print("❌ Should have failed without token!") +except Exception as e: + print("✅ Correctly blocked access:", e) - response = await client.runs.create( - thread_id=thread["thread_id"], - assistant_id="agent", - input={"messages": [{"role": "user", "content": "Hello!"}]}, - ) - print("✅ Bot responded:") - print(response) +# Try with a valid token +client = get_client( + url="http://localhost:2024", headers={"Authorization": "Bearer user1-token"} +) +# Create a thread and chat +thread = await client.threads.create() +print(f"✅ Created thread as Alice: {thread['thread_id']}") -if __name__ == "__main__": - asyncio.run(test_auth()) +response = await client.runs.create( + thread_id=thread["thread_id"], + assistant_id="agent", + input={"messages": [{"role": "user", "content": "Hello!"}]}, +) +print("✅ Bot responded:") +print(response) ``` -Run the test code and you should see that: +You should see that: 1. Without a valid token, we can't access the bot 2. With a valid token, we can create threads and chat diff --git a/docs/docs/tutorials/auth/resource_auth.md b/docs/docs/tutorials/auth/resource_auth.md index adda07adf..b5b67e812 100644 --- a/docs/docs/tutorials/auth/resource_auth.md +++ b/docs/docs/tutorials/auth/resource_auth.md @@ -87,58 +87,54 @@ Notice that our simple handler does two things: ## Testing Private Conversations -Let's test our authorization. Create a new file `test_private.py`: +Let's test our authorization. If we have set things up correctly, we should expect to see all ✅ messages. Be sure to have your development server running (run `langgraph dev`): ```python -import asyncio from langgraph_sdk import get_client -async def test_private(): - # Create clients for both users - alice = get_client( - url="http://localhost:2024", - headers={"Authorization": "Bearer user1-token"} - ) +# Create clients for both users +alice = get_client( + url="http://localhost:2024", + headers={"Authorization": "Bearer user1-token"} +) - bob = get_client( - url="http://localhost:2024", - headers={"Authorization": "Bearer user2-token"} - ) +bob = get_client( + url="http://localhost:2024", + headers={"Authorization": "Bearer user2-token"} +) - # Alice creates a thread and chats - alice_thread = await alice.threads.create() - print(f"✅ Alice created thread: {alice_thread['thread_id']}") +# Alice creates a thread and chats +alice_thread = await alice.threads.create() +print(f"✅ Alice created thread: {alice_thread['thread_id']}") - await alice.runs.create( - thread_id=alice_thread["thread_id"], - assistant_id="agent", - input={"messages": [{"role": "user", "content": "Hi, this is Alice's private chat"}]} - ) +await alice.runs.create( + thread_id=alice_thread["thread_id"], + assistant_id="agent", + input={"messages": [{"role": "user", "content": "Hi, this is Alice's private chat"}]} +) - # Bob tries to access Alice's thread - try: - await bob.threads.get(alice_thread["thread_id"]) - print("❌ Bob shouldn't see Alice's thread!") - except Exception as e: - print("✅ Bob correctly denied access:", e) +# Bob tries to access Alice's thread +try: + await bob.threads.get(alice_thread["thread_id"]) + print("❌ Bob shouldn't see Alice's thread!") +except Exception as e: + print("✅ Bob correctly denied access:", e) - # Bob creates his own thread - bob_thread = await bob.threads.create() - await bob.runs.create( - thread_id=bob_thread["thread_id"], - assistant_id="agent", - input={"messages": [{"role": "user", "content": "Hi, this is Bob's private chat"}]} - ) - print(f"✅ Bob created his own thread: {bob_thread['thread_id']}") +# Bob creates his own thread +bob_thread = await bob.threads.create() +await bob.runs.create( + thread_id=bob_thread["thread_id"], + assistant_id="agent", + input={"messages": [{"role": "user", "content": "Hi, this is Bob's private chat"}]} +) +print(f"✅ Bob created his own thread: {bob_thread['thread_id']}") - # List threads - each user only sees their own - alice_threads = await alice.threads.list() - bob_threads = await bob.threads.list() - print(f"✅ Alice sees {len(alice_threads)} thread") - print(f"✅ Bob sees {len(bob_threads)} thread") +# List threads - each user only sees their own +alice_threads = await alice.threads.list() +bob_threads = await bob.threads.list() +print(f"✅ Alice sees {len(alice_threads)} thread") +print(f"✅ Bob sees {len(bob_threads)} thread") -if __name__ == "__main__": - asyncio.run(test_private()) ``` Run the test code and you should see output like this: