Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(llama-index): add embeddings #316

Merged
merged 2 commits into from
Feb 8, 2024
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
34 changes: 34 additions & 0 deletions examples/extensions/llama_index/llama_index_embedding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
LlamaIndex Embeddings
"""
from dotenv import load_dotenv

from genai import Client, Credentials
from genai.extensions.llama_index import IBMGenAILlamaIndexEmbedding
from genai.schema import TextEmbeddingParameters

# make sure you have a .env file under genai root with
# GENAI_KEY=<your-genai-key>
# GENAI_API=<genai-api-endpoint> (optional) DEFAULT_API = "https://bam-api.res.ibm.com"
load_dotenv()


def heading(text: str) -> str:
"""Helper function for centering text."""
return "\n" + f" {text} ".center(80, "=") + "\n"


print(heading("LlamaIndex Embeddings"))

client = Client(credentials=Credentials.from_env())
embeddings = IBMGenAILlamaIndexEmbedding(
client=client,
model_id="sentence-transformers/all-minilm-l6-v2",
parameters=TextEmbeddingParameters(truncate_input_tokens=True),
)

query_embedding = embeddings.get_query_embedding("Hello world!")
print(query_embedding)

documents_embedding = embeddings.get_agg_embedding_from_queries(["First document", "Second document"])
print(documents_embedding)
3 changes: 2 additions & 1 deletion src/genai/extensions/llama_index/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Extension for LLamaIndex library"""

from genai.extensions.llama_index.embeddings import IBMGenAILlamaIndexEmbedding
from genai.extensions.llama_index.llm import IBMGenAILlamaIndex

__all__ = ["IBMGenAILlamaIndex"]
__all__ = ["IBMGenAILlamaIndex", "IBMGenAILlamaIndexEmbedding"]
53 changes: 53 additions & 0 deletions src/genai/extensions/llama_index/embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import asyncio
from typing import Optional

from genai._types import ModelLike
from genai.client import Client
from genai.schema import TextEmbeddingParameters
from genai.text.embedding.embedding_service import CreateExecutionOptions

try:
from llama_index.embeddings.base import BaseEmbedding, Embedding
except ImportError:
raise ImportError("Could not import llamaindex: Please install ibm-generative-ai[llama-index] extension.") # noqa: B904


class IBMGenAILlamaIndexEmbedding(BaseEmbedding):
client: Client
model_id: str
parameters: Optional[ModelLike[TextEmbeddingParameters]] = None
execution_options: Optional[ModelLike[CreateExecutionOptions]] = None

@classmethod
def class_name(cls) -> str:
return "IBMGenAIEmbedding"

def _get_query_embedding(self, query: str) -> Embedding:
response = self._get_embeddings([query])
return response[0]

def _get_text_embedding(self, text: str) -> Embedding:
response = self._get_embeddings([text])
return response[0]

def _get_text_embeddings(self, texts: list[str]) -> list[Embedding]:
response = self._get_embeddings(texts)
return response

async def _aget_query_embedding(self, query: str) -> Embedding:
return await asyncio.get_running_loop().run_in_executor(None, self._get_query_embedding, query)

async def _aget_text_embedding(self, text: str) -> Embedding:
return await asyncio.get_running_loop().run_in_executor(None, self._get_text_embedding, text)

async def _aget_text_embeddings(self, texts: list[str]) -> list[Embedding]:
return await asyncio.get_running_loop().run_in_executor(None, self._get_text_embeddings, texts)

def _get_embeddings(self, texts: list[str]) -> list[Embedding]:
embeddings: list[list[float]] = []
for response in self.client.text.embedding.create(
model_id=self.model_id, inputs=texts, parameters=self.parameters, execution_options=self.execution_options
):
embeddings.extend(response.results)

return embeddings
David-Kristek marked this conversation as resolved.
Show resolved Hide resolved
Loading