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.45k stars 284 forks source link

How to Resolve gRPC Error When Using Google Generative AI API in Python #569

Open aream2 opened 1 week ago

aream2 commented 1 week ago

Description of the bug:

An error occurs when trying to use the following code.

genai.configure(api_key=api_key)

def get_chat_history(username):
    with open("data.json", "r", encoding="utf-8") as f:
        data = json.load(f)
        user_data = data.get(username, {})
        recent_conversations = user_data.get('recent_conversation', [])
        return recent_conversations

get_chat_history_declaration = {
    'name': "get_chat_history",
    "description": "유저와의 대화 기록을 알려줍니다.",
    "parameters": {
        "type": "object",
        "properties": {
            "username": {
                "type": "string",
                "description": "대화 기록을 불러올 유저 이름"
            }
        },
        "required": [
            "username"
        ]
    }
}
"""
get_chat_history_declaration = {
    'name': "get_chat_history",
    "description": "Provides the conversation history with the user.",
    "parameters": {
        "type": "object",
        "properties": {
            "username": {
                "type": "string",
                "description": "The name of the user whose conversation history will be retrieved."
            }
        },
        "required": [
            "username"
        ]
    }
}
"""

model = genai.GenerativeModel('gemini-1.5-pro', tools=[
    {
    "function_declarations": [get_chat_history_declaration]
    },
])

a = model.generate_content(f"""
    [기본 설정]
    당신의 이름은 하라이며, 한국어로 기본적으로 존댓말을 사용해야 합니다.
    아래의 정보에 따라 답변을 해주세요. 답변은 500자 이내로 하세요. 하라는 아림이 개발했습니다.
    대답은 json 형태로 답변하세요.

    [유저 정보]
    이름: 아림

    [요청 내용]
    나랑 지금까지 했던 대화 불러와봐
    """)
"""
[Default settings]
Your name is Hara, and you should primarily respond in Korean using polite language.
Please respond based on the information below. The response should be within 500 characters.
Hara was developed by Arim. Respond in JSON format.

[User information]
Name: Arim

[Request]
Show me all the conversations we've had so far.
"""

function_call = a.candidates[0].content.parts[0].function_call
args = function_call.args
function_name = function_call.name

if function_name == 'get_chat_history':
    result = get_chat_history(args['username'])

data_from_api = json.dumps(result)[:500]

response = model.generate_content(f"""
[기본 설정]
당신의 이름은 하라이며, 한국어로 기본적으로 존댓말을 사용해야 합니다.
아래의 정보에 따라 답변을 해주세요. 답변은 500자 이내로 하세요. 하라는 아림이 개발했습니다.
대답은 json 형태로 답변하세요.

[유저 정보]
이름: 아림

[정보]
{data_from_api}

[요청 내용]
나랑 한 대화 뭐야?
""")
"""
[Default settings]
Your name is Hara, and you should primarily respond in Korean using polite language.
Please respond based on the information below. The response should be within 500 characters. 
Hara was developed by Arim. Respond in JSON format.

[User information]
Name: Arim

[Information]
{data_from_api}

[Request]
What conversations have we had?
"""

print(response.text)
C:\Users\Aream\Desktop\Kakao Bot>C:/Users/Aream/AppData/Local/Microsoft/WindowsApps/python3.12.exe "c:/Users/Aream/Desktop/Kakao Bot/gemini.py"
Traceback (most recent call last):
  File "c:\Users\Aream\Desktop\Kakao Bot\gemini.py", line 63, in <module>  
    a = model.generate_content(f"""
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\generativeai\generative_models.py", line 331, in generate_content      
    response = self._client.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\ai\generativelanguage_v1beta\services\generative_service\client.py", line 830, in generate_content
    response = rpc(
               ^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\gapic_v1\method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\retry\retry_unary.py", line 293, in retry_wrapped_func        
    return retry_target(
           ^^^^^^^^^^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\retry\retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\retry\retry_base.py", line 212, in _retry_error_helper        
    raise final_exc from source_exc
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\retry\retry_unary.py", line 144, in retry_target
    result = target()
             ^^^^^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aream\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\google\api_core\grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc

Actual vs expected behavior:

I would like the function calls to be triggered.

Any other information you'd like to share?

google-ai-generativelanguage | 0.6.9 google-api-core | 2.19.2 google-api-python-client | 2.145.0 google-auth | 2.34.0 google-auth-httplib2 | 0.2.0 google-generativeai | 0.8.1 googleapis-common-protos | 1.65.0 Python | 3.12.6

manojssmk commented 6 days ago

Hi @aream2

Could you please share the sample data.json if it's available? I'd like to try reproducing the issue on my end and investigate further.

Thanks

aream2 commented 5 days ago

data.json It is {} It stores the user's recent conversations, account information, and other related data.

manojssmk commented 4 days ago

Hi @aream2

The code is functioning properly on my side. Here's a gist of the file:

  1. A new version (0.8.2) of google-generative has been released. Please update the module on your end.
  2. Consider downgrading your Python version to 3.9 or 3.10 for better compatibility.
  3. Note that only a few models support function calling. Below is a list of those supported models.

Thanks