GoogleCloudPlatform / generative-ai

Sample code and notebooks for Generative AI on Google Cloud, with Gemini on Vertex AI
https://cloud.google.com/vertex-ai/docs/generative-ai/learn/overview
Apache License 2.0
6.97k stars 1.88k forks source link

ValueError: shapes (1,1408) and (1,1408) not aligned: 1408 (dim 1) != 1 (dim 0) #342

Closed yosephfernando closed 7 months ago

yosephfernando commented 8 months ago

when running notebook Multimodal Retrieval Augmented Generation (RAG) using Vertex AI Gemini API got error:

ValueError: shapes (1,1408) and (1,1408) not aligned: 1408 (dim 1) != 1 (dim 0) in **get_similar_text_from_query()**.

ValueError                                Traceback (most recent call last)

[<ipython-input-17-f8df6804e192>](https://localhost:8080/#) in <cell line: 2>()
      1 # Matching user text query with "chunk_embedding" to find relevant chunks.
----> 2 matching_results_text = get_similar_text_from_query(
      3     PROJECT_ID,
      4     query,
      5     text_metadata_df,

7 frames

[/content/utils/intro_multimodal_rag_utils.py](https://localhost:8080/#) in get_similar_text_from_query(project_id, query, text_metadata_df, column_name, top_n, embedding_size, chunk_text, print_citation)
    909 
    910     # Calculate cosine similarity between query text and metadata text
--> 911     cosine_scores = text_metadata_df.apply(
    912         lambda row: get_cosine_score(
    913             row,

[/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py](https://localhost:8080/#) in apply(self, func, axis, raw, result_type, args, **kwargs)
   9566             kwargs=kwargs,
   9567         )
-> 9568         return op.apply().__finalize__(self, method="apply")
   9569 
   9570     def applymap(

[/usr/local/lib/python3.10/dist-packages/pandas/core/apply.py](https://localhost:8080/#) in apply(self)
    762             return self.apply_raw()
    763 
--> 764         return self.apply_standard()
    765 
    766     def agg(self):

[/usr/local/lib/python3.10/dist-packages/pandas/core/apply.py](https://localhost:8080/#) in apply_standard(self)
    889 
    890     def apply_standard(self):
--> 891         results, res_index = self.apply_series_generator()
    892 
    893         # wrap results

[/usr/local/lib/python3.10/dist-packages/pandas/core/apply.py](https://localhost:8080/#) in apply_series_generator(self)
    905             for i, v in enumerate(series_gen):
    906                 # ignore SettingWithCopy here in case the user mutates
--> 907                 results[i] = self.f(v)
    908                 if isinstance(results[i], ABCSeries):
    909                     # If we have a view on v, we need to make a copy because

[/content/utils/intro_multimodal_rag_utils.py](https://localhost:8080/#) in <lambda>(row)
    910     # Calculate cosine similarity between query text and metadata text
    911     cosine_scores = text_metadata_df.apply(
--> 912         lambda row: get_cosine_score(
    913             row,
    914             column_name,

[/content/utils/intro_multimodal_rag_utils.py](https://localhost:8080/#) in get_cosine_score(dataframe, column_name, input_text_embd)
    674     """
    675 
--> 676     text_cosine_score = round(np.dot(dataframe[column_name], input_text_embd), 2)
    677     return text_cosine_score
    678 

/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in dot(*args, **kwargs)

ValueError: shapes (1,1408) and (1,1408) not aligned: 1408 (dim 1) != 1 (dim 0)
lavinigam-gcp commented 8 months ago

This is fixed https://github.com/GoogleCloudPlatform/generative-ai/pull/343

Note: We are reverting to older (pre-linting) version for the intro_multimodal_rag_utils.py for the time-being to unblock a few issues. However, we are working on fixing the lint issues that broke the notebook and push the tested version next week.