Skip to content

Commit

Permalink
fix: Clarify Python and REST API query methods in sample notebooks wh…
Browse files Browse the repository at this point in the history
…en re-using Reasoning engine agents (#1532)

# Description

This PR clarifies the use of the Python SDK or REST API for querying a
deployed Reasoning Engine instance, rather than using Vertex AI client
libraries in other languages.
---------

Co-authored-by: Holt Skinner <[email protected]>
Co-authored-by: Holt Skinner <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent 5cfe6ed commit 45e434b
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 77 deletions.
153 changes: 108 additions & 45 deletions gemini/reasoning-engine/intro_reasoning_engine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"id": "tFy3H3aPgx12"
},
Expand All @@ -198,7 +198,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"id": "XRvKdaPDTznN"
},
Expand Down Expand Up @@ -235,7 +235,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {
"id": "NyKGtVQjgx13"
},
Expand Down Expand Up @@ -264,15 +264,15 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {
"id": "Nqwi-5ufWp_B"
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
"LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
"STAGING_BUCKET = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"STAGING_BUCKET = \"gs://[your-staging-bucket]\" # @param {type:\"string\"}\n",
"\n",
"import vertexai\n",
"\n",
Expand All @@ -299,7 +299,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {
"id": "lslYAvw37JGQ"
},
Expand Down Expand Up @@ -333,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {
"id": "921890fcb875"
},
Expand All @@ -360,7 +360,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {
"id": "ff7991bf37bf"
},
Expand Down Expand Up @@ -392,18 +392,18 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {
"id": "4ae49a2ccd2e"
},
"outputs": [
{
"data": {
"text/plain": [
"{'amount': 1.0, 'base': 'USD', 'date': '2024-04-19', 'rates': {'SEK': 10.949}}"
"{'amount': 1.0, 'base': 'USD', 'date': '2024-12-13', 'rates': {'SEK': 10.9479}}"
]
},
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -432,7 +432,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {
"id": "68bc1b395f9d"
},
Expand Down Expand Up @@ -467,7 +467,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {
"id": "e0c5c699de12"
},
Expand All @@ -476,15 +476,60 @@
"data": {
"text/plain": [
"{'input': \"What's the exchange rate from US dollars to Swedish currency today?\",\n",
" 'output': 'Today, 1 US dollar is equal to 10.949 Swedish krona. \\n',\n",
" 'intermediate_steps': [(AgentActionMessageLog(tool='get_exchange_rate', tool_input={'currency_from': 'USD', 'currency_to': 'SEK'}, log=\"\\nInvoking: `get_exchange_rate` with `{'currency_from': 'USD', 'currency_to': 'SEK'}`\\n\\n\\n\", message_log=[AIMessageChunk(content='', additional_kwargs={'function_call': {'name': 'get_exchange_rate', 'arguments': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}'}}, response_metadata={'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'citation_metadata': None, 'usage_metadata': {'prompt_token_count': 74, 'candidates_token_count': 13, 'total_token_count': 87}}, id='run-b4f2744c-e65c-4688-9287-b619ee84ead4', tool_calls=[{'name': 'get_exchange_rate', 'args': {'currency_from': 'USD', 'currency_to': 'SEK'}, 'id': '0a06cddb-73a1-47b2-b937-7856225fc4f1'}], tool_call_chunks=[{'name': 'get_exchange_rate', 'args': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}', 'id': '0a06cddb-73a1-47b2-b937-7856225fc4f1', 'index': None}])]),\n",
" 'output': 'The exchange rate from US dollars to Swedish krona is 1 USD to 10.9479 SEK. \\n',\n",
" 'intermediate_steps': [[{'lc': 1,\n",
" 'type': 'constructor',\n",
" 'id': ['langchain', 'schema', 'agent', 'ToolAgentAction'],\n",
" 'kwargs': {'tool': 'get_exchange_rate',\n",
" 'tool_input': {'currency_from': 'USD', 'currency_to': 'SEK'},\n",
" 'log': \"\\nInvoking: `get_exchange_rate` with `{'currency_from': 'USD', 'currency_to': 'SEK'}`\\n\\n\\n\",\n",
" 'type': 'AgentActionMessageLog',\n",
" 'message_log': [{'lc': 1,\n",
" 'type': 'constructor',\n",
" 'id': ['langchain', 'schema', 'messages', 'AIMessageChunk'],\n",
" 'kwargs': {'content': '',\n",
" 'additional_kwargs': {'function_call': {'name': 'get_exchange_rate',\n",
" 'arguments': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}'}},\n",
" 'response_metadata': {'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False,\n",
" 'severity': 'HARM_SEVERITY_NEGLIGIBLE'},\n",
" {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False,\n",
" 'severity': 'HARM_SEVERITY_LOW'},\n",
" {'category': 'HARM_CATEGORY_HARASSMENT',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False,\n",
" 'severity': 'HARM_SEVERITY_NEGLIGIBLE'},\n",
" {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n",
" 'probability_label': 'NEGLIGIBLE',\n",
" 'blocked': False,\n",
" 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}],\n",
" 'finish_reason': 'STOP'},\n",
" 'type': 'AIMessageChunk',\n",
" 'id': 'run-f88a789f-8c13-4a5c-81c2-e48eff00462c',\n",
" 'tool_calls': [{'name': 'get_exchange_rate',\n",
" 'args': {'currency_from': 'USD', 'currency_to': 'SEK'},\n",
" 'id': 'e54070df-2055-416d-9599-c891953869d3',\n",
" 'type': 'tool_call'}],\n",
" 'usage_metadata': {'input_tokens': 41,\n",
" 'output_tokens': 13,\n",
" 'total_tokens': 54},\n",
" 'tool_call_chunks': [{'name': 'get_exchange_rate',\n",
" 'args': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}',\n",
" 'id': 'e54070df-2055-416d-9599-c891953869d3',\n",
" 'index': None,\n",
" 'type': 'tool_call_chunk'}],\n",
" 'invalid_tool_calls': []}}],\n",
" 'tool_call_id': 'e54070df-2055-416d-9599-c891953869d3'}},\n",
" {'amount': 1.0,\n",
" 'base': 'USD',\n",
" 'date': '2024-04-19',\n",
" 'rates': {'SEK': 10.949}})]}"
" 'date': '2024-12-13',\n",
" 'rates': {'SEK': 10.9479}}]]}"
]
},
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -511,7 +556,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {
"id": "b2f8365735d2"
},
Expand All @@ -537,7 +582,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {
"id": "fd3b88c73411"
},
Expand All @@ -552,10 +597,10 @@
"Creating in-memory tarfile of extra_packages\n",
"Writing to gs://your-bucket-name/reasoning_engine/dependencies.tar.gz\n",
"Creating ReasoningEngine\n",
"Create ReasoningEngine backing LRO: projects/954731520814/locations/us-central1/reasoningEngines/4714776228647665664/operations/4748723134759174144\n",
"ReasoningEngine created. Resource name: projects/954731520814/locations/us-central1/reasoningEngines/4714776228647665664\n",
"Create ReasoningEngine backing LRO: projects/your-project-number/locations/us-central1/reasoningEngines/224106858019815424/operations/3507815273723854848\n",
"ReasoningEngine created. Resource name: projects/your-project-number/locations/us-central1/reasoningEngines/224106858019815424\n",
"To use this ReasoningEngine in another session:\n",
"reasoning_engine = vertexai.preview.reasoning_engines.ReasoningEngine('projects/954731520814/locations/us-central1/reasoningEngines/4714776228647665664')\n"
"reasoning_engine = vertexai.preview.reasoning_engines.ReasoningEngine('projects/your-project-number/locations/us-central1/reasoningEngines/224106858019815424')\n"
]
}
],
Expand All @@ -573,7 +618,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {
"id": "d01b37cb77dc"
},
Expand All @@ -582,10 +627,10 @@
"data": {
"text/plain": [
"{'input': \"What's the exchange rate from US dollars to Swedish currency today?\",\n",
" 'output': 'Today, 1 US dollar is equal to 10.949 Swedish krona. \\n'}"
" 'output': 'The exchange rate from US dollars to Swedish krona today is 1 USD to 10.9479 SEK. \\n'}"
]
},
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -602,25 +647,35 @@
"id": "9f2f7d3ed7bd"
},
"source": [
"### Reusing your deployed agent from other applications or SDKs"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "360870c674d5"
},
"source": [
"You can now import and use the remotely deployed Reasoning Engine in this notebook session or in a different notebook or Python script. First you need to get its resource_name by calling:"
"### Querying your deployed agent\n",
"\n",
"You've now deployed your Reasoning Engine agent and can [interact with it in multiple ways](https://cloud.google.com/vertex-ai/generative-ai/docs/reasoning-engine/use), both within this notebook and from other applications or environments. The primary methods for accessing your deployed agent are via the Python client library or through REST API calls. Here's an overview of both methods:\n",
"\n",
"**Method 1: Reusing within this notebook or another Python environment**\n",
"\n",
"You can directly reuse and query the `remote_agent` instance you created in this notebook.\n",
"\n",
"Or, you can instantiate a new instance in another notebook or Python script. To do this, you'll need to retrieve your deployed agent's resource name that uniquely identifies your agent, which is a string that includes the project, location, and Reasoning Engine ID. You can retrieve it by running the following code in the notebook or environment where you created your agent:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"id": "fdaf8b91413f"
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'projects/your-project-number/locations/us-central1/reasoningEngines/223106848019812424'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"remote_agent.resource_name"
]
Expand All @@ -631,20 +686,20 @@
"id": "060f8369d113"
},
"source": [
"Afterwards you can use it by uncommenting and adapting the following code:"
"Use the resource name to load the agent in your other notebook or Python script, then query the remote agent as usual:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 15,
"metadata": {
"id": "78af4442827e"
},
"outputs": [],
"source": [
"# from vertexai.preview import reasoning_engines\n",
"\n",
"# REASONING_ENGINE_RESOURCE_NAME = \"YOUR_REASONING_ENGINE_RESOURCE_NAME\"\n",
"# REASONING_ENGINE_RESOURCE_NAME = \"YOUR_REASONING_ENGINE_RESOURCE_NAME\" # Replace with the resource name of your deployed Reasoning Engine\n",
"\n",
"# remote_agent = reasoning_engines.ReasoningEngine(REASONING_ENGINE_RESOURCE_NAME)\n",
"# response = remote_agent.query(input=query)"
Expand All @@ -656,7 +711,15 @@
"id": "63ab06554fc0"
},
"source": [
"Or, you can query your agent from other programming languages using any of the [available client libraries in Vertex AI](https://cloud.google.com/vertex-ai/docs/start/client-libraries), including C#, Java, Node.js, Python, Go, or REST API."
"**Method 2: Accessing from other environments via REST API**\n",
"\n",
"Beyond the Python client library, your deployed Vertex AI agent can be [queried using REST API calls](https://cloud.google.com/vertex-ai/generative-ai/docs/reasoning-engine/use), including:\n",
"\n",
"- Python: You can use Python's `requests` library or similar tools to make HTTP calls to the Vertex AI REST API.\n",
"- cURL: A command-line tool, cURL allows you to send HTTP requests directly. This is useful for testing and debugging.\n",
"- Other Programming Languages: If you prefer a different language for your application, you can use its native HTTP client library to make REST API calls.\n",
"\n",
"In summary, you have access to your deployed Reasoning Engine agent through the Python client library within Python environments, and more universally through its REST API via tools and programming languages of your choosing."
]
},
{
Expand Down Expand Up @@ -692,7 +755,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 16,
"metadata": {
"id": "5408d3da2726"
},
Expand Down Expand Up @@ -735,7 +798,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 17,
"metadata": {
"id": "08623f46a9e4"
},
Expand Down Expand Up @@ -776,7 +839,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {
"id": "7cec827288b7"
},
Expand Down
Loading

0 comments on commit 45e434b

Please sign in to comment.