auth0-ai-langchain 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of auth0-ai-langchain might be problematic. Click here for more details.

@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,221 @@
1
+ Metadata-Version: 2.3
2
+ Name: auth0-ai-langchain
3
+ Version: 0.1.1
4
+ Summary: This package is an SDK for building secure AI-powered applications using Auth0, Okta FGA and LangChain.
5
+ License: Apache-2.0
6
+ Author: Auth0
7
+ Author-email: support@auth0.com
8
+ Requires-Python: >=3.11,<4.0
9
+ Classifier: License :: OSI Approved :: Apache Software License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Requires-Dist: auth0-ai (>=0.1.0,<0.2.0)
15
+ Requires-Dist: langchain (>=0.3.20,<0.4.0)
16
+ Requires-Dist: langchain-core (>=0.3.43,<0.4.0)
17
+ Requires-Dist: langgraph (>=0.3.25,<0.4.0)
18
+ Requires-Dist: langgraph-sdk (>=0.1.55,<0.2.0)
19
+ Requires-Dist: openfga-sdk (>=0.9.0,<0.10.0)
20
+ Project-URL: Homepage, https://auth0.com
21
+ Description-Content-Type: text/markdown
22
+
23
+ # Auth0 AI for LangChain
24
+
25
+ `auth0-ai-langchain` is an SDK for building secure AI-powered applications using [Auth0](https://www.auth0.ai/), [Okta FGA](https://docs.fga.dev/) and [LangChain](https://python.langchain.com/docs/tutorials/).
26
+
27
+ ![Release](https://img.shields.io/pypi/v/auth0-ai-langchain) ![Downloads](https://img.shields.io/pypi/dw/auth0-ai-langchain) [![License](https://img.shields.io/:license-APACHE%202.0-blue.svg?style=flat)](https://opensource.org/license/apache-2-0)
28
+
29
+ ## Installation
30
+
31
+ > [!WARNING] > `auth0-ai-langchain` is currently under development and it is not intended to be used in production, and therefore has no official support.
32
+
33
+ ```bash
34
+ pip install auth0-ai-langchain
35
+ ```
36
+
37
+ ## Authorization for Tools
38
+
39
+ 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.
40
+
41
+ Full example of [Authorization for Tools](../../examples/authorization-for-tools/langchain-examples/).
42
+
43
+ 1. Create an instance of FGA Authorizer:
44
+
45
+ ```python
46
+ from langchain_auth0_ai.fga.fga_authorizer import FGAAuthorizer, FGAAuthorizerOptions
47
+
48
+ fga = FGAAuthorizer.create()
49
+ ```
50
+
51
+ **Note**: Here, you can configure and specify your FGA credentials. By `default`, they are read from environment variables:
52
+
53
+ ```sh
54
+ FGA_STORE_ID="<fga-store-id>"
55
+ FGA_CLIENT_ID="<fga-client-id>"
56
+ FGA_CLIENT_SECRET="<fga-client-secret>"
57
+ ```
58
+
59
+ 2. Define the FGA query (`build_query`) and, optionally, the `on_unauthorized` handler:
60
+
61
+ ```python
62
+ from langchain_core.runnables import ensure_config
63
+
64
+ async def build_fga_query(tool_input):
65
+ user_id = ensure_config().get("configurable",{}).get("user_id")
66
+ return {
67
+ "user": f"user:{user_id}",
68
+ "object": f"asset:{tool_input["ticker"]}",
69
+ "relation": "can_buy",
70
+ "context": {"current_time": datetime.now(timezone.utc).isoformat()}
71
+ }
72
+
73
+ def on_unauthorized(tool_input):
74
+ return f"The user is not allowed to buy {tool_input["qty"]} shares of {tool_input["ticker"]}."
75
+
76
+ use_fga = fga(FGAAuthorizerOptions(
77
+ build_query=build_fga_query,
78
+ on_unauthorized=on_unauthorized,
79
+ ))
80
+ ```
81
+
82
+ **Note**: The parameters given to the `build_query` and `on_unauthorized` functions are the same as those provided to the tool function.
83
+
84
+ 3. Wrap the tool:
85
+
86
+ ```python
87
+ from langchain_core.tools import StructuredTool
88
+
89
+ async def buy_tool_function(ticker: str, qty: int) -> str:
90
+ # TODO: implement buy operation
91
+ return f"Purchased {qty} shares of {ticker}"
92
+
93
+ func=use_fga(buy_tool_function)
94
+
95
+ buy_tool = StructuredTool(
96
+ func=func,
97
+ coroutine=func,
98
+ name="buy",
99
+ description="Use this function to buy stocks",
100
+ )
101
+ ```
102
+
103
+ ## Calling APIs On User's Behalf
104
+
105
+ The `Auth0AI.with_federated_connection` function exchanges user's refresh token taken from the runnable configuration (`config.configurable._credentials.refresh_token`) for a Federated Connection API token.
106
+
107
+ Full Example of [Calling APIs On User's Behalf](../../examples/calling-apis/langchain-examples/).
108
+
109
+ 1. Define a tool with the proper authorizer:
110
+
111
+ ```python
112
+ from langchain_auth0_ai.auth0_ai import Auth0AI
113
+ from langchain_auth0_ai.federated_connections import get_access_token_for_connection
114
+ from langchain_core.tools import StructuredTool
115
+
116
+ auth0_ai = Auth0AI()
117
+
118
+ with_google_calendar_access = auth0_ai.with_federated_connection(
119
+ connection="google-oauth2",
120
+ scopes=["https://www.googleapis.com/auth/calendar.freebusy"]
121
+ )
122
+
123
+ def tool_function(date: datetime):
124
+ access_token = get_access_token_for_connection()
125
+ # Call Google API
126
+
127
+ check_calendar_tool = with_google_calendar_access(
128
+ StructuredTool(
129
+ name="check_user_calendar",
130
+ description="Use this function to check if the user is available on a certain date and time",
131
+ func=tool_function,
132
+ # ...
133
+ )
134
+ )
135
+ ```
136
+
137
+ 2. Add a node to your graph for your tools:
138
+
139
+ ```python
140
+ workflow = (
141
+ StateGraph(State)
142
+ .add_node(
143
+ "tools",
144
+ ToolNode(
145
+ [
146
+ check_calendar_tool,
147
+ # ...
148
+ ],
149
+ # The error handler should be disabled to allow interruptions to be triggered from within tools.
150
+ handle_tool_errors=False
151
+ )
152
+ )
153
+ # ...
154
+ )
155
+ ```
156
+
157
+ 3. Handle interruptions properly. If the tool does not have access to user's Google Calendar, it will throw an interruption.
158
+
159
+ ## RAG with FGA
160
+
161
+ 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.
162
+
163
+ Full Example of [RAG Application](../../examples/authorization-for-rag/langchain-examples/).
164
+
165
+ Create a retriever instance using the `FGARetriever` class.
166
+
167
+ ```python
168
+ from langchain.vectorstores import VectorStoreIndex
169
+ from langchain.schema import Document
170
+ from langchain_auth0_ai import FGARetriever
171
+ from openfga_sdk.client.models import ClientCheckRequest
172
+ from openfga_sdk import ClientConfiguration
173
+ from openfga_sdk.credentials import CredentialConfiguration, Credentials
174
+
175
+ # Define some docs:
176
+ documents = [
177
+ Document(page_content="This is a public doc", metadata={"doc_id": "public-doc"}),
178
+ Document(page_content="This is a private doc", metadata={"doc_id": "private-doc"}),
179
+ ]
180
+
181
+ # Create a vector store:
182
+ vector_store = VectorStoreIndex.from_documents(documents)
183
+
184
+ # Create a retriever:
185
+ base_retriever = vector_store.as_retriever()
186
+
187
+ # Create the FGA retriever wrapper:
188
+ retriever = FGARetriever(
189
+ base_retriever,
190
+ build_query=lambda node: ClientCheckRequest(
191
+ user=f'user:{user}',
192
+ object=f'doc:{node.metadata["doc_id"]}',
193
+ relation="viewer",
194
+ )
195
+ )
196
+
197
+ # Create a query engine:
198
+ query_engine = RetrieverQueryEngine.from_args(
199
+ retriever=retriever,
200
+ llm=OpenAI()
201
+ )
202
+
203
+ # Query:
204
+ response = query_engine.query("What is the forecast for ZEKO?")
205
+
206
+ print(response)
207
+ ```
208
+
209
+ ---
210
+
211
+ <p align="center">
212
+ <picture>
213
+ <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
214
+ <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150">
215
+ <img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
216
+ </picture>
217
+ </p>
218
+ <p align="center">Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0?</a></p>
219
+ <p align="center">
220
+ This project is licensed under the Apache 2.0 license. See the <a href="/LICENSE"> LICENSE</a> file for more info.</p>
221
+
@@ -0,0 +1,198 @@
1
+ # Auth0 AI for LangChain
2
+
3
+ `auth0-ai-langchain` is an SDK for building secure AI-powered applications using [Auth0](https://www.auth0.ai/), [Okta FGA](https://docs.fga.dev/) and [LangChain](https://python.langchain.com/docs/tutorials/).
4
+
5
+ ![Release](https://img.shields.io/pypi/v/auth0-ai-langchain) ![Downloads](https://img.shields.io/pypi/dw/auth0-ai-langchain) [![License](https://img.shields.io/:license-APACHE%202.0-blue.svg?style=flat)](https://opensource.org/license/apache-2-0)
6
+
7
+ ## Installation
8
+
9
+ > [!WARNING] > `auth0-ai-langchain` is currently under development and it is not intended to be used in production, and therefore has no official support.
10
+
11
+ ```bash
12
+ pip install auth0-ai-langchain
13
+ ```
14
+
15
+ ## Authorization for Tools
16
+
17
+ 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.
18
+
19
+ Full example of [Authorization for Tools](../../examples/authorization-for-tools/langchain-examples/).
20
+
21
+ 1. Create an instance of FGA Authorizer:
22
+
23
+ ```python
24
+ from langchain_auth0_ai.fga.fga_authorizer import FGAAuthorizer, FGAAuthorizerOptions
25
+
26
+ fga = FGAAuthorizer.create()
27
+ ```
28
+
29
+ **Note**: Here, you can configure and specify your FGA credentials. By `default`, they are read from environment variables:
30
+
31
+ ```sh
32
+ FGA_STORE_ID="<fga-store-id>"
33
+ FGA_CLIENT_ID="<fga-client-id>"
34
+ FGA_CLIENT_SECRET="<fga-client-secret>"
35
+ ```
36
+
37
+ 2. Define the FGA query (`build_query`) and, optionally, the `on_unauthorized` handler:
38
+
39
+ ```python
40
+ from langchain_core.runnables import ensure_config
41
+
42
+ async def build_fga_query(tool_input):
43
+ user_id = ensure_config().get("configurable",{}).get("user_id")
44
+ return {
45
+ "user": f"user:{user_id}",
46
+ "object": f"asset:{tool_input["ticker"]}",
47
+ "relation": "can_buy",
48
+ "context": {"current_time": datetime.now(timezone.utc).isoformat()}
49
+ }
50
+
51
+ def on_unauthorized(tool_input):
52
+ return f"The user is not allowed to buy {tool_input["qty"]} shares of {tool_input["ticker"]}."
53
+
54
+ use_fga = fga(FGAAuthorizerOptions(
55
+ build_query=build_fga_query,
56
+ on_unauthorized=on_unauthorized,
57
+ ))
58
+ ```
59
+
60
+ **Note**: The parameters given to the `build_query` and `on_unauthorized` functions are the same as those provided to the tool function.
61
+
62
+ 3. Wrap the tool:
63
+
64
+ ```python
65
+ from langchain_core.tools import StructuredTool
66
+
67
+ async def buy_tool_function(ticker: str, qty: int) -> str:
68
+ # TODO: implement buy operation
69
+ return f"Purchased {qty} shares of {ticker}"
70
+
71
+ func=use_fga(buy_tool_function)
72
+
73
+ buy_tool = StructuredTool(
74
+ func=func,
75
+ coroutine=func,
76
+ name="buy",
77
+ description="Use this function to buy stocks",
78
+ )
79
+ ```
80
+
81
+ ## Calling APIs On User's Behalf
82
+
83
+ The `Auth0AI.with_federated_connection` function exchanges user's refresh token taken from the runnable configuration (`config.configurable._credentials.refresh_token`) for a Federated Connection API token.
84
+
85
+ Full Example of [Calling APIs On User's Behalf](../../examples/calling-apis/langchain-examples/).
86
+
87
+ 1. Define a tool with the proper authorizer:
88
+
89
+ ```python
90
+ from langchain_auth0_ai.auth0_ai import Auth0AI
91
+ from langchain_auth0_ai.federated_connections import get_access_token_for_connection
92
+ from langchain_core.tools import StructuredTool
93
+
94
+ auth0_ai = Auth0AI()
95
+
96
+ with_google_calendar_access = auth0_ai.with_federated_connection(
97
+ connection="google-oauth2",
98
+ scopes=["https://www.googleapis.com/auth/calendar.freebusy"]
99
+ )
100
+
101
+ def tool_function(date: datetime):
102
+ access_token = get_access_token_for_connection()
103
+ # Call Google API
104
+
105
+ check_calendar_tool = with_google_calendar_access(
106
+ StructuredTool(
107
+ name="check_user_calendar",
108
+ description="Use this function to check if the user is available on a certain date and time",
109
+ func=tool_function,
110
+ # ...
111
+ )
112
+ )
113
+ ```
114
+
115
+ 2. Add a node to your graph for your tools:
116
+
117
+ ```python
118
+ workflow = (
119
+ StateGraph(State)
120
+ .add_node(
121
+ "tools",
122
+ ToolNode(
123
+ [
124
+ check_calendar_tool,
125
+ # ...
126
+ ],
127
+ # The error handler should be disabled to allow interruptions to be triggered from within tools.
128
+ handle_tool_errors=False
129
+ )
130
+ )
131
+ # ...
132
+ )
133
+ ```
134
+
135
+ 3. Handle interruptions properly. If the tool does not have access to user's Google Calendar, it will throw an interruption.
136
+
137
+ ## RAG with FGA
138
+
139
+ 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.
140
+
141
+ Full Example of [RAG Application](../../examples/authorization-for-rag/langchain-examples/).
142
+
143
+ Create a retriever instance using the `FGARetriever` class.
144
+
145
+ ```python
146
+ from langchain.vectorstores import VectorStoreIndex
147
+ from langchain.schema import Document
148
+ from langchain_auth0_ai import FGARetriever
149
+ from openfga_sdk.client.models import ClientCheckRequest
150
+ from openfga_sdk import ClientConfiguration
151
+ from openfga_sdk.credentials import CredentialConfiguration, Credentials
152
+
153
+ # Define some docs:
154
+ documents = [
155
+ Document(page_content="This is a public doc", metadata={"doc_id": "public-doc"}),
156
+ Document(page_content="This is a private doc", metadata={"doc_id": "private-doc"}),
157
+ ]
158
+
159
+ # Create a vector store:
160
+ vector_store = VectorStoreIndex.from_documents(documents)
161
+
162
+ # Create a retriever:
163
+ base_retriever = vector_store.as_retriever()
164
+
165
+ # Create the FGA retriever wrapper:
166
+ retriever = FGARetriever(
167
+ base_retriever,
168
+ build_query=lambda node: ClientCheckRequest(
169
+ user=f'user:{user}',
170
+ object=f'doc:{node.metadata["doc_id"]}',
171
+ relation="viewer",
172
+ )
173
+ )
174
+
175
+ # Create a query engine:
176
+ query_engine = RetrieverQueryEngine.from_args(
177
+ retriever=retriever,
178
+ llm=OpenAI()
179
+ )
180
+
181
+ # Query:
182
+ response = query_engine.query("What is the forecast for ZEKO?")
183
+
184
+ print(response)
185
+ ```
186
+
187
+ ---
188
+
189
+ <p align="center">
190
+ <picture>
191
+ <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
192
+ <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150">
193
+ <img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
194
+ </picture>
195
+ </p>
196
+ <p align="center">Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0?</a></p>
197
+ <p align="center">
198
+ This project is licensed under the Apache 2.0 license. See the <a href="/LICENSE"> LICENSE</a> file for more info.</p>