Skip to content

Commit

Permalink
Sync notebooks from internal
Browse files Browse the repository at this point in the history
Last site change was 591064944.
  • Loading branch information
markmcd committed Dec 15, 2023
1 parent 98bc0ea commit 89a05f9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 78 deletions.
132 changes: 66 additions & 66 deletions site/en/docs/semantic_retriever.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=https://ai.google.dev/docs/semantic_retriever\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />View on Generative AI</a>\n",
" <a target=\"_blank\" href=\"https://ai.google.dev/docs/semantic_retriever\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />View on Generative AI</a>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/docs/semantic_retriever.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\" https://github.com/google/generative-ai-docs/blob/main/site/en/docs/semantic_retriever.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" <a target=\"_blank\" href=\"https://github.com/google/generative-ai-docs/blob/main/site/en/docs/semantic_retriever.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
"</table>"
]
Expand Down Expand Up @@ -113,22 +113,24 @@
"source": [
"## Authenticate\n",
"\n",
"The Google Semantic Retriever API lets you perform semantic search on your own data. Since it's **your data**, this needs stricter access controls than API keys. Authenticate with OAuth with your [user credentials](#setup_oauth_with_user_credentials) or through [service accounts](#setup_oauth_using_service_accounts). As a first step, [enable the Generative Language API](../tutorials/oauth_quickstart#1_enable_the_api)."
"The Google Semantic Retriever API lets you perform semantic search on your own data. Since it's **your data**, this needs stricter access controls than API keys. Authenticate with OAuth with [service accounts](#scrollTo=eLjhFIOQ7_Dk) or through your [user credentials](#scrollTo=9YGv4x9ehLba). As a first step, [enable the Generative Language API](https://ai.google.dev/docs/oauth_quickstart#enable-api)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9YGv4x9ehLba"
"id": "eLjhFIOQ7_Dk"
},
"source": [
"### Setup OAuth with user credentials\n",
"### Setup OAuth using service accounts (Preferred)\n",
"\n",
"Follow the steps below from the [OAuth Quickstart](/docs/oauth_quickstart) to setup OAuth authentication.\n",
"Follow the steps below to setup OAuth using service accounts:\n",
"\n",
"1. [Configure the OAuth consent screen](/docs/oauth_quickstart#2_configure_the_oauth_consent_screen).\n",
"1. Create the Service Account by following the [documentation](https://developers.google.com/identity/protocols/oauth2/service-account#creatinganaccount){:.external}.\n",
"\n",
"1. [Authorize credentials for a desktop application](/docs/oauth_quickstart#3_authorize_credentials_for_a_desktop_application). To run this notebook in Colab, first rename your credential file (usually `client_secret_*.json`) to just `client_secret.json`. Then upload the file by using the file icon on the left sidebar, then the upload icon, as shown in the screenshot below.\n",
" * After creating the service account, generate a service account key.\n",
"\n",
"1. Upload your service account file by using the file icon on the left sidebar, then the upload icon, as shown in the screenshot below.\n",
"\n",
"<img width=400 src=\"https://developers.generativeai.google/tutorials/images/colab_upload.png\">"
]
Expand All @@ -137,67 +139,69 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9C3X6r1dueO4"
"id": "akwWUSrp8Bx2"
},
"outputs": [],
"source": [
"# Replace TODO-your-project-name with the project used in the OAuth Quickstart\n",
"project_name = \"TODO-your-project-name\" # @param {type:\"string\"}\n",
"# Replace [email protected] with the email added as a test user in the OAuth Quickstart\n",
"email = \"[email protected]\" # @param {type:\"string\"}\n",
"# Rename the uploaded file to `client_secret.json` OR\n",
"# Change the variable `client_file_name` in the code below.\n",
"client_file_name = \"client_secret.json\"\n",
"!pip install -U google-auth-oauthlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2jZmqVCj8FKa"
},
"outputs": [],
"source": [
"# Rename the uploaded file to `service_account_key.json` OR\n",
"# Change the variable `service_account_file_name` in the code below.\n",
"service_account_file_name = 'service_account_key.json'\n",
"\n",
"# IMPORTANT: Follow the instructions from the output - you must copy the command\n",
"# to your terminal and copy the output after authentication back here.\n",
"!gcloud config set project $project_name\n",
"!gcloud config set account $email\n",
"from google.oauth2 import service_account\n",
"\n",
"# NOTE: The simplified project setup in this tutorial triggers a \"Google hasn't verified this app.\" dialog.\n",
"# This is normal, click \"Advanced\" -> \"Go to [app name] (unsafe)\"\n",
"!gcloud auth application-default login --no-browser --client-id-file=$client_file_name --scopes=\"https://www.googleapis.com/auth/generative-language.retriever,https://www.googleapis.com/auth/cloud-platform\""
"credentials = service_account.Credentials.from_service_account_file(service_account_file_name)\n",
"\n",
"scoped_credentials = credentials.with_scopes(\n",
" ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/generative-language.retriever'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lybTOlN9gz0I"
"id": "Qqjs2tvvicqq"
},
"source": [
"Initialize the client library."
"Initialize the client library using the service account credentials."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9MJbDTpMmlKJ"
"id": "P719DMtK8t-p"
},
"outputs": [],
"source": [
"import google.ai.generativelanguage as glm\n",
"\n",
"generative_service_client = glm.GenerativeServiceClient()\n",
"retriever_service_client = glm.RetrieverServiceClient()\n",
"permission_service_client = glm.PermissionServiceClient()"
"generative_service_client = glm.GenerativeServiceClient(credentials=scoped_credentials)\n",
"retriever_service_client = glm.RetrieverServiceClient(credentials=scoped_credentials)\n",
"permission_service_client = glm.PermissionServiceClient(credentials=scoped_credentials)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eLjhFIOQ7_Dk"
"id": "9YGv4x9ehLba"
},
"source": [
"### Setup OAuth using service accounts\n",
"### Setup OAuth with user credentials\n",
"\n",
"Optionally, you can use OAuth using service accounts:\n",
"Follow the steps below from the [OAuth Quickstart](https://ai.google.dev/docs/oauth_quickstart) to setup OAuth authentication.\n",
"\n",
"1. Create the Service Account by following the [documentation](https://developers.google.com/identity/protocols/oauth2/service-account#creatinganaccount){:.external}.\n",
"1. [Configure the OAuth consent screen](https://ai.google.dev/docs/oauth_quickstart#configure-oauth).\n",
"\n",
" * After creating the service account, generate a service account key.\n",
"\n",
"1. Upload your service account file by using the file icon on the left sidebar, then the upload icon, as shown in the screenshot below.\n",
"1. [Authorize credentials for a desktop application](https://ai.google.dev/docs/oauth_quickstart#authorize-credentials). To run this notebook in Colab, first rename your credential file (usually `client_secret_*.json`) to just `client_secret.json`. Then upload the file by using the file icon on the left sidebar, then the upload icon, as shown in the screenshot below.\n",
"\n",
"<img width=400 src=\"https://developers.generativeai.google/tutorials/images/colab_upload.png\">"
]
Expand All @@ -206,54 +210,50 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "akwWUSrp8Bx2"
},
"outputs": [],
"source": [
"!pip install -U google-auth-oauthlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2jZmqVCj8FKa"
"id": "9C3X6r1dueO4"
},
"outputs": [],
"source": [
"# Rename the uploaded file to `service_account_secret.json` OR\n",
"# Change the variable `service_account_file_name` in the code below.\n",
"service_account_file_name = 'service_account_secret.json'\n",
"\n",
"from google.oauth2 import service_account\n",
"# Replace TODO-your-project-name with the project used in the OAuth Quickstart\n",
"project_name = \"TODO-your-project-name\" # @param {type:\"string\"}\n",
"# Replace [email protected] with the email added as a test user in the OAuth Quickstart\n",
"email = \"[email protected]\" # @param {type:\"string\"}\n",
"# Rename the uploaded file to `client_secret.json` OR\n",
"# Change the variable `client_file_name` in the code below.\n",
"client_file_name = \"client_secret.json\"\n",
"\n",
"credentials = service_account.Credentials.from_service_account_file(service_account_file_name)\n",
"# IMPORTANT: Follow the instructions from the output - you must copy the command\n",
"# to your terminal and copy the output after authentication back here.\n",
"!gcloud config set project $project_name\n",
"!gcloud config set account $email\n",
"\n",
"scoped_credentials = credentials.with_scopes(\n",
" ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/generative-language.retriever'])"
"# NOTE: The simplified project setup in this tutorial triggers a \"Google hasn't verified this app.\" dialog.\n",
"# This is normal, click \"Advanced\" -> \"Go to [app name] (unsafe)\"\n",
"!gcloud auth application-default login --no-browser --client-id-file=$client_file_name --scopes=\"https://www.googleapis.com/auth/generative-language.retriever,https://www.googleapis.com/auth/cloud-platform\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Qqjs2tvvicqq"
"id": "lybTOlN9gz0I"
},
"source": [
"Initialize the client library using the service account credentials."
"Initialize the client library."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "P719DMtK8t-p"
"id": "9MJbDTpMmlKJ"
},
"outputs": [],
"source": [
"import google.ai.generativelanguage as glm\n",
"generative_service_client = glm.GenerativeServiceClient(credentials=scoped_credentials)\n",
"retriever_service_client = glm.RetrieverServiceClient(credentials=scoped_credentials)\n",
"permission_service_client = glm.PermissionServiceClient(credentials=scoped_credentials)"
"\n",
"generative_service_client = glm.GenerativeServiceClient()\n",
"retriever_service_client = glm.RetrieverServiceClient()\n",
"permission_service_client = glm.PermissionServiceClient()"
]
},
{
Expand Down Expand Up @@ -796,7 +796,7 @@
"\n",
"When `answerable_probability` is low, some clients may wish to:\n",
"* Display a message to the effect of \"We couldn't answer that question\" to the end user.\n",
"* Fall back to a general-purpose LLM that answers the question from world knowledge. The threshold and nature of such fallbacks will depend on individual use cases. A value of `answerable_probablitity` <= 0.5 is a good starting threshold.\n"
"* Fall back to a general-purpose LLM that answers the question from world knowledge. The threshold and nature of such fallbacks will depend on individual use cases. A value of `answerable_probability` <= 0.5 is a good starting threshold.\n"
]
},
{
Expand Down Expand Up @@ -829,7 +829,7 @@
"outputs": [],
"source": [
"user_query = \"What is the purpose of Project IDX?\"\n",
"answer_style = \"ABSTRACTIVE\"\n",
"answer_style = \"ABSTRACTIVE\" # Or VERBOSE, EXTRACTIVE\n",
"MODEL_NAME = \"models/aqa\"\n",
"\n",
"# Make the request\n",
Expand Down Expand Up @@ -879,7 +879,7 @@
"source": [
"user_query = \"What is AQA from Google?\"\n",
"user_query_content = glm.Content(parts=[glm.Part(text=user_query)])\n",
"answer_style = \"ABSTRACTIVE\"\n",
"answer_style = \"VERBOSE\" # or ABSTRACTIVE, EXTRACTIVE\n",
"MODEL_NAME = \"models/aqa\"\n",
"\n",
"# Create the grounding inline passages\n",
Expand Down
19 changes: 12 additions & 7 deletions site/en/tutorials/python_quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@
"id": "FTl5NjtrhA0J"
},
"source": [
"Note: For detailed information about the available models, including their capabilities and rate limits, see [Gemini models](https://ai.google.dev/models/gemini). We offer options for requesting [rate limit increases](https://ai.google.dev/docs/increase_quota). The rate limit for Gemini-Pro models is 60 requests per minute (RPM).\n",
"\n",
"The `genai` package also supports the PaLM family of models, but only the Gemini models support the generic, multimodal capabilities of the `generateContent` method."
]
},
Expand Down Expand Up @@ -1038,7 +1040,7 @@
"id": "BpHIRU5bj7aW"
},
"source": [
"[Embedding](https://developers.google.com/machine-learning/glossary#embedding-vector) is a technique used to represent information as a list of floating point numbers in an array. With Gemini, you can represent text (words, sentences, and blocks of text) in a vectorized form, making it easier to compare and contrast embeddings. For example, two texts that share a similar subject matter or sentiment should have similar embeddings, which can be identified through mathematical comparison techniques such as cosine similarity. For more on how and why you should use embeddings, refer to the [Embeddings guide](/docs/embeddings_guide.md).\n",
"[Embedding](https://developers.google.com/machine-learning/glossary#embedding-vector) is a technique used to represent information as a list of floating point numbers in an array. With Gemini, you can represent text (words, sentences, and blocks of text) in a vectorized form, making it easier to compare and contrast embeddings. For example, two texts that share a similar subject matter or sentiment should have similar embeddings, which can be identified through mathematical comparison techniques such as cosine similarity. For more on how and why you should use embeddings, refer to the [Embeddings guide](https://ai.google.dev/docs/embeddings_guide).\n",
"\n",
"Use the `embed_content` method to generate embeddings. The method handles embedding for the following tasks (`task_type`):\n",
"\n",
Expand Down Expand Up @@ -1128,9 +1130,9 @@
"id": "zBg0eNeml3d4"
},
"source": [
"While the `genai.embedd_content` function accepts simple strings or lists of strings, it is actually built around the `glm.Content` type (like `GemerativeModel.generate_content`). `glm.Content` objects are the primary units of conversation in the API.\n",
"While the `genai.embed_content` function accepts simple strings or lists of strings, it is actually built around the `glm.Content` type (like `GenerativeModel.generate_content`). `glm.Content` objects are the primary units of conversation in the API.\n",
"\n",
"While the `glm.Content` object is multimodal, the `embedd_content` method only supports text embeddings. This design gives the API the *possibility* to expand to multimodal embeddings."
"While the `glm.Content` object is multimodal, the `embed_content` method only supports text embeddings. This design gives the API the *possibility* to expand to multimodal embeddings."
]
},
{
Expand Down Expand Up @@ -1262,7 +1264,9 @@
"id": "vuz9-TWDzdlb"
},
"source": [
"## Advanced use cases"
"## Advanced use cases\n",
"\n",
"The following sections discuss advanced use cases and lower-level details of the Python SDK for the Gemini API."
]
},
{
Expand All @@ -1273,7 +1277,7 @@
"source": [
"### Safety settings\n",
"\n",
"The `safety_settings` argument lets you configure what the model blocks and allows in both prompts and responses. By default, safety settings block content with medium and/or high probability of being unsafe content across all dimensions. Learn more about [Safety settings](/docs/safety_setting).\n",
"The `safety_settings` argument lets you configure what the model blocks and allows in both prompts and responses. By default, safety settings block content with medium and/or high probability of being unsafe content across all dimensions. Learn more about [Safety settings](https://ai.google.dev/docs/safety_setting).\n",
"\n",
"Enter a questionable prompt and run the model with the default safety settings, and it will not return any candidates:"
]
Expand Down Expand Up @@ -1702,8 +1706,9 @@
"source": [
"## What's next\n",
"\n",
"- Prompt design is the process of creating prompts that elicit the desired response from language models. Writing well structured prompts is an essential part of ensuring accurate, high quality responses from a language model. Learn about best practices for [prompt writing](/docs/prompt_best_practices.md).\n",
"- Gemini offers several model variations to meet the needs of different use cases, such as input types and complexity, implementations for chat or other dialog language tasks, and size constraints. Learn about the available [Gemini models](/models/gemini.md)."
"- Prompt design is the process of creating prompts that elicit the desired response from language models. Writing well structured prompts is an essential part of ensuring accurate, high quality responses from a language model. Learn about best practices for [prompt writing](https://ai.google.dev/docs/prompt_best_practices).\n",
"- Gemini offers several model variations to meet the needs of different use cases, such as input types and complexity, implementations for chat or other dialog language tasks, and size constraints. Learn about the available [Gemini models](https://ai.google.dev/models/gemini).\n",
"- Gemini offers options for requesting [rate limit increases](https://ai.google.dev/docs/increase_quota). The rate limit for Gemini-Pro models is 60 requests per minute (RPM)."
]
}
],
Expand Down
Loading

0 comments on commit 89a05f9

Please sign in to comment.