Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions python/cocoindex/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import click
import datetime
from rich.console import Console
Expand All @@ -21,7 +20,7 @@ def ls(show_all: bool):
"""
List all flows.
"""
current_flow_names = [fl.name for fl in flow.flows()]
current_flow_names = flow.flow_names()
persisted_flow_names = flow_names_with_setup()
remaining_persisted_flow_names = set(persisted_flow_names)

Expand Down
10 changes: 7 additions & 3 deletions python/cocoindex/functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""All builtin functions."""
from typing import Annotated, Any
from typing import Annotated, Any, TYPE_CHECKING

import sentence_transformers
from .typing import Float32, Vector, TypeAttr
from . import op, llm

# Libraries that are heavy to import. Lazily import them later.
if TYPE_CHECKING:
import sentence_transformers

class ParseJson(op.FunctionSpec):
"""Parse a text into a JSON object."""

Expand Down Expand Up @@ -35,9 +38,10 @@ class SentenceTransformerEmbedExecutor:
"""Executor for SentenceTransformerEmbed."""

spec: SentenceTransformerEmbed
_model: sentence_transformers.SentenceTransformer
_model: "sentence_transformers.SentenceTransformer"

def analyze(self, text):
import sentence_transformers # pylint: disable=import-outside-toplevel
args = self.spec.args or {}
self._model = sentence_transformers.SentenceTransformer(self.spec.model, **args)
dim = self._model.get_sentence_embedding_dimension()
Expand Down
9 changes: 5 additions & 4 deletions python/cocoindex/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import dataclasses
import inspect

from typing import get_type_hints, Protocol, Any, Callable, Awaitable, dataclass_transform
from typing import Protocol, Any, Callable, Awaitable, dataclass_transform
from enum import Enum

from .typing import encode_enriched_type
from .typing import encode_enriched_type, resolve_forward_ref
from .convert import encode_engine_value, make_engine_value_decoder
from . import _engine

Expand Down Expand Up @@ -214,10 +214,11 @@ def _inner(cls: type[Executor]) -> type:
"""
Decorate a class to provide an executor for an op.
"""
type_hints = get_type_hints(cls)
# Use `__annotations__` instead of `get_type_hints`, to avoid resolving forward references.
type_hints = cls.__annotations__
if 'spec' not in type_hints:
raise TypeError("Expect a `spec` field with type hint")
spec_cls = type_hints['spec']
spec_cls = resolve_forward_ref(type_hints['spec'])
sig = inspect.signature(cls.__call__)
return _register_op_factory(
category=spec_cls._op_category,
Expand Down
5 changes: 5 additions & 0 deletions python/cocoindex/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ def encode_enriched_type(t) -> dict[str, Any] | None:
return None

return encode_enriched_type_info(analyze_type_info(t))

def resolve_forward_ref(t):
if t is str:
return eval(t) # pylint: disable=eval-used
return t