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.72k stars 1.8k forks source link

Problem when connecting Gemini to Vertex Search Data Store (currently known as Agent Builder) #569

Closed yamazakikakuyo closed 4 months ago

yamazakikakuyo commented 4 months ago

File Name

gemini/grounding/intro-grounding-gemini.ipynb

What happened?

Hello,

I have problem when connecting Data Store from Vertex Search and Conversation (known as Agent Builder now) to Gemini Model. Here's the error message

_InactiveRpcError Traceback (most recent call last) File /opt/conda/lib/python3.10/site-packages/google/api_core/grpc_helpers.py:65, in _wrap_unary_errors..error_remappedcallable(*args, **kwargs) 64 try: ---> 65 return callable(*args, **kwargs) 66 except grpc.RpcError as exc:

File /opt/conda/lib/python3.10/site-packages/grpc/_channel.py:1176, in _UnaryUnaryMultiCallable.call(self, request, timeout, metadata, credentials, wait_for_ready, compression) 1170 ( 1171 state, 1172 call, 1173 ) = self._blocking( 1174 request, timeout, metadata, credentials, wait_for_ready, compression 1175 ) -> 1176 return _end_unary_response_blocking(state, call, False, None)

File /opt/conda/lib/python3.10/site-packages/grpc/_channel.py:1005, in _end_unary_response_blocking(state, call, with_call, deadline) 1004 else: -> 1005 raise _InactiveRpcError(state)

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.INVALID_ARGUMENT details = "Request contains an invalid argument." debug_error_string = "UNKNOWN:Error received from peer ipv4:142.250.103.95:443 {grpc_message:"Request contains an invalid argument.", grpc_status:3, created_time:"2024-04-22T04:47:52.329937085+00:00"}"

The above exception was the direct cause of the following exception:

InvalidArgument Traceback (most recent call last) Cell In[13], line 3 1 tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval()) ----> 3 response = model.generate_content(PROMPT, tools=[tool]) 5 display(Markdown(response.candidates[0].text)) 7 response

File /opt/conda/lib/python3.10/site-packages/vertexai/generative_models/_generative_models.py:354, in _GenerativeModel.generate_content(self, contents, generation_config, safety_settings, tools, stream) 347 return self._generate_content_streaming( 348 contents=contents, 349 generation_config=generation_config, 350 safety_settings=safety_settings, 351 tools=tools, 352 ) 353 else: --> 354 return self._generate_content( 355 contents=contents, 356 generation_config=generation_config, 357 safety_settings=safety_settings, 358 tools=tools, 359 )

File /opt/conda/lib/python3.10/site-packages/vertexai/generative_models/_generative_models.py:435, in _GenerativeModel._generate_content(self, contents, generation_config, safety_settings, tools) 412 """Generates content. 413 414 Args: (...) 427 A single GenerationResponse object 428 """ 429 request = self._prepare_request( 430 contents=contents, 431 generation_config=generation_config, 432 safety_settings=safety_settings, 433 tools=tools, 434 ) --> 435 gapic_response = self._prediction_client.generate_content(request=request) 436 return self._parse_response(gapic_response)

File /opt/conda/lib/python3.10/site-packages/google/cloud/aiplatform_v1beta1/services/prediction_service/client.py:2080, in PredictionServiceClient.generate_content(self, request, model, contents, retry, timeout, metadata) 2077 self._validate_universe_domain() 2079 # Send the request. -> 2080 response = rpc( 2081 request, 2082 retry=retry, 2083 timeout=timeout, 2084 metadata=metadata, 2085 ) 2087 # Done; return the response. 2088 return response

File /opt/conda/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py:113, in _GapicCallable.call(self, timeout, retry, *args, *kwargs) 110 metadata.extend(self._metadata) 111 kwargs["metadata"] = metadata --> 113 return wrapped_func(args, **kwargs)

File /opt/conda/lib/python3.10/site-packages/google/api_core/grpc_helpers.py:67, in _wrap_unary_errors..error_remappedcallable(*args, **kwargs) 65 return callable(*args, **kwargs) 66 except grpc.RpcError as exc: ---> 67 raise exceptions.from_grpc_error(exc) from exc

InvalidArgument: 400 Request contains an invalid argument.

I have followed the instruction regarding how to connect Data Store as Grounding for Gemini model in this link (https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/grounding) and also tutorial in this repository (https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/grounding/intro-grounding-gemini.ipynb). But I still got the same error as above, etiher I use Vertex Data Store or Web Search one.

I hope there's a way to solve this error. Thanks a lot beforehand!

Relevant log output

No response

Code of Conduct

koverholt commented 4 months ago

Hi @yamazakikakuyo, thanks for reporting! I ran the gemini/intro-grounding-gemini.ipynb notebook with a search data store, and the retrieval and responses are working as expected. Can you give more details on:

yamazakikakuyo commented 4 months ago

Hi, thank you for replying. Here's the detail:

I have run it again recently and somehow it run smoothly without any error. Thanks a lot for your help.

Regarding the code, here's the snippet code that i used.

import vertexai
from vertexai.generative_models import GenerativeModel, Tool
from vertexai.preview import generative_models as preview_generative_models
from google.cloud import discoveryengine

vertexai.init(project=PROJECT_ID, location='us-central1')
client_discoveryengine= discoveryengine.DataStoreServiceClient()
path = client_discoveryengine.collection_path(project=PROJECT_ID, location='global', collection='default_collection')

vertex_search_tool = Tool.from_retrieval(
    retrieval=preview_generative_models.grounding.Retrieval(
        source=preview_generative_models.grounding.VertexAISearch(datastore=path+f'dataStores/{DATASTORE_ID}'),
    )
)

model = GenerativeModel(
    "gemini-1.0-pro",
    generation_config={"temperature": 0},
    tools=[
        vertex_search_tool,
    ],
)
chat = model.start_chat()

response = chat.send_message("I want to know what kind of shipping mode available right now")
response = response.candidates[0].content.parts[0]
print(response._raw_part.text)
koverholt commented 4 months ago

@yamazakikakuyo, that's great news! Thanks for letting us know that things are working again, and thanks 2x for the sample code that you used as inputs.