cocoindex 0.1.29__cp313-cp313-macosx_11_0_arm64.whl → 0.1.30__cp313-cp313-macosx_11_0_arm64.whl

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.
Binary file
@@ -3,16 +3,19 @@ Auth registry is used to register and reference auth entries.
3
3
  """
4
4
 
5
5
  from dataclasses import dataclass
6
+ from typing import Generic, TypeVar
6
7
 
7
8
  from . import _engine
8
9
  from .convert import dump_engine_object
9
10
 
11
+ T = TypeVar("T")
12
+
10
13
  @dataclass
11
- class AuthEntryReference:
14
+ class AuthEntryReference(Generic[T]):
12
15
  """Reference an auth entry by its key."""
13
16
  key: str
14
17
 
15
- def add_auth_entry(key: str, value) -> AuthEntryReference:
18
+ def add_auth_entry(key: str, value: T) -> AuthEntryReference[T]:
16
19
  """Add an auth entry to the registry. Returns its reference."""
17
20
  _engine.add_auth_entry(key, dump_engine_object(value))
18
21
  return AuthEntryReference(key)
cocoindex/cli.py CHANGED
@@ -1,9 +1,9 @@
1
- import asyncio
2
1
  import click
3
2
  import datetime
3
+
4
4
  from rich.console import Console
5
5
 
6
- from . import flow, lib
6
+ from . import flow, lib, setting
7
7
  from .setup import sync_setup, drop_setup, flow_names_with_setup, apply_setup_changes
8
8
  from .runtime import execution_context
9
9
 
@@ -21,7 +21,7 @@ def ls(show_all: bool):
21
21
  """
22
22
  List all flows.
23
23
  """
24
- current_flow_names = [fl.name for fl in flow.flows()]
24
+ current_flow_names = flow.flow_names()
25
25
  persisted_flow_names = flow_names_with_setup()
26
26
  remaining_persisted_flow_names = set(persisted_flow_names)
27
27
 
@@ -58,9 +58,9 @@ def show(flow_name: str | None, color: bool):
58
58
  """
59
59
  Show the flow spec in a readable format with colored output.
60
60
  """
61
- flow = _flow_by_name(flow_name)
61
+ fl = _flow_by_name(flow_name)
62
62
  console = Console(no_color=not color)
63
- console.print(flow._render_text())
63
+ console.print(fl._render_text())
64
64
 
65
65
  @cli.command()
66
66
  def setup():
@@ -149,32 +149,59 @@ def evaluate(flow_name: str | None, output_dir: str | None, cache: bool = True):
149
149
  options = flow.EvaluateAndDumpOptions(output_dir=output_dir, use_cache=cache)
150
150
  fl.evaluate_and_dump(options)
151
151
 
152
- _default_server_settings = lib.ServerSettings.from_env()
152
+ # Create ServerSettings lazily upon first call, as environment variables may be loaded from files, etc.
153
+ COCOINDEX_HOST = 'https://cocoindex.io'
153
154
 
154
155
  @cli.command()
155
156
  @click.option(
156
- "-a", "--address", type=str, default=_default_server_settings.address,
157
- help="The address to bind the server to, in the format of IP:PORT.")
157
+ "-a", "--address", type=str,
158
+ help="The address to bind the server to, in the format of IP:PORT. "
159
+ "If unspecified, the address specified in COCOINDEX_SERVER_ADDRESS will be used.")
160
+ @click.option(
161
+ "-c", "--cors-origin", type=str,
162
+ help="The origins of the clients (e.g. CocoInsight UI) to allow CORS from. "
163
+ "Multiple origins can be specified as a comma-separated list. "
164
+ "e.g. `https://cocoindex.io,http://localhost:3000`. "
165
+ "Origins specified in COCOINDEX_SERVER_CORS_ORIGINS will also be included.")
166
+ @click.option(
167
+ "-ci", "--cors-cocoindex", is_flag=True, show_default=True, default=False,
168
+ help=f"Allow {COCOINDEX_HOST} to access the server.")
158
169
  @click.option(
159
- "-c", "--cors-origin", type=str, default=_default_server_settings.cors_origin,
160
- help="The origin of the client (e.g. CocoInsight UI) to allow CORS from. "
161
- "e.g. `http://cocoindex.io` if you want to allow CocoInsight to access the server.")
170
+ "-cl", "--cors-local", type=int,
171
+ help="Allow http://localhost:<port> to access the server.")
162
172
  @click.option(
163
173
  "-L", "--live-update", is_flag=True, show_default=True, default=False,
164
174
  help="Continuously watch changes from data sources and apply to the target index.")
165
175
  @click.option(
166
176
  "-q", "--quiet", is_flag=True, show_default=True, default=False,
167
177
  help="Avoid printing anything to the standard output, e.g. statistics.")
168
- def server(address: str, live_update: bool, quiet: bool, cors_origin: str | None):
178
+ def server(address: str | None, live_update: bool, quiet: bool, cors_origin: str | None,
179
+ cors_cocoindex: bool, cors_local: int | None):
169
180
  """
170
181
  Start a HTTP server providing REST APIs.
171
182
 
172
183
  It will allow tools like CocoInsight to access the server.
173
184
  """
174
- lib.start_server(lib.ServerSettings(address=address, cors_origin=cors_origin))
185
+ server_settings = setting.ServerSettings.from_env()
186
+ cors_origins: set[str] = set(server_settings.cors_origins or [])
187
+ if cors_origin is not None:
188
+ cors_origins.update(setting.ServerSettings.parse_cors_origins(cors_origin))
189
+ if cors_cocoindex:
190
+ cors_origins.add(COCOINDEX_HOST)
191
+ if cors_local is not None:
192
+ cors_origins.add(f"http://localhost:{cors_local}")
193
+ server_settings.cors_origins = list(cors_origins)
194
+
195
+ if address is not None:
196
+ server_settings.address = address
197
+
198
+ lib.start_server(server_settings)
199
+
175
200
  if live_update:
176
201
  options = flow.FlowLiveUpdaterOptions(live_mode=True, print_stats=not quiet)
177
202
  execution_context.run(flow.update_all_flows(options))
203
+ if COCOINDEX_HOST in cors_origins:
204
+ click.echo(f"Open CocoInsight at: {COCOINDEX_HOST}/cocoinsight")
178
205
  input("Press Enter to stop...")
179
206
 
180
207
 
cocoindex/functions.py CHANGED
@@ -1,10 +1,13 @@
1
1
  """All builtin functions."""
2
- from typing import Annotated, Any
2
+ from typing import Annotated, Any, TYPE_CHECKING
3
3
 
4
- import sentence_transformers
5
4
  from .typing import Float32, Vector, TypeAttr
6
5
  from . import op, llm
7
6
 
7
+ # Libraries that are heavy to import. Lazily import them later.
8
+ if TYPE_CHECKING:
9
+ import sentence_transformers
10
+
8
11
  class ParseJson(op.FunctionSpec):
9
12
  """Parse a text into a JSON object."""
10
13
 
@@ -35,9 +38,10 @@ class SentenceTransformerEmbedExecutor:
35
38
  """Executor for SentenceTransformerEmbed."""
36
39
 
37
40
  spec: SentenceTransformerEmbed
38
- _model: sentence_transformers.SentenceTransformer
41
+ _model: "sentence_transformers.SentenceTransformer"
39
42
 
40
43
  def analyze(self, text):
44
+ import sentence_transformers # pylint: disable=import-outside-toplevel
41
45
  args = self.spec.args or {}
42
46
  self._model = sentence_transformers.SentenceTransformer(self.spec.model, **args)
43
47
  dim = self._model.get_sentence_embedding_dimension()
cocoindex/lib.py CHANGED
@@ -1,76 +1,23 @@
1
1
  """
2
2
  Library level functions and states.
3
3
  """
4
- import os
5
4
  import sys
6
5
  import functools
7
6
  import inspect
8
7
 
9
- from typing import Callable, Self
10
- from dataclasses import dataclass
8
+ from typing import Callable
11
9
 
12
10
  from . import _engine
13
- from . import flow, query, cli
11
+ from . import flow, query, cli, setting
14
12
  from .convert import dump_engine_object
15
13
 
16
14
 
17
- def _load_field(target: dict[str, str], name: str, env_name: str, required: bool = False):
18
- value = os.getenv(env_name)
19
- if value is None:
20
- if required:
21
- raise ValueError(f"{env_name} is not set")
22
- else:
23
- target[name] = value
24
-
25
- @dataclass
26
- class DatabaseConnectionSpec:
27
- url: str
28
- user: str | None = None
29
- password: str | None = None
30
-
31
- @dataclass
32
- class Settings:
33
- """Settings for the cocoindex library."""
34
- database: DatabaseConnectionSpec
35
-
36
- @classmethod
37
- def from_env(cls) -> Self:
38
- """Load settings from environment variables."""
39
-
40
- db_kwargs: dict[str, str] = dict()
41
- _load_field(db_kwargs, "url", "COCOINDEX_DATABASE_URL", required=True)
42
- _load_field(db_kwargs, "user", "COCOINDEX_DATABASE_USER")
43
- _load_field(db_kwargs, "password", "COCOINDEX_DATABASE_PASSWORD")
44
- database = DatabaseConnectionSpec(**db_kwargs)
45
- return cls(database=database)
46
-
47
-
48
- def init(settings: Settings):
15
+ def init(settings: setting.Settings):
49
16
  """Initialize the cocoindex library."""
50
17
  _engine.init(dump_engine_object(settings))
51
18
 
52
- @dataclass
53
- class ServerSettings:
54
- """Settings for the cocoindex server."""
55
-
56
- # The address to bind the server to.
57
- address: str = "127.0.0.1:8080"
58
-
59
- # The origin of the client (e.g. CocoInsight UI) to allow CORS from.
60
- cors_origin: str | None = None
61
-
62
- @classmethod
63
- def from_env(cls) -> Self:
64
- """Load settings from environment variables."""
65
-
66
- kwargs: dict[str, str] = dict()
67
- _load_field(kwargs, "address", "COCOINDEX_SERVER_ADDRESS")
68
- _load_field(kwargs, "cors_origin", "COCOINDEX_SERVER_CORS_ORIGIN")
69
-
70
- return cls(**kwargs)
71
-
72
19
 
73
- def start_server(settings: ServerSettings):
20
+ def start_server(settings: setting.ServerSettings):
74
21
  """Start the cocoindex server."""
75
22
  flow.ensure_all_flows_built()
76
23
  query.ensure_all_handlers_built()
@@ -81,7 +28,7 @@ def stop():
81
28
  _engine.stop()
82
29
 
83
30
  def main_fn(
84
- settings: Settings | None = None,
31
+ settings: setting.Settings | None = None,
85
32
  cocoindex_cmd: str = 'cocoindex',
86
33
  ) -> Callable[[Callable], Callable]:
87
34
  """
@@ -92,7 +39,7 @@ def main_fn(
92
39
  """
93
40
 
94
41
  def _pre_init() -> None:
95
- effective_settings = settings or Settings.from_env()
42
+ effective_settings = settings or setting.Settings.from_env()
96
43
  init(effective_settings)
97
44
 
98
45
  def _should_run_cli() -> bool:
cocoindex/op.py CHANGED
@@ -5,10 +5,10 @@ import asyncio
5
5
  import dataclasses
6
6
  import inspect
7
7
 
8
- from typing import get_type_hints, Protocol, Any, Callable, Awaitable, dataclass_transform
8
+ from typing import Protocol, Any, Callable, Awaitable, dataclass_transform
9
9
  from enum import Enum
10
10
 
11
- from .typing import encode_enriched_type
11
+ from .typing import encode_enriched_type, resolve_forward_ref
12
12
  from .convert import encode_engine_value, make_engine_value_decoder
13
13
  from . import _engine
14
14
 
@@ -214,10 +214,11 @@ def executor_class(**args) -> Callable[[type], type]:
214
214
  """
215
215
  Decorate a class to provide an executor for an op.
216
216
  """
217
- type_hints = get_type_hints(cls)
217
+ # Use `__annotations__` instead of `get_type_hints`, to avoid resolving forward references.
218
+ type_hints = cls.__annotations__
218
219
  if 'spec' not in type_hints:
219
220
  raise TypeError("Expect a `spec` field with type hint")
220
- spec_cls = type_hints['spec']
221
+ spec_cls = resolve_forward_ref(type_hints['spec'])
221
222
  sig = inspect.signature(cls.__call__)
222
223
  return _register_op_factory(
223
224
  category=spec_cls._op_category,
cocoindex/setting.py ADDED
@@ -0,0 +1,77 @@
1
+ """
2
+ Data types for settings of the cocoindex library.
3
+ """
4
+ import os
5
+
6
+ from typing import Callable, Self, Any, overload
7
+ from dataclasses import dataclass
8
+
9
+
10
+ @dataclass
11
+ class DatabaseConnectionSpec:
12
+ """
13
+ Connection spec for relational database.
14
+ Used by both internal and target storage.
15
+ """
16
+ url: str
17
+ user: str | None = None
18
+ password: str | None = None
19
+
20
+ def _load_field(target: dict[str, Any], name: str, env_name: str, required: bool = False,
21
+ parse: Callable[[str], Any] | None = None):
22
+ value = os.getenv(env_name)
23
+ if value is None:
24
+ if required:
25
+ raise ValueError(f"{env_name} is not set")
26
+ else:
27
+ target[name] = value if parse is None else parse(value)
28
+
29
+ @dataclass
30
+ class Settings:
31
+ """Settings for the cocoindex library."""
32
+ database: DatabaseConnectionSpec
33
+
34
+ @classmethod
35
+ def from_env(cls) -> Self:
36
+ """Load settings from environment variables."""
37
+
38
+ db_kwargs: dict[str, str] = dict()
39
+ _load_field(db_kwargs, "url", "COCOINDEX_DATABASE_URL", required=True)
40
+ _load_field(db_kwargs, "user", "COCOINDEX_DATABASE_USER")
41
+ _load_field(db_kwargs, "password", "COCOINDEX_DATABASE_PASSWORD")
42
+ database = DatabaseConnectionSpec(**db_kwargs)
43
+ return cls(database=database)
44
+
45
+ @dataclass
46
+ class ServerSettings:
47
+ """Settings for the cocoindex server."""
48
+
49
+ # The address to bind the server to.
50
+ address: str = "127.0.0.1:8080"
51
+
52
+ # The origins of the clients (e.g. CocoInsight UI) to allow CORS from.
53
+ cors_origins: list[str] | None = None
54
+
55
+ @classmethod
56
+ def from_env(cls) -> Self:
57
+ """Load settings from environment variables."""
58
+ kwargs: dict[str, Any] = dict()
59
+ _load_field(kwargs, "address", "COCOINDEX_SERVER_ADDRESS")
60
+ _load_field(kwargs, "cors_origins", "COCOINDEX_SERVER_CORS_ORIGINS",
61
+ parse=ServerSettings.parse_cors_origins)
62
+ return cls(**kwargs)
63
+
64
+ @overload
65
+ @staticmethod
66
+ def parse_cors_origins(s: str) -> list[str]: ...
67
+
68
+ @overload
69
+ @staticmethod
70
+ def parse_cors_origins(s: str | None) -> list[str] | None: ...
71
+
72
+ @staticmethod
73
+ def parse_cors_origins(s):
74
+ """
75
+ Parse the CORS origins from a string.
76
+ """
77
+ return [o for e in s.split(",") if (o := e.strip()) != ""] if s is not None else None
cocoindex/storages.py CHANGED
@@ -5,11 +5,11 @@ from typing import Sequence
5
5
  from . import op
6
6
  from . import index
7
7
  from .auth_registry import AuthEntryReference
8
+ from .setting import DatabaseConnectionSpec
8
9
 
9
10
  class Postgres(op.StorageSpec):
10
11
  """Storage powered by Postgres and pgvector."""
11
-
12
- database: AuthEntryReference | None = None
12
+ database: AuthEntryReference[DatabaseConnectionSpec] | None = None
13
13
  table_name: str | None = None
14
14
 
15
15
  @dataclass
@@ -72,15 +72,14 @@ NodeReferenceMapping = NodeFromFields
72
72
 
73
73
  class Neo4j(op.StorageSpec):
74
74
  """Graph storage powered by Neo4j."""
75
-
76
- connection: AuthEntryReference
75
+ connection: AuthEntryReference[Neo4jConnection]
77
76
  mapping: Nodes | Relationships
78
77
 
79
78
  class Neo4jDeclaration(op.DeclarationSpec):
80
79
  """Declarations for Neo4j."""
81
80
 
82
81
  kind = "Neo4j"
83
- connection: AuthEntryReference
82
+ connection: AuthEntryReference[Neo4jConnection]
84
83
  nodes_label: str
85
84
  primary_key_fields: Sequence[str]
86
85
  vector_indexes: Sequence[index.VectorIndexDef] = ()
cocoindex/typing.py CHANGED
@@ -245,3 +245,8 @@ def encode_enriched_type(t) -> dict[str, Any] | None:
245
245
  return None
246
246
 
247
247
  return encode_enriched_type_info(analyze_type_info(t))
248
+
249
+ def resolve_forward_ref(t):
250
+ if t is str:
251
+ return eval(t) # pylint: disable=eval-used
252
+ return t
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cocoindex
3
- Version: 0.1.29
3
+ Version: 0.1.30
4
4
  Requires-Dist: sentence-transformers>=3.3.1
5
5
  Requires-Dist: click>=8.1.8
6
6
  Requires-Dist: rich>=14.0.0
@@ -1,24 +1,25 @@
1
- cocoindex-0.1.29.dist-info/METADATA,sha256=EaJe6Rk9ypW5dZ8kqAFrId-sl_hKfbBi0cSyyHyVqa0,8107
2
- cocoindex-0.1.29.dist-info/WHEEL,sha256=_czbP61TsBkf9T201RekHMHlqESnWn7yJwXBJC9P-w0,104
3
- cocoindex-0.1.29.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
4
- cocoindex/functions.py,sha256=clnpHCYSsjUnc8Spbc1-5sQedG-60fmibodv9LpHgqo,1647
1
+ cocoindex-0.1.30.dist-info/METADATA,sha256=DgYe-2f-9L1PE3WP9x8f0om8fr3-Zct3bi5vkGbD-q0,8107
2
+ cocoindex-0.1.30.dist-info/WHEEL,sha256=_czbP61TsBkf9T201RekHMHlqESnWn7yJwXBJC9P-w0,104
3
+ cocoindex-0.1.30.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
4
+ cocoindex/functions.py,sha256=F79dNmGE127LaU67kF5Oqtf_tIzebFQH7MkyceMX4-s,1830
5
5
  cocoindex/query.py,sha256=8_3Lb_EVjZtl2ZyJNZGX16LoKXEd-PL8OjY-zs9GQeA,3205
6
6
  cocoindex/index.py,sha256=LssEOuZi6AqhwKtZM3QFeQpa9T-0ELi8G5DsrYKECvc,534
7
- cocoindex/lib.py,sha256=c6D7NuuTJj20WgVhnp0QGyK18lKMUvoDCiFr3PFs71s,3871
8
- cocoindex/auth_registry.py,sha256=lZ2rD5_9aC_UpGk7t4TmSYal_rjN7eHgO4_sU7FR0Zw,620
7
+ cocoindex/lib.py,sha256=812GB8Z-2PyjG73Odvw5jtNBLnoeU9aOh9s2ZnETKa8,2329
8
+ cocoindex/auth_registry.py,sha256=NsALZ3SKsDG9cPdrlTlalIqUvgbgFOaFGAbWJNedtJE,692
9
9
  cocoindex/convert.py,sha256=mBUTa_Ag39_ut-yE_jc1wqS3zLjtOm6QKet-bqJ-RWc,5947
10
10
  cocoindex/tests/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
11
11
  cocoindex/tests/test_convert.py,sha256=WPRKp0jv_uSEM81RGWEAmsax-J-FtXt90mZ0yEnvGLs,11236
12
12
  cocoindex/__init__.py,sha256=8atBT1HjclUOeiXd7TSzZWaqOR4x_qr5epvCKB7Z7oY,661
13
13
  cocoindex/flow.py,sha256=5W0tuDy_oc54lEmELvQAujx9f_20CSynZCF5vuNlbYw,22919
14
14
  cocoindex/llm.py,sha256=_3rtahuKcqcEHPkFSwhXOSrekZyGxVApPoYtlU_chcA,348
15
+ cocoindex/setting.py,sha256=0bDaLsPPosJxkKqaPcs70MAm5kwZSsxNkKBKKeONDOw,2355
15
16
  cocoindex/runtime.py,sha256=jqRnWkkIlAhE04gi4y0Y5bzuq9FX4j0aVNU-nengLJk,980
16
- cocoindex/op.py,sha256=ICCKZw6peCFu-CtMeIEaz6vlBxrf5dZwgUs9R4ALYNU,10604
17
+ cocoindex/op.py,sha256=OGYRYl7gPa7X7iSU30iTrCzvqRBu7jQqfvN4vjG__dA,10730
17
18
  cocoindex/sources.py,sha256=wZFU8lwSXjyofJR-syySH9fTyPnBlAPJ6-1hQNX8fGA,936
18
19
  cocoindex/setup.py,sha256=W1HshwYk_K2aeLOVn_e62ZOXBO9yWsoUboRiH4SjF48,496
19
- cocoindex/cli.py,sha256=PU5xeP9rWiRP-6_05aikFcRZ5Pmhn39DvIvl31Se23M,7269
20
+ cocoindex/cli.py,sha256=57prYaE9iljBcEhV3EqyMMndPwHJsVApvKR9XhvFzLA,8387
20
21
  cocoindex/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
- cocoindex/typing.py,sha256=p3FEUnoQc6zmiG8YwO4T155sgZtyc_1AufiJe3bNol8,8458
22
- cocoindex/storages.py,sha256=JjWDKF4R-_3XJq0t8ejlb9JlUsh2Iv4nKokl7PsAPmA,2107
23
- cocoindex/_engine.cpython-313-darwin.so,sha256=AvCA3cfKCGMQYJ7WSI4PVNJg92QoBeZxfGNEDQMJjzs,59488992
24
- cocoindex-0.1.29.dist-info/RECORD,,
22
+ cocoindex/typing.py,sha256=BI2vPw4Iu4S3aznNJQrfM2LZU_weGYASTXF1W3ZWh_Y,8568
23
+ cocoindex/storages.py,sha256=MFMsfyOCYMggTWeWrOi82miqOXQmiUuqq828x5htBr0,2207
24
+ cocoindex/_engine.cpython-313-darwin.so,sha256=YoKeEx1gYyUhVBgCGVNaETvBHM-GrNsL3LKc8Hgx874,59488640
25
+ cocoindex-0.1.30.dist-info/RECORD,,