Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Vertex AI Colab #1

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
update
  • Loading branch information
TuanaCelik committed Jan 2, 2024
commit 9673b41cf19cf34fcb285c09c79f423415b066bf
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# haystack-coookbook
👩🏻‍🍳 A collection of example notebooks
# 👩🏻‍🍳 Haystack Cookbook
A collection of example notebooks using Haystack 👇

| Name | Colab|
| ---- | ---- |
| Use Gemini Models with Vertex AI| <a href="https://colab.research.google.com/github/deepset-ai/haystack-cookbook/blob/main/vertexai-gemini-examples.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>|

## How to Contribute to this repository

If you have an example that uses Haystack, you can add it to this repository by creating a PR. You can also create a PR from Colab by creating a Fork of this repository and selecting "Save a Copy to GitHub". Once you add your example to your fork, you can create a PR onto this repository.

1. Add your Notebook
2. Give a descriptive name to your file that includes the names of (if applicable) the model providers, databases the technologies you use in your example and/or the task you are completing in the example.
3. Make sure to add a row in the table above 🎉
194 changes: 92 additions & 102 deletions vertexai-gemini-examples.ipynb
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/TuanaCelik/haystack-cookbook/blob/main/vertexai-gemini-examples.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -28,6 +18,10 @@
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "toc",
"id": "kWU_3yqtloif"
},
"source": [
">[Haystack 2.0 💙 Google Gemini](#scrollTo=b2s1IpH6OGZQ)\n",
"\n",
Expand All @@ -43,14 +37,13 @@
"\n",
">>[Build a full Retrieval-Augmented Generation Pipeline with gemini-pro-vision](#scrollTo=Pi37EVlDenPw)\n",
"\n"
],
"metadata": {
"colab_type": "toc",
"id": "kWU_3yqtloif"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XromVwB1nQ76"
},
"source": [
"## Install dependencies\n",
"\n",
Expand All @@ -59,10 +52,7 @@
"First thing first we need to install our dependencies.\n",
"\n",
"(You can ignore the pip dependency error for `cohere` and `tiktoken`, that's irrelevant for our purposes.)"
],
"metadata": {
"id": "XromVwB1nQ76"
}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -124,23 +114,23 @@
},
{
"cell_type": "markdown",
"source": [
"## Use `gemini-pro-vision`"
],
"metadata": {
"id": "08TA9zAQlqy6"
}
},
"source": [
"## Use `gemini-pro-vision`"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c1ynAXT5mI1s"
},
"source": [
"### Answer Questions\n",
"\n",
"Now that we setup everything we can create an instance of our Gemini component."
],
"metadata": {
"id": "c1ynAXT5mI1s"
}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -181,12 +171,12 @@
},
{
"cell_type": "markdown",
"source": [
"### Answer Questions about Images"
],
"metadata": {
"id": "H3pU-t_7mPyH"
}
},
"source": [
"### Answer Questions about Images"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -224,25 +214,25 @@
},
{
"cell_type": "markdown",
"source": [
"Next, let's run the `GeminiGenerator` component on it's own."
],
"metadata": {
"id": "tQsdovv2pWzh"
}
},
"source": [
"Next, let's run the `GeminiGenerator` component on it's own."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6jSBGEkmpPsN"
},
"outputs": [],
"source": [
"result = gemini.run(parts = [\"What can you tell me about this robots?\", *images])\n",
"for answer in result[\"answers\"]:\n",
" print(answer)"
],
"metadata": {
"id": "6jSBGEkmpPsN"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -255,15 +245,18 @@
},
{
"cell_type": "markdown",
"source": [
"## Function Calling with `gemini-pro`"
],
"metadata": {
"id": "gLIIQ4PZmX-H"
}
},
"source": [
"## Function Calling with `gemini-pro`"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iCiSz840mfME"
},
"source": [
"With `gemini-pro`, we can also start introducing function calling!\n",
"So let's see how we can do that 👇\n",
Expand All @@ -273,31 +266,28 @@
"First we create our function definition and tool.\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! ☀️"
],
"metadata": {
"id": "iCiSz840mfME"
}
]
},
{
"cell_type": "code",
"source": [
"def get_current_weather(location: str, unit: str = \"celsius\"):\n",
" return {\"weather\": \"sunny\", \"temperature\": 21.8, \"unit\": unit}"
],
"execution_count": null,
"metadata": {
"id": "4Wa_IoDDNg9V"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"def get_current_weather(location: str, unit: str = \"celsius\"):\n",
" return {\"weather\": \"sunny\", \"temperature\": 21.8, \"unit\": unit}"
]
},
{
"cell_type": "markdown",
"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 👇"
],
"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",
Expand Down Expand Up @@ -385,6 +375,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "c-DurWKOOSOk"
},
"outputs": [],
"source": [
"\n",
"weather = get_current_weather(**res[\"replies\"][0].content)\n",
Expand All @@ -393,42 +388,42 @@
"\n",
"res = gemini_chat.run(messages = messages)\n",
"res[\"replies\"][0].content"
],
"metadata": {
"id": "c-DurWKOOSOk"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"source": [
"Seems like the weather is nice and sunny, remember to put on your sunglasses. 😎"
],
"metadata": {
"id": "YsiwN7dbOwSW"
}
},
"source": [
"Seems like the weather is nice and sunny, remember to put on your sunglasses. 😎"
]
},
{
"cell_type": "markdown",
"source": [
"## Build a full Retrieval-Augmented Generation Pipeline with `gemini-pro-vision`"
],
"metadata": {
"id": "Pi37EVlDenPw"
}
},
"source": [
"## Build a full Retrieval-Augmented Generation Pipeline with `gemini-pro-vision`"
]
},
{
"cell_type": "markdown",
"source": [
"As a final exercise, let's add the `GeminiGenerator` to a full RAG pipeline. In the example below, we are building a RAG pipeline that does question answering on the web, using `gemini-pro-vision`"
],
"metadata": {
"id": "fQz9_N46hniU"
}
},
"source": [
"As a final exercise, let's add the `GeminiGenerator` to a full RAG pipeline. In the example below, we are building a RAG pipeline that does question answering on the web, using `gemini-pro-vision`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "43QF7y1PhRQn"
},
"outputs": [],
"source": [
"from haystack.components.fetchers.link_content import LinkContentFetcher\n",
"from haystack.components.converters import HTMLToDocument\n",
Expand Down Expand Up @@ -468,24 +463,24 @@
"pipeline.connect(\"splitter.documents\", \"ranker.documents\")\n",
"pipeline.connect(\"ranker.documents\", \"prompt_builder.documents\")\n",
"pipeline.connect(\"prompt_builder.prompt\", \"gemini\")"
],
"metadata": {
"id": "43QF7y1PhRQn"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"source": [
"Let's try asking Gemini to tell us about Haystack 2.0 and how to use it."
],
"metadata": {
"id": "cGqt94B0fZi1"
}
},
"source": [
"Let's try asking Gemini to tell us about Haystack 2.0 and how to use it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "EhEx8xO7jMf9"
},
"outputs": [],
"source": [
"question = \"What do graphs have to do with Haystack?\"\n",
"result = pipeline.run({\"prompt_builder\": {\"question\": question},\n",
Expand All @@ -494,27 +489,26 @@
"\n",
"for answer in result[\"gemini\"][\"answers\"]:\n",
" print(answer)"
],
"metadata": {
"id": "EhEx8xO7jMf9"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xk2SAGT_l25K"
},
"source": [
"Now you've seen some of what Gemini can do, as well as how to integrate it with Haystack 2.0. If you want to learn more:\n",
"- check out the Haystack [docs](https://docs.haystack.deepset.ai/v2.0/docs) or [tutorials](https://haystack.deepset.ai/tutorials)\n",
"- Try out the [Gemini quickstart colab from Google](https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/tutorials/python_quickstart.ipynb#scrollTo=IqFXdgDFRvlU)\n",
"- Participate in the [Advent of Haystack](https://haystack.deepset.ai/advent-of-haystack)"
],
"metadata": {
"id": "xk2SAGT_l25K"
}
]
}
],
"metadata": {
"colab": {
"include_colab_link": true,
"provenance": []
},
"kernelspec": {
"display_name": "google-vertex-haystack",
"language": "python",
Expand All @@ -531,12 +525,8 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
}