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.2k stars 1.59k forks source link

[Bug]: AttributeError: 'Candidate' object has no attribute 'grounding_metadata' in intro-grounding-gemini.ipynb #692

Open MrCsabaToth opened 1 month ago

MrCsabaToth commented 1 month ago

File Name

intro-grounding-gemini.ipynb

What happened?

For some reason the step

tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval())

response = model.generate_content(PROMPT, tools=[tool])

print_grounding_response(response)

resulted in this.

Holt Skinner asked me to file this bug. I was executing the lab in a Workbench instance, not Colab. The %pip install --upgrade --user --quiet google-cloud-aiplatform didn't seem to upgrade, it outputted:

%pip install --upgrade --user --quiet google-cloud-aiplatform
  WARNING: The script tb-gcp-uploader is installed in '/home/jupyter/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Note: you may need to restart the kernel to use updated packages.

pip freeze | grep ai:

aiofiles==22.1.0
aiohttp @ file:///home/conda/feedstock_root/build_artifacts/aiohttp_1710511536979/work
aiohttp-cors==0.7.0
aiosignal @ file:///home/conda/feedstock_root/build_artifacts/aiosignal_1667935791922/work
aiosqlite==0.20.0
explainable-ai-sdk==1.3.3
google-cloud-aiplatform==1.51.0
google-cloud-recommendations-ai==0.7.1
traitlets @ file:///home/conda/feedstock_root/build_artifacts/traitlets_1675110562325/work

Relevant log output

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[25], line 5
      1 tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval())
      3 response = model.generate_content(PROMPT, tools=[tool])
----> 5 print_grounding_response(response)

Cell In[24], line 5, in print_grounding_response(response)
      3 print(response.candidates[0])
      4 print(str(dir(response.candidates[0])))
----> 5 grounding_metadata = response.candidates[0].grounding_metadata
      7 # Citation indices are in byte units
      8 ENCODING = "utf-8"

AttributeError: 'Candidate' object has no attribute 'grounding_metadata'

Output of print(response.candidates[0]):

content {
  role: "model"
  parts {
    text: "The next solar eclipse in the US will be an annular solar eclipse on October 14, 2023. This eclipse will cross the US from Oregon to Texas. \n\nAfter that, the next solar eclipse will be a total solar eclipse on April 8, 2024. This eclipse will sweep across North America from Mazatl\303\241n to Newfoundland. \n\nThe next total solar eclipse visible in the contiguous US after 2024 will be on August 23, 2044. \n"
  }
}
finish_reason: STOP
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
  probability_score: 0.05654813349246979
  severity: HARM_SEVERITY_NEGLIGIBLE
  severity_score: 0.043042849749326706
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
  probability_score: 0.21667379140853882
  severity: HARM_SEVERITY_NEGLIGIBLE
  severity_score: 0.16940586268901825
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
  probability_score: 0.1710611879825592
  severity: HARM_SEVERITY_NEGLIGIBLE
  severity_score: 0.05399883911013603
}
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
  probability_score: 0.11338310688734055
  severity: HARM_SEVERITY_NEGLIGIBLE
  severity_score: 0.0699218362569809
}
grounding_metadata {
  web_search_queries: "When is the next solar eclipse in the US?"
}

Output of print(str(dir(response.candidates[0]))):

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_from_gapic', '_raw_candidate', 'citation_metadata', 'content', 'finish_message', 'finish_reason', 'from_dict', 'function_calls', 'index', 'safety_ratings', 'text', 'to_dict']

Code of Conduct

VJlaxmi commented 1 month ago

Hello Team,

I can take up this issue.

holtskinner commented 1 month ago

I think there was a change to the API service in how it outputs this field.

MrCsabaToth commented 1 month ago

What was weird to me is that the print showed that field the error complains about, however the dir didn't