You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/core/settings.mdx
+63-30Lines changed: 63 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,49 +10,82 @@ import TabItem from '@theme/TabItem';
10
10
11
11
Certain settings need to be provided for CocoIndex to work, e.g. database connections, app namespace, etc.
12
12
13
-
## Launch CocoIndex
13
+
## Configure CocoIndex Settings
14
14
15
-
You have two ways to launch CocoIndex:
15
+
Note that in general, you have two ways to launch CocoIndex:
16
16
17
+
* Call CocoIndex APIs from your own Python application or library.
17
18
* Use [Cocoindex CLI](cli). It's handy for most routine indexing building and management tasks.
18
-
It will load settings from environment variables, either already set in your environment, or specified in `.env` file.
19
-
See [CLI](cli#environment-variables) for more details.
20
19
21
-
* Call CocoIndex functionality from your own Python application or library.
22
-
It's needed when you want to leverage CocoIndex support for query, or have your custom logic to trigger indexing, etc.
23
20
24
-
<Tabs>
25
-
<TabItemvalue="python"label="Python"default>
21
+
CocoIndex exposes process-level settings specified by `cocoindex.Settings` dataclass.
22
+
Settings can be configured in three different ways.
23
+
In the following sections, the later ones will override the earlier ones.
26
24
27
-
You need to explicitly call `cocoindex.init()` before doing anything with CocoIndex, and settings will be loaded during the call.
25
+
### Environment Variables
28
26
29
-
* If it's called without any argument, it will load settings from environment variables.
30
-
Only existing environment variables already set in your environment will be used.
31
-
If you want to load environment variables from a specific `.env` file, consider call `load_dotenv()` provided by the [`python-dotenv`](https://github.com/theskumar/python-dotenv) package.
27
+
The simplest approach is to set corresponding environment variables.
28
+
See [List of Environment Variables](#list-of-environment-variables) for specific environment variables.
32
29
33
-
```py
34
-
from dotenv import load_dotenv
35
-
import cocoindex
30
+
:::tip
36
31
37
-
load_dotenv()
38
-
cocoindex.init()
39
-
```
32
+
You can consider place a `.env` file in your directory.
33
+
The [CLI](cli#environment-variables) will load environment variables from the `.env` file (see [CLI](cli#environment-variables) for more details).
34
+
From your own main module, you can also load environment variables with a package like [`python-dotenv`](https://github.com/theskumar/python-dotenv).
40
35
41
-
* It takes an optional `cocoindex.Settings` dataclass object as argument, so you can also construct settings explicitly and pass to it:
36
+
:::
37
+
38
+
### Setting Function
39
+
40
+
A more flexible approach is to provide a setting function that returns a `cocoindex.Settings` dataclass object.
41
+
The setting function can have any name, and needs to be decorated with the `@cocoindex.settings` decorator, for example:
For example, you can call it in the main function of your application.
71
+
Once the `cocoindex.init()` is called with a `cocoindex.Settings` dataclass object as argument, the `@cocoindex.settings` function and environment variables will be ignored.
72
+
73
+
This is more flexible, as you can more easily construct `cocoindex.Settings` based on other stuffs you loaded earlier.
74
+
But be careful that if you call `cocoindex.init()` only under the path of main (e.g. within `if __name__ == "__main__":` guard), it won't be executed when you're using CocoIndex CLI, as it won't execute your main logic.
75
+
76
+
:::info
77
+
78
+
`cocoindex.init()` is optional:
79
+
80
+
- You can call `cocoindex.init()` with a `cocoindex.Settings` dataclass object as argument, or without any argument.
81
+
When without argument, the settings will be loaded from the `@cocoindex.settings` function or environment variables.
82
+
83
+
- You don't have to explicitly call `cocoindex.init()`.
84
+
CocoIndex will be automatically initialized when needed, e.g. when any method of any flow is called the first time.
85
+
But calling `cocoindex.init()` explicitly (usually at startup time, e.g. in the main function of your application) has the benefit of making sure CocoIndex library is initialized and any potential exceptions are raised earlier before proceeding with the application.
86
+
If you need this clarity, you can call it explicitly even if you don't want to provide settings by the `cocoindex.init()` call.
0 commit comments