Skip to content

Commit

Permalink
Update notebook with streaming API samples and cell outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
koverholt committed Jan 22, 2025
1 parent c6ad481 commit 16110e3
Showing 1 changed file with 101 additions and 25 deletions.
126 changes: 101 additions & 25 deletions gemini/reasoning-engine/intro_reasoning_engine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@
"data": {
"text/plain": [
"{'input': \"What's the exchange rate from US dollars to Swedish currency today?\",\n",
" 'output': 'The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. ',\n",
" 'output': 'The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. \\n',\n",
" 'intermediate_steps': [[{'lc': 1,\n",
" 'type': 'constructor',\n",
" 'id': ['langchain', 'schema', 'agent', 'ToolAgentAction'],\n",
Expand Down Expand Up @@ -518,21 +518,21 @@
" 'severity_score': 0.1025390625}],\n",
" 'finish_reason': 'STOP'},\n",
" 'type': 'AIMessageChunk',\n",
" 'id': 'run-62be0739-3a82-4252-b771-4f8bc7b6d002',\n",
" 'id': 'run-5772c381-37c4-4ad9-8abe-004881ea5d38',\n",
" 'tool_calls': [{'name': 'get_exchange_rate',\n",
" 'args': {'currency_from': 'USD', 'currency_to': 'SEK'},\n",
" 'id': 'bff56715-08e6-4e95-851b-791c595684ca',\n",
" 'id': '7ccbe707-ffea-4081-a1bf-ac0b16bf4ca8',\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': 'bff56715-08e6-4e95-851b-791c595684ca',\n",
" 'id': '7ccbe707-ffea-4081-a1bf-ac0b16bf4ca8',\n",
" 'index': None,\n",
" 'type': 'tool_call_chunk'}],\n",
" 'invalid_tool_calls': []}}],\n",
" 'tool_call_id': 'bff56715-08e6-4e95-851b-791c595684ca'}},\n",
" 'tool_call_id': '7ccbe707-ffea-4081-a1bf-ac0b16bf4ca8'}},\n",
" {'amount': 1.0,\n",
" 'base': 'USD',\n",
" 'date': '2025-01-22',\n",
Expand All @@ -552,14 +552,53 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to the default query mode, the `.stream_query` method allows you to **see the agent's intermediate steps and final output from the chain**. Instead of waiting for the agent to complete all sub-tasks, the agent sends back the response in **chunks as it's being generated**:"
"In addition to the default query mode, the `.stream_query` method allows you to **see the agent's intermediate steps and final output from the chain**.\n",
"\n",
"Instead of waiting for the agent to complete all sub-tasks, the agent sends back the response in **chunks as it's being generated**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"------\n",
"\n",
"Action:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'agent', 'ToolAgentAction'], 'kwargs': {'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\", 'type': 'AgentActionMessageLog', 'message_log': [{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'AIMessageChunk'], 'kwargs': {'content': '', 'additional_kwargs': {'function_call': {'name': 'get_exchange_rate', 'arguments': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}'}}, 'response_metadata': {'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.1416015625, 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE', 'severity_score': 0.1240234375}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.2490234375, 'blocked': False, 'severity': 'HARM_SEVERITY_LOW', 'severity_score': 0.265625}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.1669921875, 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE', 'severity_score': 0.10693359375}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.08740234375, 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE', 'severity_score': 0.1025390625}], 'finish_reason': 'STOP'}, 'type': 'AIMessageChunk', 'id': 'run-3162126c-cdb1-453d-a00b-da810a1fa6f9', 'tool_calls': [{'name': 'get_exchange_rate', 'args': {'currency_from': 'USD', 'currency_to': 'SEK'}, 'id': '0cb6cd0c-3de8-4ce6-a825-10d1eb5406e4', 'type': 'tool_call'}], 'usage_metadata': {'input_tokens': 41, 'output_tokens': 13, 'total_tokens': 54}, 'tool_call_chunks': [{'name': 'get_exchange_rate', 'args': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}', 'id': '0cb6cd0c-3de8-4ce6-a825-10d1eb5406e4', 'index': None, 'type': 'tool_call_chunk'}], 'invalid_tool_calls': []}}], 'tool_call_id': '0cb6cd0c-3de8-4ce6-a825-10d1eb5406e4'}}]\n",
"\n",
"------\n",
"\n",
"Message:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'AIMessageChunk'], 'kwargs': {'content': '', 'additional_kwargs': {'function_call': {'name': 'get_exchange_rate', 'arguments': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}'}}, 'response_metadata': {'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.1416015625, 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE', 'severity_score': 0.1240234375}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.2490234375, 'blocked': False, 'severity': 'HARM_SEVERITY_LOW', 'severity_score': 0.265625}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.1669921875, 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE', 'severity_score': 0.10693359375}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'probability_score': 0.08740234375, 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE', 'severity_score': 0.1025390625}], 'finish_reason': 'STOP'}, 'type': 'AIMessageChunk', 'id': 'run-3162126c-cdb1-453d-a00b-da810a1fa6f9', 'tool_calls': [{'name': 'get_exchange_rate', 'args': {'currency_from': 'USD', 'currency_to': 'SEK'}, 'id': '0cb6cd0c-3de8-4ce6-a825-10d1eb5406e4', 'type': 'tool_call'}], 'usage_metadata': {'input_tokens': 41, 'output_tokens': 13, 'total_tokens': 54}, 'tool_call_chunks': [{'name': 'get_exchange_rate', 'args': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}', 'id': '0cb6cd0c-3de8-4ce6-a825-10d1eb5406e4', 'index': None, 'type': 'tool_call_chunk'}], 'invalid_tool_calls': []}}]\n",
"\n",
"------\n",
"\n",
"Message:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'FunctionMessage'], 'kwargs': {'content': '{\"amount\": 1.0, \"base\": \"USD\", \"date\": \"2025-01-22\", \"rates\": {\"SEK\": 10.9806}}', 'type': 'function', 'name': 'get_exchange_rate'}}]\n",
"\n",
"------\n",
"\n",
"Message:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'AIMessage'], 'kwargs': {'content': 'The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. ', 'type': 'ai', 'tool_calls': [], 'invalid_tool_calls': []}}]\n",
"\n",
"------\n",
"\n",
"Output:\n",
"\n",
"The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. \n"
]
}
],
"source": [
"message_types = {\"actions\": \"Action\", \"messages\": \"Message\", \"output\": \"Output\"}\n",
"for chunk in agent.stream_query(input=\"What's the exchange rate from US dollars to Swedish currency today?\"):\n",
Expand Down Expand Up @@ -595,7 +634,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {
"id": "b2f8365735d2"
},
Expand All @@ -621,7 +660,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {
"id": "fd3b88c73411"
},
Expand All @@ -636,10 +675,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/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",
"Create ReasoningEngine backing LRO: projects/your-project-number/locations/us-central1/reasoningEngines/3381833884248309760/operations/2331218534991921152\n",
"ReasoningEngine created. Resource name: projects/your-project-number/locations/us-central1/reasoningEngines/3381833884248309760\n",
"To use this ReasoningEngine in another session:\n",
"reasoning_engine = vertexai.preview.reasoning_engines.ReasoningEngine('projects/your-project-number/locations/us-central1/reasoningEngines/224106858019815424')\n"
"reasoning_engine = vertexai.preview.reasoning_engines.ReasoningEngine('projects/your-project-number/locations/us-central1/reasoningEngines/3381833884248309760')\n"
]
}
],
Expand All @@ -664,7 +703,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {
"id": "d01b37cb77dc"
},
Expand All @@ -673,10 +712,10 @@
"data": {
"text/plain": [
"{'input': \"What's the exchange rate from US dollars to Swedish currency today?\",\n",
" 'output': 'The exchange rate from US dollars to Swedish krona today is 1 USD to 10.9806 SEK. \\n'}"
" 'output': 'The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. \\n'}"
]
},
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -696,9 +735,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"------\n",
"\n",
"Action:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'agent', 'ToolAgentAction'], 'kwargs': {'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\", 'type': 'AgentActionMessageLog', 'message_log': [{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'AIMessageChunk'], 'kwargs': {'content': '', 'additional_kwargs': {'function_call': {'name': 'get_exchange_rate', 'arguments': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}'}}, 'response_metadata': {'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_LOW'}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}], 'finish_reason': 'STOP', 'logprobs_result': {'top_candidates': [], 'chosen_candidates': []}}, 'type': 'AIMessageChunk', 'id': 'run-bb1eaa31-ae2e-4e48-a155-8ee787b666b9', 'tool_calls': [{'name': 'get_exchange_rate', 'args': {'currency_from': 'USD', 'currency_to': 'SEK'}, 'id': '61ffcbd8-30b8-41ba-a5b4-094c0d8619e7', 'type': 'tool_call'}], 'usage_metadata': {'input_tokens': 41, 'output_tokens': 13, 'total_tokens': 54}, 'tool_call_chunks': [{'name': 'get_exchange_rate', 'args': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}', 'id': '61ffcbd8-30b8-41ba-a5b4-094c0d8619e7', 'index': None, 'type': 'tool_call_chunk'}], 'invalid_tool_calls': []}}], 'tool_call_id': '61ffcbd8-30b8-41ba-a5b4-094c0d8619e7'}}]\n",
"\n",
"------\n",
"\n",
"Message:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'AIMessageChunk'], 'kwargs': {'content': '', 'additional_kwargs': {'function_call': {'name': 'get_exchange_rate', 'arguments': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}'}}, 'response_metadata': {'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_LOW'}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False, 'severity': 'HARM_SEVERITY_NEGLIGIBLE'}], 'finish_reason': 'STOP', 'logprobs_result': {'top_candidates': [], 'chosen_candidates': []}}, 'type': 'AIMessageChunk', 'id': 'run-bb1eaa31-ae2e-4e48-a155-8ee787b666b9', 'tool_calls': [{'name': 'get_exchange_rate', 'args': {'currency_from': 'USD', 'currency_to': 'SEK'}, 'id': '61ffcbd8-30b8-41ba-a5b4-094c0d8619e7', 'type': 'tool_call'}], 'usage_metadata': {'input_tokens': 41, 'output_tokens': 13, 'total_tokens': 54}, 'tool_call_chunks': [{'name': 'get_exchange_rate', 'args': '{\"currency_from\": \"USD\", \"currency_to\": \"SEK\"}', 'id': '61ffcbd8-30b8-41ba-a5b4-094c0d8619e7', 'index': None, 'type': 'tool_call_chunk'}], 'invalid_tool_calls': []}}]\n",
"\n",
"------\n",
"\n",
"Message:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'FunctionMessage'], 'kwargs': {'content': '{\"amount\": 1.0, \"base\": \"USD\", \"date\": \"2025-01-22\", \"rates\": {\"SEK\": 10.9806}}', 'type': 'function', 'name': 'get_exchange_rate'}}]\n",
"\n",
"------\n",
"\n",
"Message:\n",
"\n",
"[{'lc': 1, 'type': 'constructor', 'id': ['langchain', 'schema', 'messages', 'AIMessage'], 'kwargs': {'content': 'The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. ', 'type': 'ai', 'tool_calls': [], 'invalid_tool_calls': []}}]\n",
"\n",
"------\n",
"\n",
"Output:\n",
"\n",
"The exchange rate from US dollars to Swedish krona is 1 USD to 10.9806 SEK. \n"
]
}
],
"source": [
"message_types = {\"actions\": \"Action\", \"messages\": \"Message\", \"output\": \"Output\"}\n",
"for chunk in remote_agent.stream_query(input=\"What's the exchange rate from US dollars to Swedish currency today?\"):\n",
Expand Down Expand Up @@ -729,18 +805,18 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 16,
"metadata": {
"id": "fdaf8b91413f"
},
"outputs": [
{
"data": {
"text/plain": [
"'projects/your-project-number/locations/us-central1/reasoningEngines/223106848019812424'"
"'projects/your-project-number/locations/us-central1/reasoningEngines/3381833884248309760'"
]
},
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -760,7 +836,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 17,
"metadata": {
"id": "78af4442827e"
},
Expand Down Expand Up @@ -824,7 +900,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"metadata": {
"id": "5408d3da2726"
},
Expand Down Expand Up @@ -867,7 +943,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 19,
"metadata": {
"id": "08623f46a9e4"
},
Expand Down Expand Up @@ -908,7 +984,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 20,
"metadata": {
"id": "7cec827288b7"
},
Expand Down

0 comments on commit 16110e3

Please sign in to comment.