-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OllamaLLM and OllamaEmbeddings classes (#231)
* Add OllamaLLM and OllamaEmbeddings classes using the ollama python client * Try removing import * :( * Add tests + reformat import in ollama embeddings for consistency with all other imports * Fix after merge
- Loading branch information
Showing
16 changed files
with
293 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
from neo4j_graphrag.llm import LLMResponse, OpenAILLM | ||
"""This example demonstrate how to invoke an LLM using a local model | ||
served by Ollama. | ||
""" | ||
|
||
# not used but needs to be provided | ||
api_key = "ollama" | ||
from neo4j_graphrag.llm import LLMResponse, OllamaLLM | ||
|
||
llm = OpenAILLM( | ||
base_url="http://localhost:11434/v1", | ||
llm = OllamaLLM( | ||
model_name="<model_name>", | ||
api_key=api_key, | ||
) | ||
res: LLMResponse = llm.invoke("What is the additive color model?") | ||
print(res.content) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 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 | ||
|
||
from neo4j_graphrag.embeddings.base import Embedder | ||
from neo4j_graphrag.exceptions import EmbeddingsGenerationError | ||
|
||
|
||
class OllamaEmbeddings(Embedder): | ||
""" | ||
Ollama embeddings class. | ||
This class uses the ollama Python client to generate vector embeddings for text data. | ||
Args: | ||
model (str): The name of the Mistral AI text embedding model to use. Defaults to "mistral-embed". | ||
""" | ||
|
||
def __init__(self, model: str, **kwargs: Any) -> None: | ||
try: | ||
import ollama | ||
except ImportError: | ||
raise ImportError( | ||
"Could not import ollama python client. " | ||
"Please install it with `pip install ollama`." | ||
) | ||
self.model = model | ||
self.client = ollama.Client(**kwargs) | ||
|
||
def embed_query(self, text: str, **kwargs: Any) -> list[float]: | ||
""" | ||
Generate embeddings for a given query using an Ollama text embedding model. | ||
Args: | ||
text (str): The text to generate an embedding for. | ||
**kwargs (Any): Additional keyword arguments to pass to the Ollama client. | ||
""" | ||
embeddings_response = self.client.embed( | ||
model=self.model, | ||
input=text, | ||
**kwargs, | ||
) | ||
|
||
if embeddings_response is None or embeddings_response.embeddings is None: | ||
raise EmbeddingsGenerationError("Failed to retrieve embeddings.") | ||
|
||
embedding = embeddings_response.embeddings | ||
if not isinstance(embedding, list): | ||
raise EmbeddingsGenerationError("Embedding is not a list of floats.") | ||
|
||
return embedding |
Oops, something went wrong.