ollama: init package (#23615)

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Isaac Francisco
2024-07-19 17:43:29 -07:00
committed by GitHub
parent f4ee3c8a22
commit 838464de25
28 changed files with 8787 additions and 571 deletions

View 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]