Last active
August 1, 2025 10:28
-
-
Save estebanfeldman/56478faa6855a806107efec5bd40b335 to your computer and use it in GitHub Desktop.
Embedding Utility class to use with a Local LM Studio embedding model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from langchain.embeddings.base import Embeddings | |
| class LMStudioEmbeddings(Embeddings): | |
| def __init__( | |
| self, | |
| endpoint, | |
| model, | |
| ): | |
| self.endpoint = endpoint | |
| self.model = model | |
| def embed_documents(self, texts): | |
| # Convert LangChain Documents to plain strings | |
| string_texts = [ | |
| text.page_content if hasattr(text, "page_content") else text | |
| for text in texts | |
| ] | |
| return [self._embed(text) for text in string_texts] | |
| def embed_query(self, text): | |
| return self._embed(text) | |
| def _embed(self, text): | |
| response = requests.post( | |
| self.endpoint, | |
| headers={"Content-Type": "application/json"}, | |
| json={"model": self.model, "input": text}, | |
| ) | |
| response.raise_for_status() | |
| return response.json()["data"][0]["embedding"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment