Open jonmach opened 9 months ago
Problem resolved by adding:
_model: INSTRUCTOR = PrivateAttr()
_instruction: str = PrivateAttr()
to the InstructorEmbeddings class
I also got the error:
ValueError: "InstructorEmbeddings" object has no field "_model"
This is my fix:
class InstructorEmbeddings(BaseEmbedding):
_instruction: str = "Represent the Computer Science text for retrieval:"
def __init__(
self,
instructor_model_name: str = "hkunlp/instructor-large",
**kwargs: Any,
) -> None:
_model: INSTRUCTOR = INSTRUCTOR(instructor_model_name)
super().__init__(**kwargs)
def _get_query_embedding(self, query: str) -> List[float]:
embeddings = model.encode([[self._instruction, query]])
return embeddings[0].tolist()
async def _aget_query_embedding(self, query: str) -> List[float]:
return self._get_query_embedding(query)
def _get_text_embedding(self, text: str) -> List[float]:
embeddings = model.encode([[self._instruction, text]])
return embeddings[0].tolist()
def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
embeddings = model.encode([[self._instruction, text] for text in texts])
return embeddings.tolist()
My results are the same as the video:
embed = instructor_embeddings.get_text_embedding("How do I create a vector index?")
print(len(embed))
print(embed[:10])
768
[0.003987060859799385, 0.012122981250286102, 0.002690523862838745, 0.01581709273159504, -0.005555964540690184, 0.03673827275633812, 0.010727009736001492, 0.00666137645021081, -0.0392913892865181, 0.013146855868399143]
Current working solution as of Jul 23rd 2024.
from typing import Any, List
from InstructorEmbedding import INSTRUCTOR
from llama_index.core.embeddings import BaseEmbedding
**from pydantic import Extra**
class InstructorEmbeddings(BaseEmbedding):
class Config:
extra = Extra.allow
_instruction: str = "Represent the Computer Science text for retrieval:"
def __init__(
self,
instructor_model_name: str = "hkunlp/instructor-large",
**kwargs: Any
) -> None:
super().__init__(**kwargs)
self._model: INSTRUCTOR = INSTRUCTOR(instructor_model_name)
def _get_query_embedding(self, query: str) -> List[float]:
embeddings = self._model.encode([[self._instruction, query]])
return embeddings[0].tolist()
async def _aget_query_embedding(self, query: str) -> List[float]:
return self._get_query_embedding(query)
def _get_text_embedding(self, text: str) -> List[float]:
embeddings = self._model.encode([[self._instruction, text]])
return embeddings[0].tolist()
def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
embeddings = self._model.encode([[self._instruction, text] for text in texts])
return embeddings.tolist()
BaseEmbedding class has pydantic validation, meaning that no extra fields can be added to InstructorEmbeddings child class out of the box.
Add following code to allow extra fields to be defined.
class Config: extra = Extra.allow
def __init__(
self,
instructor_model_name: str = "hkunlp/instructor-large",
**kwargs: Any
) -> None:
super().__init__(**kwargs) # placement of this line is important
self._model: INSTRUCTOR = INSTRUCTOR(instructor_model_name)
I"m working through the llama_docs_bot files and there is an issue with the InstructorEmbeddings class that relies on BaseEmbedding:
Running the following:
I get the following error:
This is a list of my installed modules with versions etc.