This package is an SDK for building secure AI-powered applications using Auth0, Okta FGA and LlamaIndex.
Project description
Auth0 AI for LlamaIndex
auth0-ai-llamaindex is an SDK for building secure AI-powered applications using Auth0, Okta FGA and LlamaIndex.
Installation
⚠️ WARNING:
auth0-ai-llamaindexis currently under heavy development. We strictly follow Semantic Versioning (SemVer), meaning all breaking changes will only occur in major versions. However, please note that during this early phase, major versions may be released frequently as the API evolves. We recommend locking versions when using this in production.
pip install auth0-ai-llamaindex
Async Authorization
Auth0AI uses CIBA (Client Initiated Backchannel Authentication) to handle user confirmation asynchronously. This is useful when you need to confirm a user action before proceeding with a tool execution.
Full Example of Async Authorization.
Define a tool with the proper authorizer specifying a function to resolve the user id:
from auth0_ai_llamaindex.auth0_ai import Auth0AI, set_ai_context
from auth0_ai_llamaindex.async_authorization import get_async_authorization_credentials
from llama_index.core.tools import FunctionTool
# If not provided, Auth0 settings will be read from env variables: `AUTH0_DOMAIN`, `AUTH0_CLIENT_ID`, and `AUTH0_CLIENT_SECRET`
auth0_ai = Auth0AI()
with_async_authorization = auth0_ai.with_async_authorization(
scopes=["stock:trade"],
audience=os.getenv("AUDIENCE"),
requested_expiry=os.getenv("REQUESTED_EXPIRY"),
binding_message=lambda ticker, qty: f"Authorize the purchase of {qty} {ticker}",
user_id=lambda *_, **__: session["user"]["userinfo"]["sub"],
# Optional:
# store=InMemoryStore()
)
def tool_function(ticker: str, qty: int) -> str:
credentials = get_async_authorization_credentials()
headers = {
"Authorization": f"{credentials["token_type"]} {credentials["access_token"]}",
# ...
}
# Call API
trade_tool = with_async_authorization(
FunctionTool.from_defaults(
name="trade_tool",
description="Use this function to trade a stock",
fn=tool_function,
# ...
)
)
# Set the thread ID to associate with the retrieved credentials
set_ai_context("<thread-id>")
Async Authorization with RAR (Rich Authorization Requests)
Auth0AI supports RAR (Rich Authorization Requests) for Async Authorization. This allows you to provide additional authorization parameters to be displayed during the user confirmation request.
When defining the tool authorizer, you can specify the authorization_details parameter to include detailed information about the authorization being requested:
with_async_authorization = auth0_ai.with_async_authorization(
scopes=["stock:trade"],
audience=os.getenv("AUDIENCE"),
requested_expiry=os.getenv("REQUESTED_EXPIRY"),
binding_message=lambda ticker, qty: f"Authorize the purchase of {qty} {ticker}",
authorization_details=lambda ticker, qty: [
{
"type": "trade_authorization",
"qty": qty,
"ticker": ticker,
"action": "buy"
}
],
user_id=lambda *_, **__: ensure_config().get("configurable", {}).get("user_id"),
# Optional:
# store=InMemoryStore()
)
To use RAR with CIBA, you need to set up authorization details in your Auth0 tenant. This includes defining the authorization request parameters and their types. Additionally, the Guardian SDK is required to handle these authorization details in your authorizer app.
For more information on setting up RAR with CIBA, refer to:
Authorization for Tools
The FGAAuthorizer can leverage Okta FGA to authorize tools executions. The FGAAuthorizer.create function can be used to create an authorizer that checks permissions before executing the tool.
Full Example of Authorization for Tools.
- Create an instance of FGA Authorizer:
from auth0_ai_llamaindex.fga import FGAAuthorizer
# If not provided, FGA settings will be read from env variables: `FGA_STORE_ID`, `FGA_CLIENT_ID`, `FGA_CLIENT_SECRET`, etc.
fga = FGAAuthorizer.create()
- Define the FGA query (
build_query) and, optionally, theon_unauthorizedhandler:
def build_fga_query(tool_input):
return {
"user": f"user:{context.get("user_id")}",
"object": f"asset:{tool_input["ticker"]}",
"relation": "can_buy",
"context": {"current_time": datetime.now(timezone.utc).isoformat()}
}
def on_unauthorized(tool_input):
return f"The user is not allowed to buy {tool_input["qty"]} shares of {tool_input["ticker"]}."
use_fga = fga(
build_query=build_fga_query,
on_unauthorized=on_unauthorized,
)
Note: The parameters given to the build_query and on_unauthorized functions are the same as those provided to the tool function.
- Wrap the tool:
from llama_index.core.tools import FunctionTool
async def buy_tool_function(ticker: str, qty: int) -> str:
# TODO: implement buy operation
return f"Purchased {qty} shares of {ticker}"
func=use_fga(buy_tool_function)
return FunctionTool.from_defaults(
fn=func,
async_fn=func,
name="buy",
description="Use this function to buy stocks",
)
Calling APIs On User's Behalf
The Auth0AI.with_token_vault function exchanges user's refresh token for a Token Vault access token with the third-party.
Full Example of Calling APIs On User's Behalf.
Basic Usage
Define a tool with the proper authorizer specifying a function to resolve the user's refresh token:
from auth0_ai_llamaindex.auth0_ai import Auth0AI, set_ai_context
from auth0_ai_llamaindex.token_vault import get_credentials_from_token_vault
from llama_index.core.tools import FunctionTool
# If not provided, Auth0 settings will be read from env variables: `AUTH0_DOMAIN`, `AUTH0_CLIENT_ID`, and `AUTH0_CLIENT_SECRET`
auth0_ai = Auth0AI()
with_google_calendar_access = auth0_ai.with_token_vault(
connection="google-oauth2",
scopes=["openid", "https://www.googleapis.com/auth/calendar.freebusy"],
refresh_token=lambda *_args, **_kwargs: session["user"]["refresh_token"],
# Optional:
# authorization_params={"login_hint": "user@example.com", "ui_locales": "en"}
# store=InMemoryStore()
)
def tool_function(date: datetime):
credentials = get_credentials_from_token_vault()
# Call Google API using credentials["access_token"]
check_calendar_tool = with_google_calendar_access(
FunctionTool.from_defaults(
name="check_user_calendar",
description="Use this function to check if the user is available on a certain date and time",
fn=tool_function,
# ...
)
)
# Set the thread ID to associate with the retrieved credentials
set_ai_context("<thread-id>")
Additional Authorization Parameters
The authorization_params parameter is optional and can be used to pass additional authorization parameters needed to connect an account (e.g., login_hint, ui_locales):
with_google_calendar_access = auth0_ai.with_token_vault(
connection="google-oauth2",
scopes=["openid", "https://www.googleapis.com/auth/calendar.freebusy"],
refresh_token=lambda *_args, **_kwargs: session["user"]["refresh_token"],
authorization_params={"login_hint": "user@example.com", "ui_locales": "en"}
)
RAG with FGA
The FGARetriever can be used to filter documents based on access control checks defined in Okta FGA. This retriever performs batch checks on retrieved documents, returning only the ones that pass the specified access criteria.
Full Example of RAG Application.
from llama_index.core import VectorStoreIndex, Document
from auth0_ai_llamaindex import FGARetriever
from openfga_sdk.client.models import ClientCheckRequest
from openfga_sdk import ClientConfiguration
from openfga_sdk.credentials import CredentialConfiguration, Credentials
# Define some docs:
documents = [
Document(text="This is a public doc", doc_id="public-doc"),
Document(text="This is a private doc", doc_id="private-doc"),
]
# Create a vector store:
vector_store = VectorStoreIndex.from_documents(documents)
# Create a retriever:
base_retriever = vector_store.as_retriever()
# Create the FGA retriever wrapper.
# If not provided, FGA settings will be read from env variables: `FGA_STORE_ID`, `FGA_CLIENT_ID`, `FGA_CLIENT_SECRET`, etc.
retriever = FGARetriever(
base_retriever,
build_query=lambda node: ClientCheckRequest(
user=f'user:{user}',
object=f'doc:{node.ref_doc_id}',
relation="viewer",
)
)
# Create a query engine:
query_engine = RetrieverQueryEngine.from_args(
retriever=retriever,
llm=OpenAI()
)
# Query:
response = query_engine.query("What is the forecast for ZEKO?")
print(response)
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the Apache 2.0 license. See the LICENSE file for more info.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file auth0_ai_llamaindex-1.0.1.tar.gz.
File metadata
- Download URL: auth0_ai_llamaindex-1.0.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
609a39f417aee96847331b7e2094c51f0622a530297b8f7a6e3615a95a08b13d
|
|
| MD5 |
1f4553987ac9c0d623523e5dbbbb5165
|
|
| BLAKE2b-256 |
e98a6967ad0e29ce37f1abbe1fbeab99375900205f48a7364f28497877b702af
|
File details
Details for the file auth0_ai_llamaindex-1.0.1-py3-none-any.whl.
File metadata
- Download URL: auth0_ai_llamaindex-1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab031bf073d021e2909c09455c083ef2e2e9bae05be9cdb4d9e5bdb8bf3d0ea7
|
|
| MD5 |
3ca436607a760d80d38d9bb853b5fd9d
|
|
| BLAKE2b-256 |
7d5c274d3b2081908670d12c93a5ee464e73792eb5297aff7fa17617fc5e1717
|