Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions docs/docs/core/initialization.mdx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
---
title: Initialization
description: Initialize the CocoIndex library
description: Initialize and set environment for CocoIndex library
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Initialize the CocoIndex Library

Before everything starts, the `cocoindex` library needs to be initialized with settings.
There're two options to do this:
Before everything starts, the CocoIndex library needs to be initialized with settings.
We'll talk about the code skeleton to initialize the library in your code, and the way to provide settings for CocoIndex.

* Using packaged main function. It's easier to start with.
## Initialize the library

There're two options to initialize in your code:

* Use packaged main function. It's easier to start with.
* Explicit initialization. It's more flexible.

## Using Packaged Main
### Packaged Main

The easiest way is to use a packaged main function:

Expand Down Expand Up @@ -45,33 +49,13 @@ This takes care of the following effects:
See [CocoIndex CLI](/docs/core/cli) for more details.
3. Otherwise, it will run the main function.

### Environment Variables

The following environment variables are supported:
See [Environment Variables](#environment-variables) for supported environment variables.

* `COCOINDEX_DATABASE_URL` (required): The URL of the Postgres database to use as the internal storage, e.g. `postgres://cocoindex:cocoindex@localhost/cocoindex`
* `COCOINDEX_DATABASE_USER` (optional): The username for the Postgres database. If not provided, username will come from `COCOINDEX_DATABASE_URL`.
* `COCOINDEX_DATABASE_PASSWORD` (optional): The password for the Postgres database. If not provided, password will come from `COCOINDEX_DATABASE_URL`.

## Explicit Initialization
### Explicit Initialization

Alternatively, for flexibility, you can also explicitly initialize the library by the `init()` function:

### Settings

It takes a `Settings` object as argument, which is a dataclass that contains the following fields:

* `database` (type: `DatabaseConnectionSpec`, required): The connection to the Postgres database.

#### DatabaseConnectionSpec

`DatabaseConnectionSpec` has the following fields:
* `url` (type: `str`, required): The URL of the Postgres database to use as the internal storage, e.g. `postgres://cocoindex:cocoindex@localhost/cocoindex`.
* `user` (type: `str`, optional): The username for the Postgres database. If not provided, username will come from `url`.
* `password` (type: `str`, optional): The password for the Postgres database. If not provided, password will come from `url`.

### Example

<Tabs>
<TabItem value="python" label="Python" default>

Expand All @@ -93,4 +77,45 @@ if __name__ == "__main__":
```

</TabItem>
</Tabs>
</Tabs>

## Settings

`cocoindex.Settings` is used to configure the CocoIndex library. It's a dataclass that contains the following fields:

* `database` (type: `DatabaseConnectionSpec`, required): The connection to the Postgres database.

### DatabaseConnectionSpec

`DatabaseConnectionSpec` configures the connection to a database. Only Postgres is supported for now. It has the following fields:

* `url` (type: `str`, required): The URL of the Postgres database to use as the internal storage, e.g. `postgres://cocoindex:cocoindex@localhost/cocoindex`.
* `user` (type: `str`, optional): The username for the Postgres database. If not provided, username will come from `url`.
* `password` (type: `str`, optional): The password for the Postgres database. If not provided, password will come from `url`.

:::tip

Please be careful that all values in `url` needs to be url-encoded if they contain special characters.
For this reason, prefer to use the separated `user` and `password` fields for username and password.

:::

:::info

If you use the Postgres database hosted by [Supabase](https://supabase.com/), please click **Connect** on your project dashboard and find the following URL:

* If you're on a IPv6 network, use the URL under **Direct connection**. You can visit [IPv6 test](https://test-ipv6.com/) to see if you have IPv6 Internet connection.
* Otherwise, use the URL under **Session pooler**.

:::

## Environment Variables

When you use the packaged main function, settings will be loaded from environment variables.
Each setting field has a corresponding environment variable:

| environment variable | corresponding field in `Settings` | required? |
|---------------------|-------------------|----------|
| `COCOINDEX_DATABASE_URL` | `database.url` | Yes |
| `COCOINDEX_DATABASE_USER` | `database.user` | No |
| `COCOINDEX_DATABASE_PASSWORD` | `database.password` | No |