mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
ollama: init package (#23615)
Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
51
libs/partners/ollama/langchain_ollama/embeddings.py
Normal file
51
libs/partners/ollama/langchain_ollama/embeddings.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from typing import List
|
||||
|
||||
import ollama
|
||||
from langchain_core.embeddings import Embeddings
|
||||
from langchain_core.pydantic_v1 import BaseModel, Extra
|
||||
from ollama import AsyncClient
|
||||
|
||||
|
||||
class OllamaEmbeddings(BaseModel, Embeddings):
|
||||
"""OllamaEmbeddings embedding model.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_ollama import OllamaEmbeddings
|
||||
|
||||
model = OllamaEmbeddings(model="llama3")
|
||||
embedder.embed_query("what is the place that jonathan worked at?")
|
||||
"""
|
||||
|
||||
model: str
|
||||
"""Model name to use."""
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
|
||||
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Embed search docs."""
|
||||
embedded_docs = []
|
||||
for doc in texts:
|
||||
embedded_docs.append(list(ollama.embeddings(self.model, doc)["embedding"]))
|
||||
return embedded_docs
|
||||
|
||||
def embed_query(self, text: str) -> List[float]:
|
||||
"""Embed query text."""
|
||||
return self.embed_documents([text])[0]
|
||||
|
||||
async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Embed search docs."""
|
||||
embedded_docs = []
|
||||
for doc in texts:
|
||||
embedded_docs.append(
|
||||
list((await AsyncClient().embeddings(self.model, doc))["embedding"])
|
||||
)
|
||||
return embedded_docs
|
||||
|
||||
async def aembed_query(self, text: str) -> List[float]:
|
||||
"""Embed query text."""
|
||||
return (await self.aembed_documents([text]))[0]
|
||||
Reference in New Issue
Block a user