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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Next

### Fixed
- Corrected initialization to allow specifying the embedding model name.
- Removed sentence_transformers from embeddings/__init__.py to avoid ImportError when the package is not installed.

## 0.3.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/source/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ The `OpenAIEmbedder` was illustrated previously. Here is how to use the `Sentenc

.. code:: python

from neo4j_genai.embeddings import SentenceTransformerEmbeddings
from neo4j_genai.embeddings.sentence_transformers import SentenceTransformerEmbeddings

embedder = SentenceTransformerEmbeddings(model="all-MiniLM-L6-v2") # Note: this is the default model

Expand Down
19 changes: 14 additions & 5 deletions src/neo4j_genai/embeddings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from .sentence_transformers import SentenceTransformerEmbeddings

__all__ = [
"SentenceTransformerEmbeddings",
]
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
28 changes: 22 additions & 6 deletions src/neo4j_genai/embeddings/openai.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import Any
Expand All @@ -6,7 +21,7 @@


class OpenAIEmbeddings(Embedder):
def __init__(self, *args: Any, **kwargs: Any) -> None:
def __init__(self, model: str = "text-embedding-ada-002") -> None:
try:
import openai
except ImportError:
Expand All @@ -15,10 +30,11 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
"Please install it with `pip install openai`."
)

self.model = openai.OpenAI(*args, **kwargs)
self.openai_model = openai.OpenAI()
self.model = model

def embed_query(
self, text: str, model: str = "text-embedding-ada-002", **kwargs: Any
) -> list[float]:
response = self.model.embeddings.create(input=text, model=model, **kwargs)
def embed_query(self, text: str, **kwargs: Any) -> list[float]:
response = self.openai_model.embeddings.create(
input=text, model=self.model, **kwargs
)
return response.data[0].embedding
15 changes: 15 additions & 0 deletions src/neo4j_genai/embeddings/sentence_transformers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any

import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/embeddings/test_sentence_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest
from neo4j_genai.embedder import Embedder
from neo4j_genai.embeddings import SentenceTransformerEmbeddings
from neo4j_genai.embeddings.sentence_transformers import SentenceTransformerEmbeddings


@patch("sentence_transformers.SentenceTransformer")
Expand Down