Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
tcapelle committed Jun 3, 2024
1 parent 16927b4 commit 8ec6a15
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 130 deletions.
153 changes: 38 additions & 115 deletions colabs/gemini/How_to_use_Gemini_Pro_API_with_WB_Weave.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"cells": [
{
"cell_type": "markdown",
"id": "c5eb1db5-a89e-4bba-8a4c-078b647be1b7",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/wandb/examples/blob/master/colabs/gemini/How_to_use_Gemini_Pro_API_with_WB_Weave.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
Expand All @@ -12,9 +11,7 @@
{
"cell_type": "markdown",
"id": "_7u7Ft1-GXrK",
"metadata": {
"id": "_7u7Ft1-GXrK"
},
"metadata": {},
"source": [
"# How to use Gemini Pro API with W&B Weave\n",
"\n",
Expand All @@ -24,9 +21,7 @@
{
"cell_type": "markdown",
"id": "Nk3L-jqbGsrN",
"metadata": {
"id": "Nk3L-jqbGsrN"
},
"metadata": {},
"source": [
"## Installation"
]
Expand All @@ -35,9 +30,7 @@
"cell_type": "code",
"execution_count": null,
"id": "4fdc0c0e-bc69-41ed-9b09-ec469a3a781b",
"metadata": {
"id": "4fdc0c0e-bc69-41ed-9b09-ec469a3a781b"
},
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
Expand All @@ -48,9 +41,7 @@
"cell_type": "code",
"execution_count": null,
"id": "fiI5pF6WGyd_",
"metadata": {
"id": "fiI5pF6WGyd_"
},
"metadata": {},
"outputs": [],
"source": [
"import google.generativeai as genai\n",
Expand All @@ -60,9 +51,7 @@
{
"cell_type": "markdown",
"id": "Avj5TxGjFhE9",
"metadata": {
"id": "Avj5TxGjFhE9"
},
"metadata": {},
"source": [
"## Set up your Google API key and log into W&B Weave\n",
"\n",
Expand All @@ -73,9 +62,7 @@
"cell_type": "code",
"execution_count": null,
"id": "d0Lv-deDFgLl",
"metadata": {
"id": "d0Lv-deDFgLl"
},
"metadata": {},
"outputs": [],
"source": [
"from google.colab import userdata\n",
Expand All @@ -87,9 +74,7 @@
"cell_type": "code",
"execution_count": null,
"id": "894d8781-bea4-4f74-b1cd-b893f91bfee6",
"metadata": {
"id": "894d8781-bea4-4f74-b1cd-b893f91bfee6"
},
"metadata": {},
"outputs": [],
"source": [
"weave.init('prompt-eng/gemini-weave')"
Expand All @@ -98,9 +83,7 @@
{
"cell_type": "markdown",
"id": "wFiQm3JAJpvw",
"metadata": {
"id": "wFiQm3JAJpvw"
},
"metadata": {},
"source": [
"## Generate a summary and track it in Weave"
]
Expand All @@ -109,9 +92,7 @@
"cell_type": "code",
"execution_count": null,
"id": "82d096fc-309a-4c57-a3a8-926d88a520d3",
"metadata": {
"id": "82d096fc-309a-4c57-a3a8-926d88a520d3"
},
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
Expand All @@ -124,9 +105,7 @@
"cell_type": "code",
"execution_count": null,
"id": "3343fcb9-b96d-478d-a4a8-fbb1e7b27a34",
"metadata": {
"id": "3343fcb9-b96d-478d-a4a8-fbb1e7b27a34"
},
"metadata": {},
"outputs": [],
"source": [
"model_info = genai.get_model('models/gemini-1.5-pro-latest')\n",
Expand All @@ -137,9 +116,7 @@
"cell_type": "code",
"execution_count": null,
"id": "03216cd4-9fca-4dcb-aa73-b250cda7ef26",
"metadata": {
"id": "03216cd4-9fca-4dcb-aa73-b250cda7ef26"
},
"metadata": {},
"outputs": [],
"source": [
"model = genai.GenerativeModel('models/gemini-1.5-pro-latest')\n",
Expand All @@ -150,9 +127,7 @@
"cell_type": "code",
"execution_count": null,
"id": "ba32823a-f4f1-4233-8389-590e8757e85e",
"metadata": {
"id": "ba32823a-f4f1-4233-8389-590e8757e85e"
},
"metadata": {},
"outputs": [],
"source": [
"@weave.op()\n",
Expand All @@ -168,9 +143,7 @@
"cell_type": "code",
"execution_count": null,
"id": "9128199a-e13f-4d7e-b26c-55267ab8e161",
"metadata": {
"id": "9128199a-e13f-4d7e-b26c-55267ab8e161"
},
"metadata": {},
"outputs": [],
"source": [
"summary = generate_summary(long_paper_text)"
Expand All @@ -179,9 +152,7 @@
{
"cell_type": "markdown",
"id": "ur0vXGNtJ5IF",
"metadata": {
"id": "ur0vXGNtJ5IF"
},
"metadata": {},
"source": [
"## Gemini API JSON Mode"
]
Expand All @@ -190,9 +161,7 @@
"cell_type": "code",
"execution_count": null,
"id": "4de6d4b7-f567-4070-b9d5-1867b72fc045",
"metadata": {
"id": "4de6d4b7-f567-4070-b9d5-1867b72fc045"
},
"metadata": {},
"outputs": [],
"source": [
"model = genai.GenerativeModel(\"gemini-1.5-pro-latest\",\n",
Expand All @@ -203,9 +172,7 @@
"cell_type": "code",
"execution_count": null,
"id": "c403ccf8-58fd-4e8a-b951-91e7aa39022c",
"metadata": {
"id": "c403ccf8-58fd-4e8a-b951-91e7aa39022c"
},
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field\n",
Expand All @@ -222,9 +189,7 @@
"cell_type": "code",
"execution_count": null,
"id": "9a49b8de-e40d-4dc5-8e79-97af5efb1797",
"metadata": {
"id": "9a49b8de-e40d-4dc5-8e79-97af5efb1797"
},
"metadata": {},
"outputs": [],
"source": [
"import json"
Expand All @@ -234,9 +199,7 @@
"cell_type": "code",
"execution_count": null,
"id": "10b19a45-c4a9-47be-abda-97607f0e4431",
"metadata": {
"id": "10b19a45-c4a9-47be-abda-97607f0e4431"
},
"metadata": {},
"outputs": [],
"source": [
"@weave.op()\n",
Expand Down Expand Up @@ -268,9 +231,7 @@
"cell_type": "code",
"execution_count": null,
"id": "b8b03f96-f04b-44b6-b03c-0065243408d3",
"metadata": {
"id": "b8b03f96-f04b-44b6-b03c-0065243408d3"
},
"metadata": {},
"outputs": [],
"source": [
"new_summary = generate_summary(long_paper_text, schema)"
Expand All @@ -279,9 +240,7 @@
{
"cell_type": "markdown",
"id": "EFuMHhC5J8NO",
"metadata": {
"id": "EFuMHhC5J8NO"
},
"metadata": {},
"source": [
"## Evaluation with Weave"
]
Expand All @@ -290,9 +249,7 @@
"cell_type": "code",
"execution_count": null,
"id": "65abebe7-a0f4-4a7a-bdae-888d67e9403f",
"metadata": {
"id": "65abebe7-a0f4-4a7a-bdae-888d67e9403f"
},
"metadata": {},
"outputs": [],
"source": [
"from pydantic import model_validator\n",
Expand Down Expand Up @@ -333,9 +290,7 @@
"cell_type": "code",
"execution_count": null,
"id": "4b8343cb-4a54-4fea-b2b0-b2c21b39e061",
"metadata": {
"id": "4b8343cb-4a54-4fea-b2b0-b2c21b39e061"
},
"metadata": {},
"outputs": [],
"source": [
"prompt_template = \"\"\"Generate a concise summary of below text using below JSON schema.\n",
Expand All @@ -351,9 +306,7 @@
"cell_type": "code",
"execution_count": null,
"id": "d69ed0c0-e032-4ced-8fd8-919ee332a95f",
"metadata": {
"id": "d69ed0c0-e032-4ced-8fd8-919ee332a95f"
},
"metadata": {},
"outputs": [],
"source": [
"model = SummaryModel(model_name='gemini-1.5-pro-latest', prompt_template=prompt_template, \\\n",
Expand All @@ -364,9 +317,7 @@
"cell_type": "code",
"execution_count": null,
"id": "3d78ca87-bd61-45e6-b62c-0b82754d2b79",
"metadata": {
"id": "3d78ca87-bd61-45e6-b62c-0b82754d2b79"
},
"metadata": {},
"outputs": [],
"source": [
"await model.predict(long_paper_text)"
Expand All @@ -376,9 +327,7 @@
"cell_type": "code",
"execution_count": null,
"id": "dca5e402-0805-4888-8f08-0b28748fea11",
"metadata": {
"id": "dca5e402-0805-4888-8f08-0b28748fea11"
},
"metadata": {},
"outputs": [],
"source": [
"dataset_uri = \"weave:///prompt-eng/gemini-weave/object/long_papers:9N9vkE4XY1SYoXLbvbCtP0YKqyqXErilG4XW8jYmQgE\"\n",
Expand All @@ -389,9 +338,7 @@
"cell_type": "code",
"execution_count": null,
"id": "de2f8804-dfd7-49b1-adbe-0a3dbb0c5ff1",
"metadata": {
"id": "de2f8804-dfd7-49b1-adbe-0a3dbb0c5ff1"
},
"metadata": {},
"outputs": [],
"source": [
"# Scoring function checking format adherence\n",
Expand All @@ -412,9 +359,7 @@
"cell_type": "code",
"execution_count": null,
"id": "yfF8_ZsRMhYe",
"metadata": {
"id": "yfF8_ZsRMhYe"
},
"metadata": {},
"outputs": [],
"source": [
"# Scoring function checking length of summary\n",
Expand All @@ -436,9 +381,7 @@
"cell_type": "code",
"execution_count": null,
"id": "1d1c6bdb-83f9-4a63-99c4-0b2f0d3e9139",
"metadata": {
"id": "1d1c6bdb-83f9-4a63-99c4-0b2f0d3e9139"
},
"metadata": {},
"outputs": [],
"source": [
"evaluation = weave.Evaluation(\n",
Expand All @@ -450,9 +393,7 @@
"cell_type": "code",
"execution_count": null,
"id": "fa5b2822-55cf-4983-a52f-10314ca79c0a",
"metadata": {
"id": "fa5b2822-55cf-4983-a52f-10314ca79c0a"
},
"metadata": {},
"outputs": [],
"source": [
"await evaluation.evaluate(model)"
Expand All @@ -462,63 +403,45 @@
"cell_type": "code",
"execution_count": null,
"id": "ba244582",
"metadata": {
"id": "ba244582"
},
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c39dc979",
"metadata": {
"id": "c39dc979"
},
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d2f8124",
"metadata": {
"id": "8d2f8124"
},
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3012c72",
"metadata": {
"id": "f3012c72"
},
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"provenance": []
"include_colab_link": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 8ec6a15

Please sign in to comment.