-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add get_embedding_dims util to get the embedder model dimensions #140
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
Merged
AnkushMalaker
merged 3 commits into
SimpleOpenSoftware:main
from
RKRitik:fix/ollama-embedding-dims
Oct 24, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Refine exception handling and improve code quality.
The dynamic dimension detection is a good approach, but the implementation has several issues:
=in variable assignment..get()for both keys or direct access for both.Exceptionmasks specific failures. Uselogging.exception()instead oflogging.error()to preserve stack traces."Embedding model is unrecognized setting default value {e}".Apply this diff to address these issues:
def get_embedding_dims(llm_config: Dict[str, Any]) -> int: """ Query the embedding endpoint and return the embedding vector length. Works for OpenAI and OpenAI-compatible endpoints (e.g., Ollama). """ - embedding_model=llm_config['embedding_model'] + embedding_model = llm_config['embedding_model'] try: import langfuse.openai as openai - client = openai.OpenAI(api_key=llm_config.get('api_key'), base_url=llm_config['base_url']) + client = openai.OpenAI( + api_key=llm_config.get('api_key'), + base_url=llm_config.get('base_url') + ) response = client.embeddings.create( model=embedding_model, input="hello world" ) embedding = response.data[0].embedding if not embedding or not isinstance(embedding, list): return 1536 return len(embedding) - except Exception as e: + except (ImportError, KeyError, AttributeError, IndexError) as e: embedding_dims = 1536 # default - memory_logger.error(f"Failed to get embedding dimensions: {e}") + memory_logger.exception(f"Failed to get embedding dimensions for model '{embedding_model}'") if embedding_model == "text-embedding-3-small": embedding_dims = 1536 elif embedding_model == "text-embedding-3-large": embedding_dims = 3072 elif embedding_model == "text-embedding-ada-002": embedding_dims = 1536 elif embedding_model == "nomic-embed-text:latest": embedding_dims = 768 else: # Default for OpenAI embedding models - memory_logger.info(f"Embedding model is unrecognized setting default value {e}") + memory_logger.info(f"Unrecognized embedding model '{embedding_model}', using default dimension {embedding_dims}") embedding_dims = 1536 return embedding_dimsRationale:
logging.exception()automatically includes the stack trace.As per static analysis hints.
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.14.1)
263-263: Do not catch blind exception:
Exception(BLE001)
265-265: Use
logging.exceptioninstead oflogging.errorReplace with
exception(TRY400)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai Please verify the changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RKRitik, excellent work! 🎉 All the suggested changes have been successfully applied:
✅ Fixed spacing:
embedding_model = llm_config.get('embedding_model')✅ Consistent dictionary access: Both
api_keyandbase_urlnow use.get()method✅ Specific exception handling: Changed from broad
Exceptionto specific exceptions - you even addedTypeErrorandValueErrorwhich is great!✅ Better logging: Now uses
memory_logger.exception()which includes stack traces✅ Improved log message: Clear, informative message for unrecognized models
The code quality improvements are all in place and the implementation looks solid. The function will now provide better debugging information and handle errors more gracefully.
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!