Ollama embeddings tested and Docs ready (#1384)
This commit is contained in:
@@ -15,6 +15,7 @@ Embedchain supports several embedding models from the following providers:
|
|||||||
<Card title="Vertex AI" href="#vertex-ai"></Card>
|
<Card title="Vertex AI" href="#vertex-ai"></Card>
|
||||||
<Card title="NVIDIA AI" href="#nvidia-ai"></Card>
|
<Card title="NVIDIA AI" href="#nvidia-ai"></Card>
|
||||||
<Card title="Cohere" href="#cohere"></Card>
|
<Card title="Cohere" href="#cohere"></Card>
|
||||||
|
<Card title="Ollama" href="#ollama"></Card>
|
||||||
</CardGroup>
|
</CardGroup>
|
||||||
|
|
||||||
## OpenAI
|
## OpenAI
|
||||||
@@ -357,4 +358,31 @@ embedder:
|
|||||||
vector_dimension: 768
|
vector_dimension: 768
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</CodeGroup>
|
||||||
|
|
||||||
|
## Ollama
|
||||||
|
|
||||||
|
Ollama enables the use of embedding models, allowing you to generate high-quality embeddings directly on your local machine. Make sure to install [Ollama](https://ollama.com/download) and keep it running before using the embedding model.
|
||||||
|
|
||||||
|
You can find the list of models at [Ollama Embedding Models](https://ollama.com/blog/embedding-models).
|
||||||
|
|
||||||
|
Below is an example of how to use embedding model Ollama:
|
||||||
|
|
||||||
|
<CodeGroup>
|
||||||
|
|
||||||
|
```python main.py
|
||||||
|
import os
|
||||||
|
from embedchain import App
|
||||||
|
|
||||||
|
# load embedding model configuration from config.yaml file
|
||||||
|
app = App.from_config(config_path="config.yaml")
|
||||||
|
```
|
||||||
|
|
||||||
|
```yaml config.yaml
|
||||||
|
embedder:
|
||||||
|
provider: ollama
|
||||||
|
config:
|
||||||
|
model: 'all-minilm:latest'
|
||||||
|
```
|
||||||
|
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
@@ -1,16 +1,28 @@
|
|||||||
|
import logging
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
try:
|
||||||
|
import ollama
|
||||||
|
except ImportError:
|
||||||
|
raise ImportError("Ollama Embedder requires extra dependencies. Install with `pip install ollama`") from None
|
||||||
|
|
||||||
from langchain_community.embeddings import OllamaEmbeddings
|
from langchain_community.embeddings import OllamaEmbeddings
|
||||||
|
|
||||||
from embedchain.config import OllamaEmbedderConfig
|
from embedchain.config import OllamaEmbedderConfig
|
||||||
from embedchain.embedder.base import BaseEmbedder
|
from embedchain.embedder.base import BaseEmbedder
|
||||||
from embedchain.models import VectorDimensions
|
from embedchain.models import VectorDimensions
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class OllamaEmbedder(BaseEmbedder):
|
class OllamaEmbedder(BaseEmbedder):
|
||||||
def __init__(self, config: Optional[OllamaEmbedderConfig] = None):
|
def __init__(self, config: Optional[OllamaEmbedderConfig] = None):
|
||||||
super().__init__(config=config)
|
super().__init__(config=config)
|
||||||
|
|
||||||
|
local_models = ollama.list()["models"]
|
||||||
|
if not any(model.get("name") == self.config.model for model in local_models):
|
||||||
|
logger.info(f"Pulling {self.config.model} from Ollama!")
|
||||||
|
ollama.pull(self.config.model)
|
||||||
embeddings = OllamaEmbeddings(model=self.config.model, base_url=self.config.base_url)
|
embeddings = OllamaEmbeddings(model=self.config.model, base_url=self.config.base_url)
|
||||||
embedding_fn = BaseEmbedder._langchain_default_concept(embeddings)
|
embedding_fn = BaseEmbedder._langchain_default_concept(embeddings)
|
||||||
self.set_embedding_fn(embedding_fn=embedding_fn)
|
self.set_embedding_fn(embedding_fn=embedding_fn)
|
||||||
|
|||||||
Reference in New Issue
Block a user