google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.6k stars 319 forks source link

Empty Chat Responses #12

Closed mwmeyer closed 1 year ago

mwmeyer commented 1 year ago

I'm noticing that a few simple questions are not getting responses why I try to use them with the chat methods. For example:

palm.chat(messages='Why is the sky blue?')
> ChatResponse(model='models/chat-bison-001', context='', examples=[], messages=[{'author': '0', 'content': 'Why is the sky blue?'}, None], temperature=None, candidate_count=None, candidates=[], top_p=None, top_k=None, filters=[])

The same question works fine with generate_text

palm.generate_text(model=model, prompt='Why is the sky blue?')
> Completion(candidates=[{'output': 'The sky is blue due to a phenomenon called Rayleigh scattering. This is the scattering of light by particles that are smaller than the wavelength of light. The particles in the atmosphere that cause Rayleigh scattering are molecules of nitrogen and oxygen.\n\nWhen sunlight hits these molecules, it is scattered in all directions. However, blue light is scattered more than other colors because it has a shorter wavelength. This is why the sky appears blue during the day.\n\nThe amount of scattering depends on the amount of particles in the atmosphere. This is why the sky appears darker at sunrise and sunset, when there are more particles in the air.\n\nThe sky can also appear blue at other times, such as when there is dust or moisture in the air. This is because these particles can also scatter sunlight.', 'safety_ratings': [{'category': <HarmCategory.HARM_CATEGORY_DEROGATORY: 1>, 'probability': <HarmProbability.NEGLIGIBLE: 1>}, {'category': <HarmCategory.HARM_CATEGORY_TOXICITY: 2>, 'probability': <HarmProbability.NEGLIGIBLE: 1>}, {'category': <HarmCategory.HARM_CATEGORY_VIOLENCE: 3>, 'probability': <HarmProbability.NEGLIGIBLE: 1>}, {'category': <HarmCategory.HARM_CATEGORY_SEXUAL: 4>, 'probability': <HarmProbability.NEGLIGIBLE: 1>}, {'category': <HarmCategory.HARM_CATEGORY_MEDICAL: 5>, 'probability': <HarmProbability.NEGLIGIBLE: 1>}, {'category': <HarmCategory.HARM_CATEGORY_DANGEROUS: 6>, 'probability': <HarmProbability.NEGLIGIBLE: 1>}]}], result='The sky is blue due to a phenomenon called Rayleigh scattering. This is the scattering of light by particles that are smaller than the wavelength of light. The particles in the atmosphere that cause Rayleigh scattering are molecules of nitrogen and oxygen.\n\nWhen sunlight hits these molecules, it is scattered in all directions. However, blue light is scattered more than other colors because it has a shorter wavelength. This is why the sky appears blue during the day.\n\nThe amount of scattering depends on the amount of particles in the atmosphere. This is why the sky appears darker at sunrise and sunset, when there are more particles in the air.\n\nThe sky can also appear blue at other times, such as when there is dust or moisture in the air. This is because these particles can also scatter sunlight.', filters=[], safety_feedback=[])

Not sure if its the model or the client, but how can developers generally go about trouble shooting this? Is there any way to look at the raw api response?

infwinston commented 1 year ago

+1. I also got None response with certain question like "Who are you?" but I was able to get response for questions like "Tell me some story."

> palm.chat(messages=["Who are you?"])
ChatResponse(model='models/chat-bison-001', context='', examples=[], messages=[{'author': '0', 'content': 'Who are you?'}, None], temperature=None, candidate_count=None, candidates=[], top_p=None, top_k=None, filters=[{'reason': <BlockedReason.OTHER: 2>}])
> palm.chat(messages=["Tell me some story."])
ChatResponse(model='models/chat-bison-001', context='', examples=[], messages=[{'author': '0', 'content': 'Tell me some story.'}, {'author': '1', 'content': 'Once upon a time, there was a beautiful princess who lived in a grand castle. She had everything she could ever want, but she was not happy. She longed for adventure and excitement.\n\nOne day, a handsome prince came to the castle. He was everything the princess had ever dreamed of. He was brave, kind, and strong. The princess fell in love with him immediately.\n\nThe prince told the princess stories of his travels and adventures. The princess was captivated. She knew that she wanted to experience the world for herself.\n\nThe next day, the princess packed her bags and left the castle. She set off on a journey to find her own adventure.\n\nThe princess traveled far and wide. She met many interesting people and had many exciting experiences. She learned a lot about herself and the world around her.\n\nOne day, the princess came to a dark forest. She was afraid, but she knew that she had to go through the forest to reach her destination.\n\nThe princess walked through the forest for hours. She was tired and hungry. She was about to give up when she saw a light in the distance.\n\nThe princess followed the light and came to a small cottage. An old woman lived in the cottage. She invited the princess in and gave her food and a place to sleep.\n\nThe next morning, the princess told the old woman about her journey. The old woman listened patiently. When the princess was finished, the old woman said, "You have come a long way, my dear. You have learned a lot about yourself and the world around you. Now it is time for you to go home."\n\nThe princess was sad to leave the old woman, but she knew that the old woman was right. She said goodbye to the old woman and set off on her journey home.\n\nThe princess traveled for many days. She was tired and homesick, but she knew that she was almost home.\n\nOne day, the princess came to a hill. She climbed to the top of the hill and looked out. She could see her castle in the distance.\n\nThe princess ran down the hill and into the castle. She was greeted by her parents and her friends. They were all so happy to see her.\n\nThe princess told her parents and her friends all about her journey. They were amazed by her stories.\n\nThe princess was glad to be home, but she knew that she would never forget her journey. She had learned a lot about herself and the world around her. She was a different person now, and she was ready for whatever the future held.'}], temperature=None, candidate_count=None, candidates=[{'author': '1', 'content': 'Once upon a time, there was a beautiful princess who lived in a grand castle. She had everything she could ever want, but she was not happy. She longed for adventure and excitement.\n\nOne day, a handsome prince came to the castle. He was everything the princess had ever dreamed of. He was brave, kind, and strong. The princess fell in love with him immediately.\n\nThe prince told the princess stories of his travels and adventures. The princess was captivated. She knew that she wanted to experience the world for herself.\n\nThe next day, the princess packed her bags and left the castle. She set off on a journey to find her own adventure.\n\nThe princess traveled far and wide. She met many interesting people and had many exciting experiences. She learned a lot about herself and the world around her.\n\nOne day, the princess came to a dark forest. She was afraid, but she knew that she had to go through the forest to reach her destination.\n\nThe princess walked through the forest for hours. She was tired and hungry. She was about to give up when she saw a light in the distance.\n\nThe princess followed the light and came to a small cottage. An old woman lived in the cottage. She invited the princess in and gave her food and a place to sleep.\n\nThe next morning, the princess told the old woman about her journey. The old woman listened patiently. When the princess was finished, the old woman said, "You have come a long way, my dear. You have learned a lot about yourself and the world around you. Now it is time for you to go home."\n\nThe princess was sad to leave the old woman, but she knew that the old woman was right. She said goodbye to the old woman and set off on her journey home.\n\nThe princess traveled for many days. She was tired and homesick, but she knew that she was almost home.\n\nOne day, the princess came to a hill. She climbed to the top of the hill and looked out. She could see her castle in the distance.\n\nThe princess ran down the hill and into the castle. She was greeted by her parents and her friends. They were all so happy to see her.\n\nThe princess told her parents and her friends all about her journey. They were amazed by her stories.\n\nThe princess was glad to be home, but she knew that she would never forget her journey. She had learned a lot about herself and the world around her. She was a different person now, and she was ready for whatever the future held.'}], top_p=None, top_k=None, filters=[])
MarkDaoust commented 1 year ago

Yeah thanks for reporting. The devs are working on giving more feedback for blocked responses. But it's not clear if that will help for simple seemingly innocuous cases like these.