Skip to content

Openai

OpenAIDocumentEmbedder

Bases: DocumentEmbedder

Provides functionality to compute embeddings for documents using OpenAI's models.

This class extends ConnectionNode to create embeddings for documents using OpenAI's API.

Attributes:

Name Type Description
group Literal[EMBEDDERS]

The group the node belongs to.

name str

The name of the node.

connection OpenAI | None

The connection to the OpenAI API.

client OpenAIClient | None

The OpenAI client instance.

model str

The model name to use for embedding.

dimensions int | None

The number of dimensions for the output embeddings.

document_embedder OpenAIDocumentEmbedderComponent

The component for document embedding.

Parameters:

Name Type Description Default
connection Optional[OpenAI]

The connection to the OpenAI API. A new connection is created if none is provided.

required
model str

The model name to use for embedding. Defaults to 'text-embedding-3-small'.

required
dimensions Optional[int]

The number of dimensions for the output embeddings. Supported only by 'text-embedding-3' and later models. Defaults to None.

required
Source code in dynamiq/nodes/embedders/openai.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class OpenAIDocumentEmbedder(DocumentEmbedder):
    """
    Provides functionality to compute embeddings for documents using OpenAI's models.

    This class extends ConnectionNode to create embeddings for documents using OpenAI's API.

    Attributes:
        group (Literal[NodeGroup.EMBEDDERS]): The group the node belongs to.
        name (str): The name of the node.
        connection (OpenAIConnection | None): The connection to the OpenAI API.
        client (OpenAIClient | None): The OpenAI client instance.
        model (str): The model name to use for embedding.
        dimensions (int | None): The number of dimensions for the output embeddings.
        document_embedder (OpenAIDocumentEmbedderComponent): The component for document embedding.

    Args:
        connection (Optional[OpenAIConnection]): The connection to the OpenAI API. A new connection
            is created if none is provided.
        model (str): The model name to use for embedding. Defaults to 'text-embedding-3-small'.
        dimensions (Optional[int]): The number of dimensions for the output embeddings. Supported
            only by 'text-embedding-3' and later models. Defaults to None.
    """

    name: str = "OpenAIDocumentEmbedder"
    connection: OpenAIConnection | None = None
    model: str = "text-embedding-3-small"
    dimensions: int | None = None
    document_embedder: OpenAIEmbedderComponent | None = None

    def __init__(self, **kwargs):
        """
        Initializes the OpenAIDocumentEmbedder.

        If neither client nor connection is provided in kwargs, a new OpenAIConnection is created.

        Args:
            **kwargs: Arbitrary keyword arguments.
        """
        if kwargs.get("client") is None and kwargs.get("connection") is None:
            kwargs["connection"] = OpenAIConnection()
        super().__init__(**kwargs)

    def init_components(self, connection_manager: ConnectionManager | None = None):
        """
        Initializes the components of the OpenAIDocumentEmbedder.

        This method sets up the document_embedder component if it hasn't been initialized yet.

        Args:
            connection_manager (ConnectionManager): The connection manager to use. Defaults to a new
                ConnectionManager instance.
        """
        connection_manager = connection_manager or ConnectionManager()
        super().init_components(connection_manager)
        if self.document_embedder is None:
            self.document_embedder = OpenAIEmbedderComponent(
                connection=self.connection,
                model=self.model,
                dimensions=self.dimensions,
                client=self.client,
            )

__init__(**kwargs)

Initializes the OpenAIDocumentEmbedder.

If neither client nor connection is provided in kwargs, a new OpenAIConnection is created.

Parameters:

Name Type Description Default
**kwargs

Arbitrary keyword arguments.

{}
Source code in dynamiq/nodes/embedders/openai.py
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(self, **kwargs):
    """
    Initializes the OpenAIDocumentEmbedder.

    If neither client nor connection is provided in kwargs, a new OpenAIConnection is created.

    Args:
        **kwargs: Arbitrary keyword arguments.
    """
    if kwargs.get("client") is None and kwargs.get("connection") is None:
        kwargs["connection"] = OpenAIConnection()
    super().__init__(**kwargs)

init_components(connection_manager=None)

Initializes the components of the OpenAIDocumentEmbedder.

This method sets up the document_embedder component if it hasn't been initialized yet.

Parameters:

Name Type Description Default
connection_manager ConnectionManager

The connection manager to use. Defaults to a new ConnectionManager instance.

None
Source code in dynamiq/nodes/embedders/openai.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def init_components(self, connection_manager: ConnectionManager | None = None):
    """
    Initializes the components of the OpenAIDocumentEmbedder.

    This method sets up the document_embedder component if it hasn't been initialized yet.

    Args:
        connection_manager (ConnectionManager): The connection manager to use. Defaults to a new
            ConnectionManager instance.
    """
    connection_manager = connection_manager or ConnectionManager()
    super().init_components(connection_manager)
    if self.document_embedder is None:
        self.document_embedder = OpenAIEmbedderComponent(
            connection=self.connection,
            model=self.model,
            dimensions=self.dimensions,
            client=self.client,
        )

OpenAITextEmbedder

Bases: TextEmbedder

A component designed to embed strings using specified OpenAI models.

This class extends ConnectionNode to provide text embedding functionality using OpenAI's API.

Parameters:

Name Type Description Default
connection Optional[OpenAI]

An existing connection to OpenAI's API. If not provided, a new connection will be established using environment variables.

required
model str

The identifier of the OpenAI model for text embeddings. Defaults to 'text-embedding-3-small'.

required
dimensions Optional[int]

Desired dimensionality of output embeddings. Defaults to None, using the model's default output dimensionality.

required

Attributes:

Name Type Description
group Literal[EMBEDDERS]

The group the node belongs to.

name str

The name of the node.

connection OpenAI | None

The connection to OpenAI's API.

client OpenAIClient | None

The OpenAI client instance.

model str

The OpenAI model identifier for text embeddings.

dimensions int | None

The desired dimensionality of output embeddings.

text_embedder OpenAITextEmbedderComponent

The component for text embedding.

Notes

The dimensions parameter is model-dependent and may not be supported by all models.

Source code in dynamiq/nodes/embedders/openai.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class OpenAITextEmbedder(TextEmbedder):
    """
    A component designed to embed strings using specified OpenAI models.

    This class extends ConnectionNode to provide text embedding functionality using OpenAI's API.

    Args:
        connection (Optional[OpenAIConnection]): An existing connection to OpenAI's API. If not
            provided, a new connection will be established using environment variables.
        model (str): The identifier of the OpenAI model for text embeddings. Defaults to
            'text-embedding-3-small'.
        dimensions (Optional[int]): Desired dimensionality of output embeddings. Defaults to None,
            using the model's default output dimensionality.

    Attributes:
        group (Literal[NodeGroup.EMBEDDERS]): The group the node belongs to.
        name (str): The name of the node.
        connection (OpenAIConnection | None): The connection to OpenAI's API.
        client (OpenAIClient | None): The OpenAI client instance.
        model (str): The OpenAI model identifier for text embeddings.
        dimensions (int | None): The desired dimensionality of output embeddings.
        text_embedder (OpenAITextEmbedderComponent): The component for text embedding.

    Notes:
        The `dimensions` parameter is model-dependent and may not be supported by all models.
    """

    name: str = "OpenAITextEmbedder"
    connection: OpenAIConnection | None = None
    model: str = "text-embedding-3-small"
    dimensions: int | None = None
    text_embedder: OpenAIEmbedderComponent = None

    def __init__(self, **kwargs):
        """Initialize the OpenAITextEmbedder.

        If neither client nor connection is provided in kwargs, a new OpenAIConnection is created.

        Args:
            **kwargs: Keyword arguments to initialize the node.
        """
        if kwargs.get("client") is None and kwargs.get("connection") is None:
            kwargs["connection"] = OpenAIConnection()
        super().__init__(**kwargs)

    def init_components(self, connection_manager: ConnectionManager | None = None):
        """
        Initialize the components of the OpenAITextEmbedder.

        This method sets up the text_embedder component if it hasn't been initialized yet.

        Args:
            connection_manager (ConnectionManager): The connection manager to use. Defaults to a new
                ConnectionManager instance.
        """
        connection_manager = connection_manager or ConnectionManager()
        super().init_components(connection_manager)
        if self.text_embedder is None:
            self.text_embedder = OpenAIEmbedderComponent(
                connection=self.connection,
                model=self.model,
                dimensions=self.dimensions,
                client=self.client,
            )

__init__(**kwargs)

Initialize the OpenAITextEmbedder.

If neither client nor connection is provided in kwargs, a new OpenAIConnection is created.

Parameters:

Name Type Description Default
**kwargs

Keyword arguments to initialize the node.

{}
Source code in dynamiq/nodes/embedders/openai.py
103
104
105
106
107
108
109
110
111
112
113
def __init__(self, **kwargs):
    """Initialize the OpenAITextEmbedder.

    If neither client nor connection is provided in kwargs, a new OpenAIConnection is created.

    Args:
        **kwargs: Keyword arguments to initialize the node.
    """
    if kwargs.get("client") is None and kwargs.get("connection") is None:
        kwargs["connection"] = OpenAIConnection()
    super().__init__(**kwargs)

init_components(connection_manager=None)

Initialize the components of the OpenAITextEmbedder.

This method sets up the text_embedder component if it hasn't been initialized yet.

Parameters:

Name Type Description Default
connection_manager ConnectionManager

The connection manager to use. Defaults to a new ConnectionManager instance.

None
Source code in dynamiq/nodes/embedders/openai.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def init_components(self, connection_manager: ConnectionManager | None = None):
    """
    Initialize the components of the OpenAITextEmbedder.

    This method sets up the text_embedder component if it hasn't been initialized yet.

    Args:
        connection_manager (ConnectionManager): The connection manager to use. Defaults to a new
            ConnectionManager instance.
    """
    connection_manager = connection_manager or ConnectionManager()
    super().init_components(connection_manager)
    if self.text_embedder is None:
        self.text_embedder = OpenAIEmbedderComponent(
            connection=self.connection,
            model=self.model,
            dimensions=self.dimensions,
            client=self.client,
        )