Skip to content

Commit

Permalink
Merge pull request #166 from deepset-ai/vertex-updates
Browse files Browse the repository at this point in the history
update Vertex notebook
  • Loading branch information
bilgeyucel authored Jan 30, 2025
2 parents f69d7da + e56bd3e commit faa47b6
Showing 1 changed file with 21 additions and 51 deletions.
72 changes: 21 additions & 51 deletions notebooks/vertexai-gemini-examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@
"\n",
"Let's see if we can build a system that can run a `get_current_weather` function, based on a question asked in natural language.\n",
"\n",
"First we create our function definition and tool.\n",
"First we create our function definition and tool (learn more about [Tools](https://docs.haystack.deepset.ai/docs/tool) in the docs).\n",
"\n",
"For demonstration purposes, we're simply creating a `get_current_weather` function that returns an object which will _always_ tell us it's 'Sunny, and 21.8 degrees'.. If it's Celsius, that's a good day! ☀️"
"For demonstration purposes, we're simply creating a `get_current_weather` function that returns an object which will _always_ tell us it's 'Sunny, and 21.8 degrees'... If it's Celsius, that's a good day! ☀️"
]
},
{
Expand All @@ -255,48 +255,17 @@
},
"outputs": [],
"source": [
"def get_current_weather(location: str, unit: str = \"celsius\"):\n",
" return {\"weather\": \"sunny\", \"temperature\": 21.8, \"unit\": unit}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GjapaK2yq-ce"
},
"source": [
"Now we have to provide this function as a `Tool` to Gemini. So, first we need to create a `FunctionDeclaration` that _explains_ this function to Gemini 👇"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6KCv7g4JOGZW"
},
"outputs": [],
"source": [
"from vertexai.generative_models import Tool, FunctionDeclaration\n",
"\n",
"get_current_weather_func = FunctionDeclaration(\n",
" name=\"get_current_weather\",\n",
" description=\"Get the current weather in a given location\",\n",
" parameters={\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\"type\": \"string\", \"description\": \"The city and state, e.g. San Francisco, CA\"},\n",
" \"unit\": {\n",
" \"type\": \"string\",\n",
" \"enum\": [\n",
" \"celsius\",\n",
" \"fahrenheit\",\n",
" ],\n",
" },\n",
" },\n",
" \"required\": [\"location\"],\n",
" },\n",
")\n",
"tool = Tool([get_current_weather_func])"
"from haystack.components.tools import ToolInvoker\n",
"from haystack.tools import create_tool_from_function\n",
"from typing import Annotated\n",
"\n",
"def get_current_weather(\n",
" location: Annotated[str, \"The city for which to get the weather, e.g. 'San Francisco'\"] = \"Munich\",\n",
" unit: Annotated[str, \"The unit for the temperature, e.g. 'celsius'\"] = \"celsius\",\n",
"):\n",
" return {\"weather\": \"sunny\", \"temperature\": 21.8, \"unit\": unit}\n",
"\n",
"weather_tool = create_tool_from_function(get_current_weather)"
]
},
{
Expand All @@ -322,7 +291,7 @@
"source": [
"from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator\n",
"\n",
"gemini_chat = VertexAIGeminiChatGenerator(model=\"gemini-pro\", project_id=project_id, tools=[tool])"
"gemini_chat = VertexAIGeminiChatGenerator(model=\"gemini-pro\", project_id=project_id, tools=[weather_tool])"
]
},
{
Expand All @@ -335,9 +304,9 @@
"source": [
"from haystack.dataclasses import ChatMessage\n",
"\n",
"messages = [ChatMessage.from_user(content = \"What is the temperature in celsius in Berlin?\")]\n",
"res = gemini_chat.run(messages=messages)\n",
"res[\"replies\"]"
"user_message = [ChatMessage.from_user(\"What is the temperature in celsius in Berlin?\")]\n",
"replies = gemini_chat.run(messages=user_message)[\"replies\"]\n",
"print(replies)"
]
},
{
Expand All @@ -360,10 +329,11 @@
},
"outputs": [],
"source": [
"tool_invoker = ToolInvoker(tools=[weather_tool])\n",
"tool_messages = tool_invoker.run(messages=replies)[\"tool_messages\"]\n",
"\n",
"weather = get_current_weather(**res[\"replies\"][0].text)\n",
"\n",
"messages += res[\"replies\"] + [ChatMessage.from_function(content=weather, name=\"get_current_weather\")]\n",
"messages = user_message + replies + tool_messages\n",
"print(messages)\n",
"\n",
"res = gemini_chat.run(messages = messages)\n",
"res[\"replies\"][0].text"
Expand Down

0 comments on commit faa47b6

Please sign in to comment.