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

Bug: when do the normalization of embeddings. #412

Closed
Bannerli opened this issue Jul 7, 2024 · 4 comments
Closed

Bug: when do the normalization of embeddings. #412

Bannerli opened this issue Jul 7, 2024 · 4 comments

Comments

@Bannerli
Copy link

Bannerli commented Jul 7, 2024

I have found an error in the source code, which targets to do the normalization of embeddings but encountering the ZeronDivision.

INFO: Reading settings from settings.yaml
Error embedding chunk {'OpenAIEmbedding': 'Internal Server Error'}
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/main.py", line 75, in
run_local_search(
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/cli.py", line 154, in run_local_search
result = search_engine.search(query=query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/structured_search/local_search/search.py", line 118, in search
context_text, context_records = self.context_builder.build_context(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/structured_search/local_search/mixed_context.py", line 139, in build_context
selected_entities = map_query_to_entities(
^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/context_builder/entity_extraction.py", line 55, in map_query_to_entities
search_results = text_embedding_vectorstore.similarity_search_by_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/vector_stores/lancedb.py", line 118, in similarity_search_by_text
query_embedding = text_embedder(text)
^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/context_builder/entity_extraction.py", line 57, in
text_embedder=lambda t: text_embedder.embed(t),
^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/graphrag/query/llm/oai/embedding.py", line 96, in embed
chunk_embeddings = np.average(chunk_embeddings, axis=0, weights=chunk_lens)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/miniconda3/envs/rag/lib/python3.11/site-packages/numpy/lib/function_base.py", line 550, in average
raise ZeroDivisionError(
ZeroDivisionError: Weights sum to zero, can't be normalized

@jamesfyda
Copy link

I encounter the same situation while using local opensource embedding server with all-MiniLM-L6-v2 model

@Kingatlas115
Copy link

same issue. i can get global search working by modifying the openai_embeddings file. but no local search. hoping this is fixed soon

@AlonsoGuevara
Copy link
Contributor

Hi! We are centralizing other LLM discussions in these threads:
Other LLM/Api bases: #339,
Ollama: #345
Local embeddings: #370

I'll resolve this issue so we can keep the focus on those threads

@mbt1909432
Copy link

mbt1909432 commented Jul 12, 2024

My way can work, try this

file:.venv/Lib/site-packages/graphrag/query/llm/oai/embedding.py

def embed(self, text: str, **kwargs: Any) -> list[float]:
"""
Embed text using OpenAI Embedding's sync function.

    For text longer than max_tokens, chunk texts into max_tokens, embed each chunk, then combine using weighted average.
    Please refer to: https://github.com/openai/openai-cookbook/blob/main/examples/Embedding_long_inputs.ipynb
    """
    token_chunks = chunk_text(
        text=text, token_encoder=self.token_encoder, max_tokens=self.max_tokens
    )
    chunk_embeddings = []
    chunk_lens = []
    for chunk in token_chunks:

        chunk = self.token_encoder.decode(chunk)
        try:
            # embedding, chunk_len = self._embed_with_retry(chunk, **kwargs)
            # Make sure to `pip install openai` first
            from openai import OpenAI
            client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

            def get_embedding(text, model="nomic-ai/nomic-embed-text-v1.5-GGUF"):
                text = text.replace("\n", " ")
                return client.embeddings.create(input=[text], model=model).data[0].embedding

            print("__________")
            print(chunk)
            chunk_embeddings.append(get_embedding(chunk))
            chunk_lens.append(len(chunk))
        # TODO: catch a more specific exception
        except Exception as e:  # noqa BLE001
            self._reporter.error(
                message="Error embedding chunk",
                details={self.__class__.__name__: str(e)},
            )

            continue
    chunk_embeddings = np.average(chunk_embeddings, axis=0, weights=chunk_lens)
    chunk_embeddings = chunk_embeddings / np.linalg.norm(chunk_embeddings)
    return chunk_embeddings.tolist()

you should integrate ml studio api callback into this function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants