CINXE.COM
Generating content | Gemini API | Google AI for Developers
<!doctype html> <html lang="en" dir="ltr"> <head> <meta name="google-signin-client-id" content="157101835696-ooapojlodmuabs2do2vuhhnf90bccmoi.apps.googleusercontent.com"> <meta name="google-signin-scope" content="profile email https://www.googleapis.com/auth/developerprofiles https://www.googleapis.com/auth/developerprofiles.award"> <meta property="og:site_name" content="Google AI for Developers"> <meta property="og:type" content="website"><meta name="theme-color" content="#1967d2"><meta charset="utf-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="manifest" href="/_pwa/googledevai/manifest.json" crossorigin="use-credentials"> <link rel="preconnect" href="//www.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.googleapis.com" crossorigin> <link rel="preconnect" href="//apis.google.com" crossorigin> <link rel="preconnect" href="//www.google-analytics.com" crossorigin><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"> <link rel="stylesheet" href="//fonts.googleapis.com/css2?family=Material+Icons&family=Material+Symbols+Outlined&display=block"><link rel="stylesheet" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/css/app.css"> <link rel="shortcut icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/images/favicon-new.png"> <link rel="apple-touch-icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/images/touchicon-180-new.png"><link rel="canonical" href="https://ai.google.dev/api/generate-content"><link rel="search" type="application/opensearchdescription+xml" title="Google AI for Developers" href="https://ai.google.dev/s/opensearch.xml"> <link rel="alternate" hreflang="en" href="https://ai.google.dev/api/generate-content" /><link rel="alternate" hreflang="x-default" href="https://ai.google.dev/api/generate-content" /><link rel="alternate" hreflang="ar" href="https://ai.google.dev/api/generate-content?hl=ar" /><link rel="alternate" hreflang="bn" href="https://ai.google.dev/api/generate-content?hl=bn" /><link rel="alternate" hreflang="zh-Hans" href="https://ai.google.dev/api/generate-content?hl=zh-cn" /><link rel="alternate" hreflang="zh-Hant" href="https://ai.google.dev/api/generate-content?hl=zh-tw" /><link rel="alternate" hreflang="fa" href="https://ai.google.dev/api/generate-content?hl=fa" /><link rel="alternate" hreflang="fr" href="https://ai.google.dev/api/generate-content?hl=fr" /><link rel="alternate" hreflang="de" href="https://ai.google.dev/api/generate-content?hl=de" /><link rel="alternate" hreflang="he" href="https://ai.google.dev/api/generate-content?hl=he" /><link rel="alternate" hreflang="hi" href="https://ai.google.dev/api/generate-content?hl=hi" /><link rel="alternate" hreflang="id" href="https://ai.google.dev/api/generate-content?hl=id" /><link rel="alternate" hreflang="it" href="https://ai.google.dev/api/generate-content?hl=it" /><link rel="alternate" hreflang="ja" href="https://ai.google.dev/api/generate-content?hl=ja" /><link rel="alternate" hreflang="ko" href="https://ai.google.dev/api/generate-content?hl=ko" /><link rel="alternate" hreflang="pl" href="https://ai.google.dev/api/generate-content?hl=pl" /><link rel="alternate" hreflang="pt-BR" href="https://ai.google.dev/api/generate-content?hl=pt-br" /><link rel="alternate" hreflang="ru" href="https://ai.google.dev/api/generate-content?hl=ru" /><link rel="alternate" hreflang="es-419" href="https://ai.google.dev/api/generate-content?hl=es-419" /><link rel="alternate" hreflang="th" href="https://ai.google.dev/api/generate-content?hl=th" /><link rel="alternate" hreflang="tr" href="https://ai.google.dev/api/generate-content?hl=tr" /><link rel="alternate" hreflang="vi" href="https://ai.google.dev/api/generate-content?hl=vi" /><title>Generating content | Gemini API | Google AI for Developers</title> <meta property="og:title" content="Generating content | Gemini API | Google AI for Developers"><meta property="og:url" content="https://ai.google.dev/api/generate-content"><meta property="og:image" content="https://ai.google.dev/static/site-assets/images/share-gemini-api.png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="675"><meta property="og:locale" content="en"><meta name="twitter:card" content="summary_large_image"><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{ "@type": "ListItem", "position": 1, "name": "Gemini API", "item": "https://ai.google.dev/gemini-api" },{ "@type": "ListItem", "position": 2, "name": "Generating content", "item": "https://ai.google.dev/api/generate-content" }] } </script> <link rel="stylesheet" href="/extras.css"></head> <body class="docs" template="page" theme="googledevai-theme" type="reference" layout="docs" display-toc pending> <devsite-progress type="indeterminate" id="app-progress"></devsite-progress> <section class="devsite-wrapper"> <devsite-cookie-notification-bar></devsite-cookie-notification-bar><devsite-header role="banner" keep-tabs-visible> <div class="devsite-header--inner nocontent"> <div class="devsite-top-logo-row-wrapper-wrapper"> <div class="devsite-top-logo-row-wrapper"> <div class="devsite-top-logo-row"> <button type="button" id="devsite-hamburger-menu" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Navigation menu button" visually-hidden aria-label="Open menu"> </button> <div class="devsite-product-name-wrapper"> <a href="/" class="devsite-site-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Site logo" track-type="globalNav" track-name="geminiAPI" track-metadata-position="nav" track-metadata-eventDetail="nav"> <picture> <img src="https://ai.google.dev/_static/googledevai/images/lockup-new.svg" class="devsite-site-logo" alt="Gemini API"> </picture> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> </li> </ul> </span> </div> <div class="devsite-top-logo-row-middle"> <div class="devsite-header-upper-tabs"> <devsite-tabs class="upper-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Upper tabs"> <tab class="devsite-active"> <a href="https://ai.google.dev/gemini-api/docs" track-metadata-eventdetail="https://ai.google.dev/gemini-api/docs" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - gemini api" track-metadata-module="primary nav" aria-label="Gemini API, selected" data-category="Site-Wide Custom Events" data-label="Tab: Gemini API" track-name="gemini api" > Gemini API </a> </tab> <tab > <a href="https://aistudio.google.com" track-metadata-eventdetail="https://aistudio.google.com" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - google ai studio" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Google AI Studio" track-name="google ai studio" > Google AI Studio </a> </tab> <tab > <a href="https://ai.google.dev/gemma" track-metadata-eventdetail="https://ai.google.dev/gemma" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - gemma" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Gemma" track-name="gemma" > Gemma </a> </tab> <tab > <a href="https://ai.google.dev/edge" track-metadata-eventdetail="https://ai.google.dev/edge" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - google ai edge" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Google AI Edge" track-name="google ai edge" > Google AI Edge </a> </tab> <tab class="devsite-dropdown "> <span class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - tools" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Tools" track-name="tools" > Tools </span> <a href="#" role="button" aria-haspopup="true" aria-expanded="false" aria-label="Dropdown menu for Tools" track-type="nav" track-metadata-eventdetail="" track-metadata-position="nav - tools" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Tools" track-name="tools" class="devsite-tabs-dropdown-toggle devsite-icon devsite-icon-arrow-drop-down"></a> <div class="devsite-tabs-dropdown" aria-label="submenu" hidden> <div class="devsite-tabs-dropdown-content"> <div class="devsite-tabs-dropdown-column "> <ul class="devsite-tabs-dropdown-section "> <li class="devsite-nav-item"> <a href="https://ai.google.dev/responsible" track-type="nav" track-metadata-eventdetail="https://ai.google.dev/responsible" track-metadata-position="nav - tools" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Responsible GenAI Toolkit </div> </a> </li> <li class="devsite-nav-item"> <a href="https://colab.google/" track-type="nav" track-metadata-eventdetail="https://colab.google/" track-metadata-position="nav - tools" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Colab </div> </a> </li> <li class="devsite-nav-item"> <a href="https://firebase.google.com/docs/genkit" track-type="nav" track-metadata-eventdetail="https://firebase.google.com/docs/genkit" track-metadata-position="nav - tools" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Firebase Genkit </div> </a> </li> <li class="devsite-nav-item"> <a href="https://jax.readthedocs.io/" track-type="nav" track-metadata-eventdetail="https://jax.readthedocs.io/" track-metadata-position="nav - tools" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> JAX </div> </a> </li> <li class="devsite-nav-item"> <a href="https://keras.io/" track-type="nav" track-metadata-eventdetail="https://keras.io/" track-metadata-position="nav - tools" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Keras </div> </a> </li> <li class="devsite-nav-item"> <a href="https://www.tensorflow.org/" track-type="nav" track-metadata-eventdetail="https://www.tensorflow.org/" track-metadata-position="nav - tools" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> TensorFlow </div> </a> </li> </ul> </div> </div> </div> </tab> <tab > <a href="https://discuss.ai.google.dev/" track-metadata-eventdetail="https://discuss.ai.google.dev/" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - community" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Community" track-name="community" > Community </a> </tab> </nav> </devsite-tabs> </div> <devsite-search enable-signin enable-search enable-suggestions enable-query-completion project-name="Gemini API" tenant-name="Google AI for Developers" > <form class="devsite-search-form" action="https://ai.google.dev/s/results" method="GET"> <div class="devsite-search-container"> <button type="button" search-open class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Open search"></button> <div class="devsite-searchbox"> <input aria-activedescendant="" aria-autocomplete="list" aria-label="Search" aria-expanded="false" aria-haspopup="listbox" autocomplete="off" class="devsite-search-field devsite-search-query" name="q" placeholder="Search" role="combobox" type="text" value="" > <div class="devsite-search-image material-icons" aria-hidden="true"> </div> <div class="devsite-search-shortcut-icon-container" aria-hidden="true"> <kbd class="devsite-search-shortcut-icon">/</kbd> </div> </div> </div> </form> <button type="button" search-close class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Close search"></button> </devsite-search> </div> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pl" >Polski</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="ru" >Русский</a> </li> <li role="presentation"> <a role="menuitem" lang="he" >עברית</a> </li> <li role="presentation"> <a role="menuitem" lang="ar" >العربيّة</a> </li> <li role="presentation"> <a role="menuitem" lang="fa" >فارسی</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="bn" >বাংলা</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> <devsite-user enable-profiles id="devsite-user"> <span class="button devsite-top-button" aria-hidden="true" visually-hidden>Sign in</span> </devsite-user> </div> </div> </div> <div class="devsite-collapsible-section "> <div class="devsite-header-background"> <div class="devsite-doc-set-nav-row"> <devsite-tabs class="lower-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Lower tabs"> <tab > <a href="https://ai.google.dev/gemini-api/docs" track-metadata-eventdetail="https://ai.google.dev/gemini-api/docs" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - docs" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Docs" track-name="docs" > Docs </a> </tab> <tab class="devsite-active"> <a href="https://ai.google.dev/api" track-metadata-eventdetail="https://ai.google.dev/api" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - api reference" track-metadata-module="primary nav" aria-label="API Reference, selected" data-category="Site-Wide Custom Events" data-label="Tab: API Reference" track-name="api reference" > API Reference </a> </tab> <tab > <a href="https://ai.google.dev/pricing" track-metadata-eventdetail="https://ai.google.dev/pricing" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - pricing" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Pricing" track-name="pricing" > Pricing </a> </tab> </nav> </devsite-tabs> </div> </div> </div> </div> </devsite-header> <devsite-book-nav scrollbars > <div class="devsite-book-nav-filter" hidden> <span class="filter-list-icon material-icons" aria-hidden="true"></span> <input type="text" placeholder="Filter" aria-label="Type to filter" role="searchbox"> <span class="filter-clear-button hidden" data-title="Clear filter" aria-label="Clear filter" role="button" tabindex="0"></span> </div> <nav class="devsite-book-nav devsite-nav nocontent" aria-label="Side menu"> <div class="devsite-mobile-header"> <button type="button" id="devsite-close-nav" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close navigation" aria-label="Close navigation"> </button> <div class="devsite-product-name-wrapper"> <a href="/" class="devsite-site-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Site logo" track-type="globalNav" track-name="geminiAPI" track-metadata-position="nav" track-metadata-eventDetail="nav"> <picture> <img src="https://ai.google.dev/_static/googledevai/images/lockup-new.svg" class="devsite-site-logo" alt="Gemini API"> </picture> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> </li> </ul> </span> </div> </div> <div class="devsite-book-nav-wrapper"> <div class="devsite-mobile-nav-top"> <ul class="devsite-nav-list"> <li class="devsite-nav-item"> <a href="/gemini-api/docs" class="devsite-nav-title gc-analytics-event devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: Gemini API" track-name="gemini api" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Gemini API" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Gemini API </span> </a> <ul class="devsite-nav-responsive-tabs"> <li class="devsite-nav-item"> <a href="/gemini-api/docs" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Docs" track-name="docs" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Docs" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Docs </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/api" class="devsite-nav-title gc-analytics-event devsite-nav-has-children devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: API Reference" track-name="api reference" data-category="Site-Wide Custom Events" data-label="Responsive Tab: API Reference" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip menu="_book"> API Reference </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="_book"> </span> </a> </li> <li class="devsite-nav-item"> <a href="/pricing" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Pricing" track-name="pricing" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Pricing" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Pricing </span> </a> </li> </ul> </li> <li class="devsite-nav-item"> <a href="https://aistudio.google.com" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Google AI Studio" track-name="google ai studio" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google AI Studio" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google AI Studio </span> </a> </li> <li class="devsite-nav-item"> <a href="/gemma" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Gemma" track-name="gemma" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Gemma" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Gemma </span> </a> </li> <li class="devsite-nav-item"> <a href="/edge" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Google AI Edge" track-name="google ai edge" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google AI Edge" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google AI Edge </span> </a> </li> <li class="devsite-nav-item"> <span class="devsite-nav-title" tooltip data-category="Site-Wide Custom Events" data-label="Tab: Tools" track-name="tools" > <span class="devsite-nav-text" tooltip > Tools </span> </span> <ul class="devsite-nav-responsive-tabs devsite-nav-has-menu "> <li class="devsite-nav-item"> <span class="devsite-nav-title" tooltip data-category="Site-Wide Custom Events" data-label="Tab: Tools" track-name="tools" > <span class="devsite-nav-text" tooltip menu="Tools"> More </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="Tools"> </span> </span> </li> </ul> </li> <li class="devsite-nav-item"> <a href="https://discuss.ai.google.dev/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Community" track-name="community" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Community" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Community </span> </a> </li> </ul> </div> <div class="devsite-mobile-nav-bottom"> <ul class="devsite-nav-list" menu="_book"> <li class="devsite-nav-item"><a href="/api" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api" ><span class="devsite-nav-text" tooltip>Overview</span></a></li> <li class="devsite-nav-item"><a href="/gemini-api/docs/api-versions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /gemini-api/docs/api-versions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/gemini-api/docs/api-versions" ><span class="devsite-nav-text" tooltip>API versions</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Capabilities</span> </div></li> <li class="devsite-nav-item"><a href="/api/models" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/models" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/models" ><span class="devsite-nav-text" tooltip>Models</span></a></li> <li class="devsite-nav-item"><a href="/api/generate-content" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/generate-content" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/generate-content" ><span class="devsite-nav-text" tooltip>Generating content</span></a></li> <li class="devsite-nav-item"><a href="/api/tokens" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/tokens" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/tokens" ><span class="devsite-nav-text" tooltip>Tokens</span></a></li> <li class="devsite-nav-item"><a href="/api/files" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/files" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/files" ><span class="devsite-nav-text" tooltip>Files</span></a></li> <li class="devsite-nav-item"><a href="/api/caching" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/caching" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/caching" ><span class="devsite-nav-text" tooltip>Caching</span></a></li> <li class="devsite-nav-item"><a href="/api/embeddings" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/embeddings" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/embeddings" ><span class="devsite-nav-text" tooltip>Embeddings</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Tuning</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/api/tuning" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/tuning" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/tuning" ><span class="devsite-nav-text" tooltip>Tuning</span></a></li><li class="devsite-nav-item"><a href="/api/tuning/permissions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/tuning/permissions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/tuning/permissions" ><span class="devsite-nav-text" tooltip>Permissions</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Semantic retrieval</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/api/semantic-retrieval/question-answering" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/semantic-retrieval/question-answering" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/semantic-retrieval/question-answering" ><span class="devsite-nav-text" tooltip>Question answering</span></a></li><li class="devsite-nav-item"><a href="/api/semantic-retrieval/corpora" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/semantic-retrieval/corpora" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/semantic-retrieval/corpora" ><span class="devsite-nav-text" tooltip>Corpus</span></a></li><li class="devsite-nav-item"><a href="/api/semantic-retrieval/documents" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/semantic-retrieval/documents" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/semantic-retrieval/documents" ><span class="devsite-nav-text" tooltip>Document</span></a></li><li class="devsite-nav-item"><a href="/api/semantic-retrieval/chunks" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/semantic-retrieval/chunks" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/semantic-retrieval/chunks" ><span class="devsite-nav-text" tooltip>Chunk</span></a></li><li class="devsite-nav-item"><a href="/api/semantic-retrieval/permissions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/semantic-retrieval/permissions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/semantic-retrieval/permissions" ><span class="devsite-nav-text" tooltip>Permissions</span></a></li></ul></div></li> <li class="devsite-nav-item"><a href="/api/all-methods" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/all-methods" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/all-methods" ><span class="devsite-nav-text" tooltip>All methods</span></a></li> <li class="devsite-nav-item devsite-nav-expandable devsite-nav-deprecated"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Deprecated</span><span class="devsite-nav-icon material-icons" data-icon="deprecated" data-title="Deprecated" aria-hidden="true"></span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/api/palm" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /api/palm" track-type="bookNav" track-name="click" track-metadata-eventdetail="/api/palm" ><span class="devsite-nav-text" tooltip>PaLM (decomissioned)</span></a></li></ul></div></li> </ul> <ul class="devsite-nav-list" menu="Tools" aria-label="Side menu" hidden> <li class="devsite-nav-item"> <a href="/responsible" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Responsible GenAI Toolkit" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Responsible GenAI Toolkit </span> </a> </li> <li class="devsite-nav-item"> <a href="https://colab.google/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Colab" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Colab </span> </a> </li> <li class="devsite-nav-item"> <a href="https://firebase.google.com/docs/genkit" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Firebase Genkit" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Firebase Genkit </span> </a> </li> <li class="devsite-nav-item"> <a href="https://jax.readthedocs.io/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: JAX" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > JAX </span> </a> </li> <li class="devsite-nav-item"> <a href="https://keras.io/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Keras" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Keras </span> </a> </li> <li class="devsite-nav-item"> <a href="https://www.tensorflow.org/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: TensorFlow" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > TensorFlow </span> </a> </li> </ul> </div> </div> </nav> </devsite-book-nav> <section id="gc-wrapper"> <main role="main" class="devsite-main-content" has-book-nav has-sidebar > <div class="devsite-sidebar"> <div class="devsite-sidebar-content"> <devsite-toc class="devsite-nav" role="navigation" aria-label="On this page" depth="2" scrollbars ></devsite-toc> <devsite-recommendations-sidebar class="nocontent devsite-nav"> </devsite-recommendations-sidebar> </div> </div> <devsite-content> <article class="devsite-article"> <div class="devsite-article-meta nocontent" role="navigation"> <ul class="devsite-breadcrumb-list" aria-label="Breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://ai.google.dev/" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="" > Home </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://ai.google.dev/gemini-api" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="Gemini API" > Gemini API </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://ai.google.dev/api" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="3" track-type="globalNav" track-name="breadcrumb" track-metadata-position="3" track-metadata-eventdetail="" > API Reference </a> </li> </ul> <devsite-thumb-rating position="header"> </devsite-thumb-rating> </div> <devsite-feedback position="header" project-name="Gemini API" product-id="5292923" bucket="documentation" context="" version="t-devsite-webserver-20241114-r00-rc02.464921008191574316" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="header" class="nocontent" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/images/touchicon-180-new.png" > <button> Send feedback </button> </devsite-feedback> <h1 class="devsite-page-title" tabindex="-1"> Generating content </h1> <div class="devsite-page-title-meta"><devsite-view-release-notes></devsite-view-release-notes></div> <devsite-toc class="devsite-nav" depth="2" devsite-toc-embedded > </devsite-toc> <div class="devsite-article-body clearfix "> <p>The Gemini API supports content generation with images, audio, code, tools, and more. For details on each of these features, read on and check out the task-focused sample code, or read the comprehensive guides.</p> <ul> <li><a href="https://ai.google.dev/gemini-api/docs/text-generation">Text generation</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/vision">Vision</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/audio">Audio</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/long-context">Long context</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/code-execution">Code execution</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/json-mode">JSON Mode</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/function-calling">Function calling</a></li> <li><a href="https://ai.google.dev/gemini-api/docs/system-instructions">System instructions</a></li> </ul> <style> @media screen and (max-width: 2099px) { devsite-toc.devsite-toc,devsite-toc[visible].devsite-toc { display:none } devsite-toc.devsite-toc-embedded:not(:empty) { display: block; margin: 28px 0 24px } body[layout][concierge] devsite-toc.devsite-toc-embedded { display: none } devsite-toc.devsite-toc-embedded:not(:empty)~.devsite-article-body>:first-child { margin-top: 0 } body[template=landing] devsite-toc.devsite-toc-embedded:not(:empty) { margin: 20px 40px 24px } } body[layout=docs] .devsite-main-content[has-book-nav], body[layout=docs] .devsite-main-content[has-book-nav][has-sidebar], body[layout=docs][concierge] .devsite-main-content[has-book-nav], body[layout=docs][concierge] .devsite-main-content[has-book-nav][has-sidebar] { grid-template-columns: minmax(269px, 1fr) minmax(365px, 1600px) 1fr; } body[layout=docs] devsite-content, body[layout=docs][concierge] devsite-content { width: 100%; max-width: 1600px; } .prototype { code { padding: 0; word-break: break-all; } devsite-selector { font-size: 0.9rem; devsite-tabs tab > a { font-size: 0.9rem; } devsite-selector { margin: 12px -23px 0; } } devsite-code pre { margin: 0; padding-block: 12px; padding-inline: 12px; max-height: 600px; font-size: 0.8rem; code { font-size: 0.8rem; } } .code-shrink { zoom: 80%; } .endpoint { display: inline-flex; flex-wrap: nowrap; flex-direction: row; align-items: baseline; justify-content: flex-start; column-gap: 12px; padding: 4px 8px; color: var(--devsite-ref-palette--grey800, #3c4043); background: var(--devsite-ref-palette--grey100, #f1f3f4); border: 1px solid var(--devsite-ref-palette--grey500, #9aa0a6); border-radius: 4px; /* Sys > Typography > Core Composites - Desktop/Overline-s */ font-family: Roboto; font-size: 14px; font-style: normal; font-weight: 500; line-height: 16px; /* 145.455% */ letter-spacing: 0.8px; .http-method { color: var(--devsite-ref-palette--green900, #0d652d); font-size: 12px; text-transform: uppercase; } .endpoint-url { display: inline-block; } } .field-entry { display: flex; flex-direction: column; align-items: initial; justify-content: initial; overflow: hidden; margin: 24px 0 0 12px; p { margin: 0; font-family: Roboto; font-size: 14px; font-style: normal; font-weight: 400; line-height: 20px; /* 166.667% */ } .signature { display: flex; flex-direction: row; flex-wrap: wrap; align-items: flex-end; justify-content: initial; overflow: hidden; column-gap: 12px; row-gap: 0; .field-name { display: inline-block; padding-block: 2px; padding-inline: 0; font-weight: 500; } .field-type { display: inline-block; padding-block: 2px; padding-inline: 0; opacity: 0.66; font-size: 0.9em; } .field-nessesity { display: inline-block; padding: 2px 0; &.required { color: red; } &.optional { color: rgba(0, 0, 0, 0.66); } } } .field-description { display: inline-block; margin-top: 4px; } &.union-type { .union-type-preamble { display: flex; flex-direction: column; align-items: initial; justify-content: initial; row-gap: 12px; } /* nested field-entry styles */ .field-entry { border-left: solid 1px #a8a8a8; padding-inline: 12px 0; } } } .column-container { display: flex; flex-direction: row; flex-wrap: wrap; align-items: initial; justify-content: initial; max-width: 1600px; gap: 0 24px; .reference { flex: 1 1 0; min-width: 400px; } .second-column { flex: 1 1 0; min-width: 350px; position: sticky; top: 110px; height: 100%; } } } </style><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="method:-models.generatecontent" data-text="Method: models.generateContent" tabindex="-1">Method: models.generateContent</h2><a name="v1beta.models.generateContent"></a> <meta content="generateContent" itemprop="name"/> <meta content="/api/rest/v1beta/models/generateContent" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/models/generateContent"> <ul class="toc"> <li><a href="#body.HTTP_TEMPLATE">Endpoint</a></li><li><a href="#body.PATH_PARAMETERS">Path parameters</a></li><li><a href="#body.request_body">Request body</a> <ul> <li><a href="#body.request_body.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#body.response_body">Response body</a></li><li><a href="#body.aspect">Authorization scopes</a></li><li><a href="#body.codeSnippets">Example request</a> <ul> <li><a href="#body.codeSnippets.group">Text</a></li><li><a href="#body.codeSnippets.group_1">Image</a></li><li><a href="#body.codeSnippets.group_2">Audio</a></li><li><a href="#body.codeSnippets.group_3">Video</a></li><li><a href="#body.codeSnippets.group_4">PDF</a></li><li><a href="#body.codeSnippets.group_5">Chat</a></li><li><a href="#body.codeSnippets.group_6">Cache</a></li><li><a href="#body.codeSnippets.group_7">Tuned Model</a></li><li><a href="#body.codeSnippets.group_8">JSON Mode</a></li><li><a href="#body.codeSnippets.group_9">Code execution</a></li><li><a href="#body.codeSnippets.group_10">Function Calling</a></li><li><a href="#body.codeSnippets.group_11">Generation config</a></li><li><a href="#body.codeSnippets.group_12">Safety Settings</a></li><li><a href="#body.codeSnippets.group_13">System Instruction</a></li> </ul> </li> </ul> <section id="google.ai.generativelanguage.v1beta.GenerativeService.GenerateContent"> </section> <section id="description"> <p>Generates a model response given an input <code translate="no" dir="ltr">GenerateContentRequest</code>. Refer to the <a href="https://ai.google.dev/gemini-api/docs/text-generation">text generation guide</a> for detailed usage information. Input capabilities differ between models, including tuned models. Refer to the <a href="https://ai.google.dev/gemini-api/docs/models/gemini">model guide</a> and <a href="https://ai.google.dev/gemini-api/docs/model-tuning">tuning guide</a> for details.</p> </section> <div class="column-container"> <div class="reference"> <section id="body.HTTP_TEMPLATE"> <h3 id="endpoint" data-text="Endpoint" tabindex="-1">Endpoint</h3> <span class="endpoint"> <span class="http-method"> post </span> <span class="endpoint-url"> https://generativelanguage.googleapis.com/v1beta/{model=models/*}:generateContent </span> </span> <br/> <span> </span> </section> <section id="body.PATH_PARAMETERS"> <h3 id="path-parameters" data-text="Path parameters" tabindex="-1">Path parameters</h3> <section id="body.PATH_PARAMETERS.model"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">model</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Required. The name of the <code translate="no" dir="ltr">Model</code> to use for generating the completion.</p><p>Format: <code translate="no" dir="ltr">name=models/{model}</code>. It takes the form <code translate="no" dir="ltr">models/{model}</code>.</p> </div> </div> </section> </section> <section id="body.request_body"> <h3 id="request-body" data-text="Request body" tabindex="-1">Request body</h3> <p>The request body contains data with the following structure:</p> <section id="body.request_body.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="body.request_body.FIELDS.contents"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">contents[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</code> </span> </div> <div class="field-description"> <p>Required. The content of the current conversation with the model.</p><p>For single-turn queries, this is a single instance. For multi-turn queries like <a href="https://ai.google.dev/gemini-api/docs/text-generation#chat">chat</a>, this is a repeated field that contains the conversation history and the latest request.</p> </div> </div> </section> <section id="body.request_body.FIELDS.tools"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">tools[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Tool">Tool</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. A list of <code translate="no" dir="ltr">Tools</code> the <code translate="no" dir="ltr">Model</code> may use to generate the next response.</p><p>A <code translate="no" dir="ltr">Tool</code> is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the <code translate="no" dir="ltr">Model</code>. Supported <code translate="no" dir="ltr">Tool</code>s are <code translate="no" dir="ltr">Function</code> and <code translate="no" dir="ltr">codeExecution</code>. Refer to the <a href="https://ai.google.dev/gemini-api/docs/function-calling">Function calling</a> and the <a href="https://ai.google.dev/gemini-api/docs/code-execution">Code execution</a> guides to learn more.</p> </div> </div> </section> <section id="body.request_body.FIELDS.tool_config"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">toolConfig</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#ToolConfig">ToolConfig</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Tool configuration for any <code translate="no" dir="ltr">Tool</code> specified in the request. Refer to the <a href="https://ai.google.dev/gemini-api/docs/function-calling#function_calling_mode">Function calling guide</a> for a usage example.</p> </div> </div> </section> <section id="body.request_body.FIELDS.safety_settings"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">safetySettings[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.SafetySetting">SafetySetting</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. A list of unique <code translate="no" dir="ltr">SafetySetting</code> instances for blocking unsafe content.</p><p>This will be enforced on the <code translate="no" dir="ltr">GenerateContentRequest.contents</code> and <code translate="no" dir="ltr">GenerateContentResponse.candidates</code>. There should not be more than one setting for each <code translate="no" dir="ltr">SafetyCategory</code> type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each <code translate="no" dir="ltr">SafetyCategory</code> specified in the safetySettings. If there is no <code translate="no" dir="ltr">SafetySetting</code> for a given <code translate="no" dir="ltr">SafetyCategory</code> provided in the list, the API will use the default safety setting for that category. Harm categories HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT are supported. Refer to the <a href="https://ai.google.dev/gemini-api/docs/safety-settings">guide</a> for detailed information on available safety settings. Also refer to the <a href="https://ai.google.dev/gemini-api/docs/safety-guidance">Safety guidance</a> to learn how to incorporate safety considerations in your AI applications.</p> </div> </div> </section> <section id="body.request_body.FIELDS.system_instruction"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">systemInstruction</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Developer set <a href="https://ai.google.dev/gemini-api/docs/system-instructions">system instruction(s)</a>. Currently, text only.</p> </div> </div> </section> <section id="body.request_body.FIELDS.generation_config"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">generationConfig</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.GenerationConfig">GenerationConfig</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Configuration options for model generation and outputs.</p> </div> </div> </section> <section id="body.request_body.FIELDS.cached_content"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">cachedContent</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. The name of the content <a href="https://ai.google.dev/gemini-api/docs/caching">cached</a> to use as context to serve the prediction. Format: <code translate="no" dir="ltr">cachedContents/{cachedContent}</code></p> </div> </div> </section> </section> </section> </div> <div class="second-column"> <h3 id="example-request" data-text="Example request" tabindex="-1">Example request</h3> <devsite-selector> <section> <h3 id="text" data-text="Text" tabindex="-1">Text</h3> <devsite-selector> <section> <h3 id="text_gen_text_only_prompt-PYTHON" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L26-L28" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-JAVASCRIPT" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L29-L37" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-GO" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L50-L56" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-SHELL" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[{"text": "Write a story about a magic backpack."}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L21-L29" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-KOTLIN" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Write a story about a magic backpack."</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#L35-L44" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-SWIFT" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/TextGeneration.swift/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">prompt</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-s">"Write a story about a magic backpack."</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">response</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/TextGeneration.swift#L36-L49" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/TextGeneration.swift#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-n">TextGeneration</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-DART" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Write a story about a magic backpack.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)]);</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/text_generation.dart#L30-L39" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt-JAVA" track-metadata-position="text_gen_text_only_prompt" track-metadata-region-tag="text_gen_text_only_prompt" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java/HEAD/text_gen_text_only_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">ListenableFuture<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Futures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addCallback</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FutureCallback<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSuccess</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onFailure</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#L44-L74" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#text_gen_text_only_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="image" data-text="Image" tabindex="-1">Image</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_one_image_prompt-PYTHON" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">PIL.Image</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">organ</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">PIL</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Image</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">open</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"organ.jpg"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"Tell me about this instrument"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">organ</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L42-L47" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-JAVASCRIPT" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileToGenerativePart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">path</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">inlineData</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">data</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Buffer</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-kr">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">fs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">readFileSync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">path</span><span class="devsite-syntax-p">)).</span><span class="devsite-syntax-nx">toString</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"base64"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Describe how this product might be manufactured."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-c1">// Note: The only accepted mime types are some image types, image/*.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">imagePart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileToGenerativePart</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">mediaPath</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">/jetpack.jpg`</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"image/jpeg"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">imagePart</span><span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L62-L84" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-GO" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">imgData</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">os</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ReadFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">filepath</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">testDataDir</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"organ.jpg"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Tell me about this instrument"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ImageData</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"jpeg"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">imgData</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L69-L83" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-SHELL" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># Use a temporary file to hold the base64 encoded image data</span> <span class="devsite-syntax-nv">TEMP_B64</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>mktemp<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">trap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'rm -f "$TEMP_B64"'</span><span class="devsite-syntax-w"> </span>EXIT base64<span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">$B64FLAGS</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">$IMG_PATH</span><span class="devsite-syntax-w"> > </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-nv">$TEMP_B64</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Use a temporary file to hold the JSON payload</span> <span class="devsite-syntax-nv">TEMP_JSON</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>mktemp<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">trap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'rm -f "$TEMP_JSON"'</span><span class="devsite-syntax-w"> </span>EXIT cat<span class="devsite-syntax-w"> > </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-nv">$TEMP_JSON</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> << </span>EOF <span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"contents"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"parts"</span>:<span class="devsite-syntax-o">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span><span class="devsite-syntax-s2">"text"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Tell me about this instrument"</span><span class="devsite-syntax-o">}</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"inline_data"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"mime_type"</span>:<span class="devsite-syntax-s2">"image/jpeg"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"data"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">$(</span>cat<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-nv">$TEMP_B64</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">]</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}]</span> <span class="devsite-syntax-o">}</span> EOF curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-nv">$TEMP_JSON</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L41-L70" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-KOTLIN" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">image</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Bitmap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BitmapFactory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">decodeResource</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">resources</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">R</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">drawable</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">image</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">inputContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What's in this picture?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">inputContent</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#L66-L80" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-SWIFT" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/TextGeneration.swift/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">guard</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">image</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-bp">UIImage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">systemName</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"cloud.sun"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">else</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">fatalError</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">prompt</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-s">"What's in this picture?"</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">response</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/TextGeneration.swift#L76-L92" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/TextGeneration.swift#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-n">TextGeneration</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-DART" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Future<DataPart></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fileToPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">mimeType</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DataPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">mimeType</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">File</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">readAsBytes</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Describe how this product might be manufactured.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fileToPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'image/jpeg'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'resources/jetpack.jpg'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">([</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">multi</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">TextPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/text_generation.dart#L62-L79" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt-JAVA" track-metadata-position="text_gen_multimodal_one_image_prompt" track-metadata-region-tag="text_gen_multimodal_one_image_prompt" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java/HEAD/text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Bitmap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BitmapFactory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">decodeResource</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getResources</span><span class="devsite-syntax-p">(),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">R</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">drawable</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">image</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What's different between these pictures?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addImage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">ListenableFuture<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Futures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addCallback</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FutureCallback<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSuccess</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onFailure</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#L124-L159" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#text_gen_multimodal_one_image_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="audio" data-text="Audio" tabindex="-1">Audio</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_audio-PYTHON" track-metadata-position="text_gen_multimodal_audio" track-metadata-region-tag="text_gen_multimodal_audio" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_audio" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_audio" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">sample_audio</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"sample.mp3"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"Give me a summary of this audio file."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">sample_audio</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L93-L96" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_audio" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_audio"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_audio-JAVASCRIPT" track-metadata-position="text_gen_multimodal_audio" track-metadata-region-tag="text_gen_multimodal_audio" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_multimodal_audio" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_audio" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileToGenerativePart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">path</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">inlineData</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">data</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Buffer</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-kr">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">fs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">readFileSync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">path</span><span class="devsite-syntax-p">)).</span><span class="devsite-syntax-nx">toString</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"base64"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Give me a summary of this audio file."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-c1">// Note: The only accepted mime types are some image types, image/*.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">audioPart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileToGenerativePart</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">mediaPath</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">/samplesmall.mp3`</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"audio/mp3"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">audioPart</span><span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L192-L214" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_multimodal_audio" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_audio"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_audio-SHELL" track-metadata-position="text_gen_multimodal_audio" track-metadata-region-tag="text_gen_multimodal_audio" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_audio" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_audio" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># Use File API to upload audio data to API request.</span> <span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>file<span class="devsite-syntax-w"> </span>-b<span class="devsite-syntax-w"> </span>--mime-type<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">AUDIO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>wc<span class="devsite-syntax-w"> </span>-c<span class="devsite-syntax-w"> < </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">AUDIO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-o">=</span>AUDIO <span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-o">=</span>upload-header.tmp <span class="devsite-syntax-c1"># Initial resumable request defining metadata.</span> <span class="devsite-syntax-c1"># The upload url is in the response headers dump them to a file.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">BASE_URL</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">/upload/v1beta/files?key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-D<span class="devsite-syntax-w"> </span>upload-header.tmp<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Protocol: resumable"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: start"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Type: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Type: application/json"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"{'file': {'display_name': '</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">'}}"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null <span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>grep<span class="devsite-syntax-w"> </span>-i<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"x-goog-upload-url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>cut<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-s2">" "</span><span class="devsite-syntax-w"> </span>-f2<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>tr<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"\r"</span><span class="devsite-syntax-k">)</span> rm<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Upload the actual bytes.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Offset: 0"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: upload, finalize"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--data-binary<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">AUDIO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.uri"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$file_uri</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Please describe this file."},</span> <span class="devsite-syntax-s1"> {"file_data":{"mime_type": "audio/mpeg", "file_uri": '</span><span class="devsite-syntax-nv">$file_uri</span><span class="devsite-syntax-s1">'}}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>response.json cat<span class="devsite-syntax-w"> </span>response.json <span class="devsite-syntax-nb">echo</span> jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".candidates[].content.parts[].text"</span><span class="devsite-syntax-w"> </span>response.json<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L174-L220" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_audio" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_audio">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="video" data-text="Video" tabindex="-1">Video</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_video_prompt-PYTHON" track-metadata-position="text_gen_multimodal_video_prompt" track-metadata-region-tag="text_gen_multimodal_video_prompt" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_video_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">time</span> <span class="devsite-syntax-c1"># Video clip (CC BY 3.0) from https://peach.blender.org/download/</span> <span class="devsite-syntax-n">myfile</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"Big_Buck_Bunny.mp4"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-si">=}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Videos need to be processed before you can use them.</span> <span class="devsite-syntax-k">while</span> <span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">state</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">==</span> <span class="devsite-syntax-s2">"PROCESSING"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"processing video..."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">time</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">sleep</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">5</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">myfile</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"Describe this video clip"</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-si">=}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L112-L126" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_video_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_video_prompt-JAVASCRIPT" track-metadata-position="text_gen_multimodal_video_prompt" track-metadata-region-tag="text_gen_multimodal_video_prompt" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_multimodal_video_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-c1">// import { GoogleAIFileManager, FileState } from "@google/generative-ai/server";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleAIFileManager</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">uploadFile</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">mediaPath</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">/Big_Buck_Bunny.mp4`</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"video/mp4"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-k">while</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">state</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">===</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">FileState</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">PROCESSING</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Sleep for 10 seconds</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">Promise</span><span class="devsite-syntax-p">((</span><span class="devsite-syntax-nx">resolve</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setTimeout</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resolve</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">10</span><span class="devsite-syntax-nx">_000</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Fetch the file from the API again</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">state</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">===</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">FileState</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">FAILED</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">throw</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ne">Error</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Video processing failed."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Describe this video clip"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">videoPart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileData</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileUri</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">uri</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">};</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">videoPart</span><span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L220-L255" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_multimodal_video_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_video_prompt-GO" track-metadata-position="text_gen_multimodal_video_prompt" track-metadata-region-tag="text_gen_multimodal_video_prompt" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/text_gen_multimodal_video_prompt" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">UploadFileFromPath</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">filepath</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">testDataDir</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"earth.mp4"</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">DeleteFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Videos need to be processed before you can use them.</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">State</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">FileStateProcessing</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Printf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"processing %s"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">time</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Sleep</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">5</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">time</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Second</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">error</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GetFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Name</span><span class="devsite-syntax-p">);</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">State</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">FileStateActive</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatalf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"uploaded file has state %s, not active"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">State</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Describe this video clip"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">FileData</span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">URI</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">URI</span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L96-L124" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#text_gen_multimodal_video_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_video_prompt-SHELL" track-metadata-position="text_gen_multimodal_video_prompt" track-metadata-region-tag="text_gen_multimodal_video_prompt" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_video_prompt" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># Use File API to upload audio data to API request.</span> <span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>file<span class="devsite-syntax-w"> </span>-b<span class="devsite-syntax-w"> </span>--mime-type<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">VIDEO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>wc<span class="devsite-syntax-w"> </span>-c<span class="devsite-syntax-w"> < </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">VIDEO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-o">=</span>VIDEO <span class="devsite-syntax-c1"># Initial resumable request defining metadata.</span> <span class="devsite-syntax-c1"># The upload url is in the response headers dump them to a file.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">BASE_URL</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">/upload/v1beta/files?key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-D<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Protocol: resumable"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: start"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Type: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Type: application/json"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"{'file': {'display_name': '</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">'}}"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null <span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>grep<span class="devsite-syntax-w"> </span>-i<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"x-goog-upload-url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>cut<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-s2">" "</span><span class="devsite-syntax-w"> </span>-f2<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>tr<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"\r"</span><span class="devsite-syntax-k">)</span> rm<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Upload the actual bytes.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Offset: 0"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: upload, finalize"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--data-binary<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">VIDEO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.uri"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$file_uri</span> <span class="devsite-syntax-nv">state</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.state"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">state</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$state</span> <span class="devsite-syntax-nv">name</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.name"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">name</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$name</span> <span class="devsite-syntax-k">while</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[[</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"(</span><span class="devsite-syntax-nv">$state</span><span class="devsite-syntax-s2">)"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span>*<span class="devsite-syntax-s2">"PROCESSING"</span>*<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">]]</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">do</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Processing video..."</span> <span class="devsite-syntax-w"> </span>sleep<span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">5</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Get the file of interest to check state</span> <span class="devsite-syntax-w"> </span>curl<span class="devsite-syntax-w"> </span>https://generativelanguage.googleapis.com/v1beta/files/<span class="devsite-syntax-nv">$name</span><span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">state</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.state"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-k">done</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Transcribe the audio from this video, giving timestamps for salient events in the video. Also provide visual descriptions."},</span> <span class="devsite-syntax-s1"> {"file_data":{"mime_type": "video/mp4", "file_uri": '</span><span class="devsite-syntax-nv">$file_uri</span><span class="devsite-syntax-s1">'}}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>response.json cat<span class="devsite-syntax-w"> </span>response.json <span class="devsite-syntax-nb">echo</span> jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".candidates[].content.parts[].text"</span><span class="devsite-syntax-w"> </span>response.json<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L272-L331" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_video_prompt" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="pdf" data-text="PDF" tabindex="-1">PDF</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_pdf-PYTHON" track-metadata-position="text_gen_multimodal_pdf" track-metadata-region-tag="text_gen_multimodal_pdf" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_pdf" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_pdf" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">sample_pdf</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"test.pdf"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"Give me a summary of this document:"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">sample_pdf</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-si">=}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L153-L156" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_pdf" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_pdf"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_pdf-SHELL" track-metadata-position="text_gen_multimodal_pdf" track-metadata-region-tag="text_gen_multimodal_pdf" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_pdf" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_pdf" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>file<span class="devsite-syntax-w"> </span>-b<span class="devsite-syntax-w"> </span>--mime-type<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">PDF_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>wc<span class="devsite-syntax-w"> </span>-c<span class="devsite-syntax-w"> < </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">PDF_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-o">=</span>TEXT <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">$MIME_TYPE</span> <span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-o">=</span>upload-header.tmp <span class="devsite-syntax-c1"># Initial resumable request defining metadata.</span> <span class="devsite-syntax-c1"># The upload url is in the response headers dump them to a file.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">BASE_URL</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">/upload/v1beta/files?key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-D<span class="devsite-syntax-w"> </span>upload-header.tmp<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Protocol: resumable"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: start"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Type: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Type: application/json"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"{'file': {'display_name': '</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">'}}"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null <span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>grep<span class="devsite-syntax-w"> </span>-i<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"x-goog-upload-url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>cut<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-s2">" "</span><span class="devsite-syntax-w"> </span>-f2<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>tr<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"\r"</span><span class="devsite-syntax-k">)</span> rm<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Upload the actual bytes.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Offset: 0"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: upload, finalize"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--data-binary<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">PDF_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.uri"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$file_uri</span> <span class="devsite-syntax-c1"># Now generate content using that file</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Can you add a few more lines to this poem?"},</span> <span class="devsite-syntax-s1"> {"file_data":{"mime_type": "application/pdf", "file_uri": '</span><span class="devsite-syntax-nv">$file_uri</span><span class="devsite-syntax-s1">'}}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>response.json cat<span class="devsite-syntax-w"> </span>response.json <span class="devsite-syntax-nb">echo</span> jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".candidates[].content.parts[].text"</span><span class="devsite-syntax-w"> </span>response.json<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L393-L441" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_pdf" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_pdf">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="chat" data-text="Chat" tabindex="-1">Chat</h3> <devsite-selector> <section> <h3 id="chat-PYTHON" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/chat.py/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/chat.py" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">chat</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start_chat</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">history</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-p">{</span><span class="devsite-syntax-s2">"role"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"parts"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"Hello"</span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">{</span><span class="devsite-syntax-s2">"role"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"parts"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">send_message</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"I have 2 dogs in my house."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">send_message</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"How many paws are in my house?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/chat.py#L26-L36" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/chat.py#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/chat.py" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-JAVASCRIPT" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/chat.js/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/chat.js" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">startChat</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">history</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">role</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">parts</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Hello"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">role</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">parts</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"I have 2 dogs in my house."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"How many paws are in my house?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/chat.js#L28-L47" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/chat.js#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/chat.js" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-GO" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/chat" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">cs</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">StartChat</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-nx">cs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">History</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Content</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Parts</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Part</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Role</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Parts</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Part</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Role</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">res</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">res</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L967-L989" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-SHELL" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/chat.sh/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/chat.sh" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span>https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key<span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [</span> <span class="devsite-syntax-s1"> {"role":"user",</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "Hello"}]},</span> <span class="devsite-syntax-s1"> {"role": "model",</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "Great to meet you. What would you like to know?"}]},</span> <span class="devsite-syntax-s1"> {"role":"user",</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "I have two dogs in my house. How many paws are in my house?"}]},</span> <span class="devsite-syntax-s1"> ]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>grep<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"text"</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/chat.sh#L7-L23" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/chat.sh#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/chat.sh" data-indented-block="" data-regexp="" data-region-tag="chat">chat.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-KOTLIN" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startChat</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}))</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt#L35-L52" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-SWIFT" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/ChatSnippets.swift/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/ChatSnippets.swift" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Optionally specify existing chat history</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">history</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-p">[</span> <span class="devsite-syntax-n">ModelContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-n">ModelContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">]</span> <span class="devsite-syntax-c1">// Initialize the chat with optional chat history</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">chat</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">startChat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// To generate text output, call sendMessage and pass in the message</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">response</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/ChatSnippets.swift#L35-L57" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/ChatSnippets.swift#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/ChatSnippets.swift" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-n">ChatSnippets</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-DART" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">startChat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nl">history:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'hello'</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">TextPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Great to meet you. What would you like to know?'</span><span class="devsite-syntax-p">)])</span> <span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'I have 2 dogs in my house.'</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'How many paws are in my house?'</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/chat.dart#L30-L45" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat-JAVA" track-metadata-position="chat" track-metadata-region-tag="chat" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java/HEAD/chat" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java" data-git-revision="HEAD" data-region-tag="chat" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// (optional) Create previous chat history for context</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelContentBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">modelContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">modelContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">List<Content></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">userContent</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelContent</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Initialize the chat</span> <span class="devsite-syntax-n">ChatFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startChat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Create a new user message</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// Send the message</span> <span class="devsite-syntax-n">ListenableFuture<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Futures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addCallback</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FutureCallback<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSuccess</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onFailure</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java#L47-L98" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java#chat" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java" data-indented-block="" data-regexp="" data-region-tag="chat"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="cache" data-text="Cache" tabindex="-1">Cache</h3> <devsite-selector> <section> <h3 id="cache_create-PYTHON" track-metadata-position="cache_create" track-metadata-region-tag="cache_create" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/cache.py/HEAD/cache_create" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/cache.py" data-git-revision="HEAD" data-region-tag="cache_create" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">document</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"a11.txt"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">model_name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"gemini-1.5-flash-001"</span> <span class="devsite-syntax-n">cache</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">caching</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">CachedContent</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">create</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">model_name</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">system_instruction</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"You are an expert analyzing transcripts."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">contents</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">document</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">cache</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">from_cached_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">cache</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Please summarize this transcript"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/cache.py#L27-L38" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/cache.py#cache_create" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/cache.py" data-indented-block="" data-regexp="" data-region-tag="cache_create"><span class="devsite-syntax-n">cache</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="cache_create-JAVASCRIPT" track-metadata-position="cache_create" track-metadata-region-tag="cache_create" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/cache.js/HEAD/cache_create" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/cache.js" data-git-revision="HEAD" data-region-tag="cache_create" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleAICacheManager, GoogleAIFileManager } from "@google/generative-ai/server";</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cacheManager</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleAICacheManager</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleAIFileManager</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">uploadFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">mediaPath</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">/a11.txt`</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"text/plain"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cacheResult</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cacheManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">create</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"models/gemini-1.5-flash-001"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">contents</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">role</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">parts</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileData</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileUri</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">uri</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">cacheResult</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModelFromCachedContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">cacheResult</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Please summarize this transcript."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/cache.js#L31-L65" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/cache.js#cache_create" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/cache.js" data-indented-block="" data-regexp="" data-region-tag="cache_create"><span class="devsite-syntax-nx">cache</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="tuned-model" data-text="Tuned Model" tabindex="-1">Tuned Model</h3> <devsite-selector> <section> <h3 id="tuned_models_generate_content-PYTHON" track-metadata-position="tuned_models_generate_content" track-metadata-region-tag="tuned_models_generate_content" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/tuned_models.py/HEAD/tuned_models_generate_content" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/tuned_models.py" data-git-revision="HEAD" data-region-tag="tuned_models_generate_content" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">model_name</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"tunedModels/my-increment-model"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">result</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"III"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># "IV"<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/tuned_models.py#L77-L79" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/tuned_models.py#tuned_models_generate_content" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/tuned_models.py" data-indented-block="" data-regexp="" data-region-tag="tuned_models_generate_content">tuned_models.py</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="json-mode" data-text="JSON Mode" tabindex="-1">JSON Mode</h3> <devsite-selector> <section> <h3 id="json_controlled_generation-PYTHON" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/controlled_generation.py/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/controlled_generation.py" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">typing_extensions</span> <span class="devsite-syntax-k">as</span> <span class="devsite-syntax-nn">typing</span> <span class="devsite-syntax-k">class</span> <span class="devsite-syntax-nc">Recipe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">typing</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">TypedDict</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-n">recipe_name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">str</span> <span class="devsite-syntax-n">ingredients</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">list</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-nb">str</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-pro-latest"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">result</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-s2">"List a few popular cookie recipes."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">generation_config</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">response_mime_type</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"application/json"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">response_schema</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nb">list</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">Recipe</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/controlled_generation.py#L24-L37" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/controlled_generation.py#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/controlled_generation.py" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-n">controlled_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-JAVASCRIPT" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/controlled_generation.js/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/controlled_generation.js" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">schema</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">description</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"List of recipes"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">type</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">SchemaType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ARRAY</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">items</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">type</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">SchemaType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">OBJECT</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">properties</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">recipeName</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">type</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">SchemaType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">STRING</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">description</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Name of the recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">nullable</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">required</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"recipeName"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">};</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-pro"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">generationConfig</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">responseMimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"application/json"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">responseSchema</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">schema</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"List a few popular cookie recipes."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/controlled_generation.js#L25-L56" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/controlled_generation.js#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/controlled_generation.js" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-nx">controlled_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-GO" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-pro-latest"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Ask the model to respond with JSON.</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ResponseMIMEType</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"application/json"</span> <span class="devsite-syntax-c1">// Specify the schema.</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ResponseSchema</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span>&<span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Schema</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Type</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">TypeArray</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Items</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span>&<span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Schema</span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">Type</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">TypeString</span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"List a few popular cookie recipes using this JSON schema."</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">_</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">part</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">range</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Candidates</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-mi">0</span><span class="devsite-syntax-p">].</span><span class="devsite-syntax-nx">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Parts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">txt</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ok</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">part</span><span class="devsite-syntax-p">.(</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">);</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ok</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">recipes</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-kt">string</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">json</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Unmarshal</span><span class="devsite-syntax-p">([]</span><span class="devsite-syntax-nb">byte</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">txt</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span>&<span class="devsite-syntax-nx">recipes</span><span class="devsite-syntax-p">);</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">recipes</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L907-L927" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-SHELL" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/controlled_generation.sh/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/controlled_generation.sh" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> -H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> -d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "List 5 popular cookie recipes"}</span> <span class="devsite-syntax-s1"> ]</span> <span class="devsite-syntax-s1"> }],</span> <span class="devsite-syntax-s1"> "generationConfig": {</span> <span class="devsite-syntax-s1"> "response_mime_type": "application/json",</span> <span class="devsite-syntax-s1"> "response_schema": {</span> <span class="devsite-syntax-s1"> "type": "ARRAY",</span> <span class="devsite-syntax-s1"> "items": {</span> <span class="devsite-syntax-s1"> "type": "OBJECT",</span> <span class="devsite-syntax-s1"> "properties": {</span> <span class="devsite-syntax-s1"> "recipe_name": {"type":"STRING"},</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1">}'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>head<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/controlled_generation.sh#L5-L25" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/controlled_generation.sh#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/controlled_generation.sh" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation">controlled_generation.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-KOTLIN" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/controlled_generation.kt/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/controlled_generation.kt" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-pro"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responseMimeType</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"application/json"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responseSchema</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipes"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">description</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"List of recipes"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">type</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ARRAY</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">items</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">description</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"A recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">type</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">OBJECT</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">properties</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">mapOf</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">to</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">description</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Name of the recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">type</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">STRING</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">nullable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">required</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"List a few popular cookie recipes."</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/controlled_generation.kt#L33-L64" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/controlled_generation.kt#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/controlled_generation.kt" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-n">controlled_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-SWIFT" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/ControlledGeneration.swift/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/ControlledGeneration.swift" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">jsonSchema</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">array</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">description</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"List of recipes"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">items</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">object</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">properties</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span> <span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">string</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">description</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Name of the recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">nullable</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">requiredProperties</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a model that supports controlled generation like Gemini 1.5 Pro</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-pro"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">responseMIMEType</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"application/json"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">responseSchema</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">jsonSchema</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">prompt</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-s">"List a few popular cookie recipes."</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">response</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/ControlledGeneration.swift#L35-L63" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/ControlledGeneration.swift#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/ControlledGeneration.swift" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-n">ControlledGeneration</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-DART" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/controlled_generation.dart/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/controlled_generation.dart" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">schema</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">array</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">description:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'List of recipes'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">items:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">object</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nl">properties:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'recipeName'</span><span class="devsite-syntax-o">:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">string</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nl">description:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Name of the recipe.'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">nullable:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">requiredProperties:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'recipeName'</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">]));</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-pro'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">generationConfig:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">responseMimeType:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'application/json'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">responseSchema:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">schema</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'List a few popular cookie recipes.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)]);</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/controlled_generation.dart#L30-L49" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/controlled_generation.dart#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/controlled_generation.dart" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-n">controlled_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="json_controlled_generation-JAVA" track-metadata-position="json_controlled_generation" track-metadata-region-tag="json_controlled_generation" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/controlled_generation.java/HEAD/json_controlled_generation" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/controlled_generation.java" data-git-revision="HEAD" data-region-tag="json_controlled_generation" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">Schema<List<String></span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">schema</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* name */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipes"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* description */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"List of recipes"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* format */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* nullable */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* list */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* properties */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* required */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* items */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* name */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* description */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"A recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* format */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* nullable */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* list */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* properties */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Map</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">of</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* name */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"recipeName"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* description */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Name of the recipe"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* format */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* nullable */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* list */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* properties */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* required */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* items */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* type */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">STRING</span><span class="devsite-syntax-p">)),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* required */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* items */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* type */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">OBJECT</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* type */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionType</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ARRAY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">responseMimeType</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"application/json"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">responseSchema</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">schema</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-pro"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* generationConfig */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"List a few popular cookie recipes."</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">ListenableFuture<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Futures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addCallback</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FutureCallback<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSuccess</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onFailure</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/controlled_generation.java#L42-L110" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/controlled_generation.java#json_controlled_generation" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/controlled_generation.java" data-indented-block="" data-regexp="" data-region-tag="json_controlled_generation"><span class="devsite-syntax-n">controlled_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="code-execution" data-text="Code execution" tabindex="-1">Code execution</h3> <devsite-selector> <section> <h3 id="code_execution_basic-PYTHON" track-metadata-position="code_execution_basic" track-metadata-region-tag="code_execution_basic" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/code_execution.py/HEAD/code_execution_basic" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/code_execution.py" data-git-revision="HEAD" data-region-tag="code_execution_basic" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">model_name</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">tools</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"code_execution"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-p">(</span> <span class="devsite-syntax-s2">"What is the sum of the first 50 prime numbers? "</span> <span class="devsite-syntax-s2">"Generate and run code for the calculation, and make sure you get all 50."</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Each `part` either contains `text`, `executable_code` or an `execution_result`</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">part</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">candidates</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-mi">0</span><span class="devsite-syntax-p">]</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">part</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"-"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># The `.text` accessor joins the parts into a markdown compatible text representation.</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-se">\n\n</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/code_execution.py#L23-L37" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/code_execution.py#code_execution_basic" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/code_execution.py" data-indented-block="" data-regexp="" data-region-tag="code_execution_basic"><span class="devsite-syntax-n">code_execution</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="code_execution_basic-KOTLIN" track-metadata-position="code_execution_basic" track-metadata-region-tag="code_execution_basic" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt/HEAD/code_execution_basic" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt" data-git-revision="HEAD" data-region-tag="code_execution_basic" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-pro"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">tools</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">CODE_EXECUTION</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What is the sum of the first 50 prime numbers?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Each `part` either contains `text`, `executable_code` or an `execution_result`</span> <span class="devsite-syntax-n">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">candidates</span><span class="devsite-syntax-o">[</span><span class="devsite-syntax-m">0</span><span class="devsite-syntax-o">]</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">parts</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">joinToString</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"\n"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-c1">// Alternatively, you can use the `text` accessor which joins the parts into a markdown compatible</span> <span class="devsite-syntax-c1">// text representation</span> <span class="devsite-syntax-n">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt#L31-L47" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt#code_execution_basic" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt" data-indented-block="" data-regexp="" data-region-tag="code_execution_basic"><span class="devsite-syntax-n">code_execution</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="code_execution_basic-JAVA" track-metadata-position="code_execution_basic" track-metadata-region-tag="code_execution_basic" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java/HEAD/code_execution_basic" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java" data-git-revision="HEAD" data-region-tag="code_execution_basic" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-pro"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* generationConfig */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* safetySettings */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* requestOptions */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">RequestOptions</span><span class="devsite-syntax-p">(),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* tools */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Collections</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">singletonList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">CODE_EXECUTION</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">inputContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What is the sum of the first 50 prime numbers?"</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">ListenableFuture<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">inputContent</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Futures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addCallback</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FutureCallback<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSuccess</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Each `part` either contains `text`, `executable_code` or an</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// `execution_result`</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Candidate</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">candidate</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getCandidates</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">get</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">0</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Part</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">part</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">candidate</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getContent</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">getParts</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">part</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Alternatively, you can use the `text` accessor which joins the parts into a</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// markdown compatible text representation</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">resultText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onFailure</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java#L46-L89" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java#code_execution_basic" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java" data-indented-block="" data-regexp="" data-region-tag="code_execution_basic"><span class="devsite-syntax-n">code_execution</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="function-calling" data-text="Function Calling" tabindex="-1">Function Calling</h3> <devsite-selector> <section> <h3 id="function_calling-PYTHON" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/function_calling.py/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/function_calling.py" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">add</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""returns a + b."""</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">a</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-n">b</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">subtract</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""returns a - b."""</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">a</span> <span class="devsite-syntax-o">-</span> <span class="devsite-syntax-n">b</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">multiply</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""returns a * b."""</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">a</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-n">b</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">divide</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">float</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""returns a / b."""</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">a</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">b</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">model_name</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">tools</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">add</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">subtract</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">multiply</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">divide</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">chat</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start_chat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">enable_automatic_function_calling</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">True</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">send_message</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-s2">"I have 57 cats, each owns 44 mittens, how many mittens is that in total?"</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/function_calling.py#L23-L46" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/function_calling.py#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/function_calling.py" data-indented-block="" data-regexp="" data-region-tag="function_calling"><span class="devsite-syntax-n">function_calling</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="function_calling-JAVASCRIPT" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/function_calling.js/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/function_calling.js" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-k">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setLightValues</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">brightness</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">colorTemperature</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// This mock API returns the requested lighting values</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">brightness</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">colorTemperature</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">controlLightFunctionDeclaration</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"controlLight"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">parameters</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">type</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"OBJECT"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">description</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Set the brightness and color temperature of a room light."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">properties</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">brightness</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">type</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"NUMBER"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">description</span><span class="devsite-syntax-o">:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Light level from 0 to 100. Zero is off and 100 is full brightness."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">colorTemperature</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">type</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"STRING"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">description</span><span class="devsite-syntax-o">:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Color temperature of the light fixture which can be `daylight`, `cool` or `warm`."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">required</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"brightness"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"colorTemperature"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">};</span> <span class="devsite-syntax-c1">// Executable function code. Put it in a map keyed by the function name</span> <span class="devsite-syntax-c1">// so that you can call it once you get the name string from the model.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">functions</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">controlLight</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">brightness</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">colorTemperature</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">})</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setLightValues</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">brightness</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">colorTemperature</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">};</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">tools</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">functionDeclarations</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-nx">controlLightFunctionDeclaration</span><span class="devsite-syntax-p">]</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">startChat</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Dim the lights so the room feels cozy and warm."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-c1">// Send the message to the model.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// For simplicity, this uses the first function call found.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">call</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">functionCalls</span><span class="devsite-syntax-p">()[</span><span class="devsite-syntax-mf">0</span><span class="devsite-syntax-p">];</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">call</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Call the executable function named in the function call</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// with the arguments specified in the function call and</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// let it call the hypothetical API.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">apiResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">functions</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-nx">call</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-p">](</span><span class="devsite-syntax-nx">call</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">args</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Send the API response back to the model so it can generate</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// a text response that can be displayed to the user.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result2</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">sendMessage</span><span class="devsite-syntax-p">([</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">functionResponse</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"controlLight"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">apiResponse</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Log the text response.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result2</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/function_calling.js#L22-L94" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/function_calling.js#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/function_calling.js" data-indented-block="" data-regexp="" data-region-tag="function_calling"><span class="devsite-syntax-nx">function_calling</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="function_calling-SHELL" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/function_calling.sh/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/function_calling.sh" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"> cat<span class="devsite-syntax-w"> > </span>tools.json<span class="devsite-syntax-w"> << </span>EOF <span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"function_declarations"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"name"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"enable_lights"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"description"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Turn on the lighting system."</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"parameters"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"type"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"object"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"name"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"set_light_color"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"description"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Set the light color. Lights must be enabled for this to work."</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"parameters"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"type"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"object"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"properties"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"rgb_hex"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"type"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"string"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"description"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"The light color as a 6-digit hex string, e.g. ff0000 for red."</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"required"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"rgb_hex"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">]</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"name"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"stop_lights"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"description"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Turn off the lighting system."</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"parameters"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"type"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"object"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">]</span> <span class="devsite-syntax-o">}</span><span class="devsite-syntax-w"> </span> EOF curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span>@<<span class="devsite-syntax-o">(</span><span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'</span> <span class="devsite-syntax-s1"> {</span> <span class="devsite-syntax-s1"> "system_instruction": {</span> <span class="devsite-syntax-s1"> "parts": {</span> <span class="devsite-syntax-s1"> "text": "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks."</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> },</span> <span class="devsite-syntax-s1"> "tools": ['</span><span class="devsite-syntax-k">$(</span><span class="devsite-syntax-nb">source</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-nv">$tools</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span><span class="devsite-syntax-s1">'],</span> <span class="devsite-syntax-s1"> "tool_config": {</span> <span class="devsite-syntax-s1"> "function_calling_config": {"mode": "none"}</span> <span class="devsite-syntax-s1"> },</span> <span class="devsite-syntax-s1"> "contents": {</span> <span class="devsite-syntax-s1"> "role": "user",</span> <span class="devsite-syntax-s1"> "parts": {</span> <span class="devsite-syntax-s1"> "text": "What can you do?"</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1">'</span><span class="devsite-syntax-o">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>>/dev/null<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span>sed<span class="devsite-syntax-w"> </span>-n<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'/"content"/,/"finishReason"/p'</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/function_calling.sh#L4-L61" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/function_calling.sh#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/function_calling.sh" data-indented-block="" data-regexp="" data-region-tag="function_calling">function_calling.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="function_calling-KOTLIN" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/function_calling.kt/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/function_calling.kt" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">fun</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">multiply</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">Double</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">Double</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">b</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">multiplyDefinition</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">defineFunction</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"multiply"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">description</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"returns the product of the provided numbers."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">parameters</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">double</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"a"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"First number"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">double</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"b"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Second number"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">usableFunctions</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">multiplyDefinition</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// List the functions definitions you want to make available to the model</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">tools</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">usableFunctions</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startChat</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"I have 57 cats, each owns 44 mittens, how many mittens is that in total?"</span> <span class="devsite-syntax-c1">// Send the message to the generative model</span> <span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Check if the model responded with a function call</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">functionCalls</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">first</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">it</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"multiply"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}.</span><span class="devsite-syntax-na">apply</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">a</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">by</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">b</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">by</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JSONObject</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">mapOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"result"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">to</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">multiply</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">toDouble</span><span class="devsite-syntax-p">(),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">toDouble</span><span class="devsite-syntax-p">())))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessage</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"function"</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">part</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">FunctionResponsePart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"multiply"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-c1">// Whenever the model responds with text, show it in the UI</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-o">?.</span><span class="devsite-syntax-na">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">modelResponse</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/function_calling.kt#L38-L83" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/function_calling.kt#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/function_calling.kt" data-indented-block="" data-regexp="" data-region-tag="function_calling"><span class="devsite-syntax-n">function_calling</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="function_calling-SWIFT" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/FunctionCalling.swift/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/FunctionCalling.swift" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Calls a hypothetical API to control a light bulb and returns the values that were set.</span> <span class="devsite-syntax-kd">func</span> <span class="devsite-syntax-nf">controlLight</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">brightness</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">Double</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">colorTemperature</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">String</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-o">-</span>> <span class="devsite-syntax-n">JSONObject</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"brightness"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">number</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">brightness</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-s">"colorTemperature"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">string</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">colorTemperature</span><span class="devsite-syntax-p">)]</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Use a model that supports function calling, like a Gemini 1.5 model</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">tools</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">functionDeclarations</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span> <span class="devsite-syntax-n">FunctionDeclaration</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"controlLight"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">description</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Set the brightness and color temperature of a room light."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parameters</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span> <span class="devsite-syntax-s">"brightness"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">number</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">format</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"double"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">description</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Light level from 0 to 100. Zero is off and 100 is full brightness."</span> <span class="devsite-syntax-p">),</span> <span class="devsite-syntax-s">"colorTemperature"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">type</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">string</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">format</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"enum"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">description</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Color temperature of the light fixture."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">enumValues</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"daylight"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s">"cool"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s">"warm"</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">requiredParameters</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"brightness"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s">"colorTemperature"</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">])]</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">chat</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">startChat</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">prompt</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-s">"Dim the lights so the room feels cozy and warm."</span> <span class="devsite-syntax-c1">// Send the message to the model.</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">response1</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Check if the model responded with a function call.</span> <span class="devsite-syntax-c1">// For simplicity, this sample uses the first function call found.</span> <span class="devsite-syntax-k">guard</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">functionCall</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response1</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">functionCalls</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-bp">first</span> <span class="devsite-syntax-k">else</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">fatalError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Model did not respond with a function call."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-c1">// Print an error if the returned function was not declared</span> <span class="devsite-syntax-k">guard</span> <span class="devsite-syntax-n">functionCall</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">name</span> <span class="devsite-syntax-p">==</span> <span class="devsite-syntax-s">"controlLight"</span> <span class="devsite-syntax-k">else</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">fatalError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Unexpected function called: </span><span class="devsite-syntax-si">\(</span><span class="devsite-syntax-n">functionCall</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-si">)</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-c1">// Verify that the names and types of the parameters match the declaration</span> <span class="devsite-syntax-k">guard</span> <span class="devsite-syntax-k">case</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">number</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">brightness</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">functionCall</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"brightness"</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">else</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">fatalError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Missing argument: brightness"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">guard</span> <span class="devsite-syntax-k">case</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">string</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">colorTemperature</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">functionCall</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"colorTemperature"</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">else</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">fatalError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Missing argument: colorTemperature"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-c1">// Call the executable function named in the FunctionCall with the arguments specified in the</span> <span class="devsite-syntax-c1">// FunctionCall and let it call the hypothetical API.</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">apiResponse</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">controlLight</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">brightness</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">brightness</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">colorTemperature</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">colorTemperature</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Send the API response back to the model so it can generate a text response that can be</span> <span class="devsite-syntax-c1">// displayed to the user.</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">response2</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessage</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">ModelContent</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">role</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"function"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[.</span><span class="devsite-syntax-n">functionResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">FunctionResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"controlLight"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">apiResponse</span><span class="devsite-syntax-p">))]</span> <span class="devsite-syntax-p">)])</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response2</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/FunctionCalling.swift#L35-L106" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/FunctionCalling.swift#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/FunctionCalling.swift" data-indented-block="" data-regexp="" data-region-tag="function_calling"><span class="devsite-syntax-n">FunctionCalling</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="function_calling-DART" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/function_calling.dart/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/function_calling.dart" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-n">Map<String</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">Object</span><span class="devsite-syntax-o">?</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">setLightValues</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Map<String</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">Object</span><span class="devsite-syntax-o">?</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">controlLightFunction</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionDeclaration</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'controlLight'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Set the brightness and color temperature of a room light.'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">object</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nl">properties:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'brightness'</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">number</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">description:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Light level from 0 to 100. Zero is off and 100 is full brightness.'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">nullable:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'colorTemperatur'</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">string</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">description:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Color temperature of the light fixture which can be `daylight`, `cool`, or `warm`'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">nullable:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">false</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}));</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functions</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-n">controlLightFunction</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nl">name:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">setLightValues</span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-n">FunctionResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">dispatchFunctionCall</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">FunctionCall</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">call</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functions</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">call</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">]</span><span class="devsite-syntax-o">!</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">function</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">call</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">call</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-pro'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">tools:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nl">functionDeclarations:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">controlLightFunction</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Dim the lights so the room feels cozy and warm.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)];</span> <span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">List<FunctionCall></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functionCalls</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">while</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">((</span><span class="devsite-syntax-n">functionCalls</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">functionCalls</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">toList</span><span class="devsite-syntax-p">()).</span><span class="devsite-syntax-n">isNotEmpty</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><<span class="devsite-syntax-n">FunctionResponse</span>><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functionCall</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functionCalls</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">dispatchFunctionCall</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">functionCall</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">];</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">..</span><span class="devsite-syntax-n">add</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">candidates</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">first</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">..</span><span class="devsite-syntax-n">add</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">functionResponses</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Response: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s1">'</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/function_calling.dart#L30-L80" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/function_calling.dart#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/function_calling.dart" data-indented-block="" data-regexp="" data-region-tag="function_calling"><span class="devsite-syntax-n">function_calling</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="function_calling-JAVA" track-metadata-position="function_calling" track-metadata-region-tag="function_calling" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/function_calling.java/HEAD/function_calling" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/function_calling.java" data-git-revision="HEAD" data-region-tag="function_calling" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">FunctionDeclaration</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">multiplyDefinition</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">defineFunction</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* name */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"multiply"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* description */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"returns a * b."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* parameters */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">numDouble</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"a"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"First parameter"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Schema</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">numDouble</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"b"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Second parameter"</span><span class="devsite-syntax-p">)),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* required */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"a"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"b"</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">tool</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Tool</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">multiplyDefinition</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* generationConfig (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* safetySettings (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* requestOptions (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">RequestOptions</span><span class="devsite-syntax-p">(),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* functionDeclarations (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">tool</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Create prompt</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"I have 57 cats, each owns 44 mittens, how many mittens is that in total?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// Initialize the chat</span> <span class="devsite-syntax-n">ChatFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startChat</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// Send the message</span> <span class="devsite-syntax-n">ListenableFuture<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Futures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addCallback</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FutureCallback<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSuccess</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">!</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getFunctionCalls</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">isEmpty</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">handleFunctionCall</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">!</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">isEmpty</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onFailure</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">handleFunctionCall</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionCallPart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">multiplyFunctionCallPart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getFunctionCalls</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">stream</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">filter</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">fun</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fun</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getName</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">equals</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"multiply"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">findFirst</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">get</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">double</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Double</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">parseDouble</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">multiplyFunctionCallPart</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getArgs</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">get</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"a"</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">double</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">b</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Double</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">parseDouble</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">multiplyFunctionCallPart</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getArgs</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">get</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"b"</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// `multiply(a, b)` is a regular java function defined in another class</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionResponsePart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functionResponsePart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FunctionResponsePart</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"multiply"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JSONObject</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">put</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"result"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">multiply</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">b</span><span class="devsite-syntax-p">)));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create prompt</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">functionCallResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">functionResponsePart</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">JSONException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">throw</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">RuntimeException</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-p">);</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/function_calling.java#L54-L140" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/function_calling.java#function_calling" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/function_calling.java" data-indented-block="" data-regexp="" data-region-tag="function_calling"><span class="devsite-syntax-n">function_calling</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="generation-config" data-text="Generation config" tabindex="-1">Generation config</h3> <devsite-selector> <section> <h3 id="configure_model_parameters-PYTHON" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/configure_model_parameters.py/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/configure_model_parameters.py" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-s2">"Tell me a story about a magic backpack."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">generation_config</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">types</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1"># Only one candidate for now.</span> <span class="devsite-syntax-n">candidate_count</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">stop_sequences</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"x"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">max_output_tokens</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-mi">20</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">temperature</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-mf">1.0</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/configure_model_parameters.py#L23-L35" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/configure_model_parameters.py#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/configure_model_parameters.py" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters"><span class="devsite-syntax-n">configure_model_parameters</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model-JAVASCRIPT" track-metadata-position="configure_model" track-metadata-region-tag="configure_model" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/model_configuration.js/HEAD/configure_model" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/model_configuration.js" data-git-revision="HEAD" data-region-tag="configure_model" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">generationConfig</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">candidateCount</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">stopSequences</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"x"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">maxOutputTokens</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">20</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">temperature</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1.0</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Tell me a story about a magic backpack."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/model_configuration.js#L22-L38" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/model_configuration.js#configure_model" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/model_configuration.js" data-indented-block="" data-regexp="" data-region-tag="configure_model"><span class="devsite-syntax-nx">model_configuration</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model_parameters-GO" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-pro-latest"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SetTemperature</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mf">0.9</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SetTopP</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mf">0.5</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SetTopK</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">20</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SetMaxOutputTokens</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">100</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SystemInstruction</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">NewUserContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You are Yoda from Star Wars."</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ResponseMIMEType</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"application/json"</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What is the average size of a swallow?"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L238-L249" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model_parameters-SHELL" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/configure_model_parameters.sh/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/configure_model_parameters.sh" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span>https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key<span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Write a story about a magic backpack."}</span> <span class="devsite-syntax-s1"> ]</span> <span class="devsite-syntax-s1"> }],</span> <span class="devsite-syntax-s1"> "safetySettings": [</span> <span class="devsite-syntax-s1"> {</span> <span class="devsite-syntax-s1"> "category": "HARM_CATEGORY_DANGEROUS_CONTENT",</span> <span class="devsite-syntax-s1"> "threshold": "BLOCK_ONLY_HIGH"</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> ],</span> <span class="devsite-syntax-s1"> "generationConfig": {</span> <span class="devsite-syntax-s1"> "stopSequences": [</span> <span class="devsite-syntax-s1"> "Title"</span> <span class="devsite-syntax-s1"> ],</span> <span class="devsite-syntax-s1"> "temperature": 1.0,</span> <span class="devsite-syntax-s1"> "maxOutputTokens": 800,</span> <span class="devsite-syntax-s1"> "topP": 0.8,</span> <span class="devsite-syntax-s1"> "topK": 10</span> <span class="devsite-syntax-s1"> }</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>grep<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"text"</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/configure_model_parameters.sh#L4-L29" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/configure_model_parameters.sh#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/configure_model_parameters.sh" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters">configure_model_parameters.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model_parameters-KOTLIN" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/configure_model_parameters.kt/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/configure_model_parameters.kt" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">config</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">temperature</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">0.9f</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">topK</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">16</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">topP</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">0.1f</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">maxOutputTokens</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">200</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">stopSequences</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"red"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">config</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/configure_model_parameters.kt#L32-L45" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/configure_model_parameters.kt#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/configure_model_parameters.kt" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters"><span class="devsite-syntax-n">configure_model_parameters</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model_parameters-SWIFT" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/GenerationConfig.swift/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/GenerationConfig.swift" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">config</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">temperature</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-mf">0.9</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">topP</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-mf">0.1</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">topK</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-mi">16</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">candidateCount</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-mi">1</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">maxOutputTokens</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-mi">200</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">stopSequences</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s">"red"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s">"orange"</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">config</span> <span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/GenerationConfig.swift#L35-L52" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/GenerationConfig.swift#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/GenerationConfig.swift" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters"><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model_parameters-DART" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/configure_model_parameters.dart/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/configure_model_parameters.dart" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Tell me a story about a magic backpack.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">generationConfig:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">candidateCount:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">1</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">stopSequences:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'x'</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">maxOutputTokens:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">20</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">temperature:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">1.0</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/configure_model_parameters.dart#L30-L45" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/configure_model_parameters.dart#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/configure_model_parameters.dart" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters"><span class="devsite-syntax-n">configure_model_parameters</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="configure_model_parameters-JAVA" track-metadata-position="configure_model_parameters" track-metadata-region-tag="configure_model_parameters" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/configure_model_parameters.java/HEAD/configure_model_parameters" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/configure_model_parameters.java" data-git-revision="HEAD" data-region-tag="configure_model_parameters" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">temperature</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0.9f</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">topK</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">16</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">topP</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0.1f</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">maxOutputTokens</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">200</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">stopSequences</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"red"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">configBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generationConfig</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/configure_model_parameters.java#L32-L45" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/configure_model_parameters.java#configure_model_parameters" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/configure_model_parameters.java" data-indented-block="" data-regexp="" data-region-tag="configure_model_parameters"><span class="devsite-syntax-n">configure_model_parameters</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="safety-settings" data-text="Safety Settings" tabindex="-1">Safety Settings</h3> <devsite-selector> <section> <h3 id="safety_settings_multi-PYTHON" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/safety_settings.py/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/safety_settings.py" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">unsafe_prompt</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them."</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">unsafe_prompt</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">safety_settings</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-s2">"HATE"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"MEDIUM"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"HARASSMENT"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"BLOCK_ONLY_HIGH"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># If you want to set all the safety_settings to the same value you can just pass that value:</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">unsafe_prompt</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">safety_settings</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"MEDIUM"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"No information generated by the model."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">candidates</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-mi">0</span><span class="devsite-syntax-p">]</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">safety_ratings</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/safety_settings.py#L36-L52" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/safety_settings.py#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/safety_settings.py" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-n">safety_settings</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-JAVASCRIPT" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/safety_settings.js/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/safety_settings.js" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">safetySettings</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">category</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">HARM_CATEGORY_HARASSMENT</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">threshold</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">HarmBlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">BLOCK_MEDIUM_AND_ABOVE</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">category</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">HARM_CATEGORY_HATE_SPEECH</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">threshold</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">HarmBlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">BLOCK_LOW_AND_ABOVE</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">unsafePrompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"I support Martians Soccer Club and I think "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Jupiterians Football Club sucks! Write an ironic phrase telling "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"them how I feel about them."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">unsafePrompt</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">error</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">e</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">candidates</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-mf">0</span><span class="devsite-syntax-p">].</span><span class="devsite-syntax-nx">safetyRatings</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/safety_settings.js#L57-L86" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/safety_settings.js#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/safety_settings.js" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-nx">safety_settings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-GO" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SafetySettings</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SafetySetting</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Category</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">HarmCategoryDangerousContent</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Threshold</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">HarmBlockLowAndAbove</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Category</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">HarmCategoryHarassment</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Threshold</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">HarmBlockMediumAndAbove</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them."</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L305-L320" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-SHELL" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/safety_settings.sh/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/safety_settings.sh" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "safetySettings": [</span> <span class="devsite-syntax-s1"> {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"},</span> <span class="devsite-syntax-s1"> {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}</span> <span class="devsite-syntax-s1"> ],</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "'</span>I<span class="devsite-syntax-w"> </span>support<span class="devsite-syntax-w"> </span>Martians<span class="devsite-syntax-w"> </span>Soccer<span class="devsite-syntax-w"> </span>Club<span class="devsite-syntax-w"> </span>and<span class="devsite-syntax-w"> </span>I<span class="devsite-syntax-w"> </span>think<span class="devsite-syntax-w"> </span>Jupiterians<span class="devsite-syntax-w"> </span>Football<span class="devsite-syntax-w"> </span>Club<span class="devsite-syntax-w"> </span>sucks!<span class="devsite-syntax-w"> </span>Write<span class="devsite-syntax-w"> </span>a<span class="devsite-syntax-w"> </span>ironic<span class="devsite-syntax-w"> </span>phrase<span class="devsite-syntax-w"> </span>about<span class="devsite-syntax-w"> </span>them.<span class="devsite-syntax-s1">'"}]}]}'</span><span class="devsite-syntax-w"> > </span>request.json curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span>@request.json<span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/safety_settings.sh#L20-L33" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/safety_settings.sh#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/safety_settings.sh" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi">safety_settings.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-KOTLIN" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/safety_settings.kt/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/safety_settings.kt" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">harassmentSafety</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">HARASSMENT</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ONLY_HIGH</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">hateSpeechSafety</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">HATE_SPEECH</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MEDIUM_AND_ABOVE</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// The Gemini 1.5 models are versatile and work with most use cases</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">safetySettings</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">harassmentSafety</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">hateSpeechSafety</span><span class="devsite-syntax-p">))</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/safety_settings.kt#L48-L57" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/safety_settings.kt#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/safety_settings.kt" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-n">safety_settings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-SWIFT" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/SafetySettings.swift/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/SafetySettings.swift" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">safetySettings</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-p">[</span> <span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">harmCategory</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dangerousContent</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">threshold</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">blockLowAndAbove</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">harmCategory</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">harassment</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">threshold</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">blockMediumAndAbove</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">harmCategory</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">hateSpeech</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">threshold</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">blockOnlyHigh</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">]</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">safetySettings</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">safetySettings</span> <span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/SafetySettings.swift#L52-L66" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/SafetySettings.swift#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/SafetySettings.swift" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-n">SafetySettings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-DART" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/safety_settings.dart/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/safety_settings.dart" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'I support Martians Soccer Club and I think '</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Jupiterians Football Club sucks! Write an ironic phrase telling '</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'them how I feel about them.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">safetySettings:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">harassment</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">HarmBlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">medium</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">hateSpeech</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">HarmBlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">low</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetyRating</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-n">category</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-n">probability</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">candidates</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">first</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">safetyRatings</span><span class="devsite-syntax-o">!</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Safety Rating: </span><span class="devsite-syntax-si">$</span><span class="devsite-syntax-n">category</span><span class="devsite-syntax-s1"> - </span><span class="devsite-syntax-si">$</span><span class="devsite-syntax-n">probability</span><span class="devsite-syntax-s1">'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/safety_settings.dart#L60-L85" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/safety_settings.dart#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/safety_settings.dart" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-n">safety_settings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="safety_settings_multi-JAVA" track-metadata-position="safety_settings_multi" track-metadata-region-tag="safety_settings_multi" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/safety_settings.java/HEAD/safety_settings_multi" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/safety_settings.java" data-git-revision="HEAD" data-region-tag="safety_settings_multi" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">harassmentSafety</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">HARASSMENT</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ONLY_HIGH</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">hateSpeechSafety</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SafetySetting</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HarmCategory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">HATE_SPEECH</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BlockThreshold</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MEDIUM_AND_ABOVE</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// generation config is optional</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">harassmentSafety</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">hateSpeechSafety</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/safety_settings.java#L52-L66" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/safety_settings.java#safety_settings_multi" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/safety_settings.java" data-indented-block="" data-regexp="" data-region-tag="safety_settings_multi"><span class="devsite-syntax-n">safety_settings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="system-instruction" data-text="System Instruction" tabindex="-1">System Instruction</h3> <devsite-selector> <section> <h3 id="system_instruction-PYTHON" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/system_instruction.py/HEAD/system_instruction" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/system_instruction.py" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-s2">"models/gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">system_instruction</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s2">"You are a cat. Your name is Neko."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Good morning! How are you?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/system_instruction.py#L23-L28" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/system_instruction.py#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/system_instruction.py" data-indented-block="" data-regexp="" data-region-tag="system_instruction"><span class="devsite-syntax-n">system_instruction</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instruction-JAVASCRIPT" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/system_instruction.js/HEAD/system_instruction" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/system_instruction.js" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">systemInstruction</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"You are a cat. Your name is Neko."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Good morning! How are you?"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/system_instruction.js#L22-L35" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/system_instruction.js#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/system_instruction.js" data-indented-block="" data-regexp="" data-region-tag="system_instruction"><span class="devsite-syntax-nx">system_instruction</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instruction-GO" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/system_instruction" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SystemInstruction</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">NewUserContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You are a cat. Your name is Neko."</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Good morning! How are you?"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L262-L268" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="system_instruction"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instruction-SHELL" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/system_instruction.sh/HEAD/system_instruction" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/system_instruction.sh" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> -H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> -d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{ "system_instruction": {</span> <span class="devsite-syntax-s1"> "parts":</span> <span class="devsite-syntax-s1"> { "text": "You are a cat. Your name is Neko."}},</span> <span class="devsite-syntax-s1"> "contents": {</span> <span class="devsite-syntax-s1"> "parts": {</span> <span class="devsite-syntax-s1"> "text": "Hello there"}}}'</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/system_instruction.sh#L4-L12" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/system_instruction.sh#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/system_instruction.sh" data-indented-block="" data-regexp="" data-region-tag="system_instruction">system_instruction.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instruction-KOTLIN" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/system_instruction.kt/HEAD/system_instruction" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/system_instruction.kt" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">systemInstruction</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You are a cat. Your name is Neko."</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/system_instruction.kt#L31-L37" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/system_instruction.kt#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/system_instruction.kt" data-indented-block="" data-regexp="" data-region-tag="system_instruction"><span class="devsite-syntax-n">system_instruction</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instruction-SWIFT" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/SystemInstructions.swift/HEAD/system_instruction" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/SystemInstructions.swift" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a model that supports system instructions, like a Gemini 1.5 model</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">systemInstruction</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">ModelContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"system"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"You are a cat. Your name is Neko."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/SystemInstructions.swift#L35-L43" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/SystemInstructions.swift#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/SystemInstructions.swift" data-indented-block="" data-regexp="" data-region-tag="system_instruction"><span class="devsite-syntax-n">SystemInstructions</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instructions-DART" track-metadata-position="system_instructions" track-metadata-region-tag="system_instructions" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/system_instructions.dart/HEAD/system_instructions" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/system_instructions.dart" data-git-revision="HEAD" data-region-tag="system_instructions" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">systemInstruction:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">system</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'You are a cat. Your name is Neko.'</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Good morning! How are you?'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContent</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)]);</span> <span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/system_instructions.dart#L30-L40" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/system_instructions.dart#system_instructions" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/system_instructions.dart" data-indented-block="" data-regexp="" data-region-tag="system_instructions"><span class="devsite-syntax-n">system_instructions</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="system_instruction-JAVA" track-metadata-position="system_instruction" track-metadata-region-tag="system_instruction" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/system_instruction.java/HEAD/system_instruction" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/system_instruction.java" data-git-revision="HEAD" data-region-tag="system_instruction" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* generationConfig (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* safetySettings (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* requestOptions (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">RequestOptions</span><span class="devsite-syntax-p">(),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* tools (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* toolsConfig (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* systemInstruction (optional) */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"You are a cat. Your name is Neko."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">());</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/system_instruction.java#L31-L43" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/system_instruction.java#system_instruction" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/system_instruction.java" data-indented-block="" data-regexp="" data-region-tag="system_instruction"><span class="devsite-syntax-n">system_instruction</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> </devsite-selector> </div> </div> <div class="column-container"> <div class="reference"> <section id="body.response_body"> <h3 id="response-body" data-text="Response body" tabindex="-1">Response body</h3> <p>If successful, the response body contains an instance of <code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.GenerateContentResponse">GenerateContentResponse</a></code>.</p> </section> </div> </div> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="method:-models.streamgeneratecontent" data-text="Method: models.streamGenerateContent" tabindex="-1">Method: models.streamGenerateContent</h2><a name="v1beta.models.streamGenerateContent"></a> <meta content="streamGenerateContent" itemprop="name"/> <meta content="/api/rest/v1beta/models/streamGenerateContent" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/models/streamGenerateContent"> <ul class="toc"> <li><a href="#body.HTTP_TEMPLATE">Endpoint</a></li><li><a href="#body.PATH_PARAMETERS">Path parameters</a></li><li><a href="#body.request_body">Request body</a> <ul> <li><a href="#body.request_body.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#body.response_body">Response body</a></li><li><a href="#body.aspect">Authorization scopes</a></li><li><a href="#body.codeSnippets">Example request</a> <ul> <li><a href="#body.codeSnippets.group">Text</a></li><li><a href="#body.codeSnippets.group_1">Image</a></li><li><a href="#body.codeSnippets.group_2">Audio</a></li><li><a href="#body.codeSnippets.group_3">Video</a></li><li><a href="#body.codeSnippets.group_4">PDF</a></li><li><a href="#body.codeSnippets.group_5">Chat</a></li> </ul> </li> </ul> <section id="google.ai.generativelanguage.v1beta.GenerativeService.StreamGenerateContent"> </section> <section id="description"> <p>Generates a <a href="https://ai.google.dev/gemini-api/docs/text-generation?lang=python#generate-a-text-stream">streamed response</a> from the model given an input <code translate="no" dir="ltr">GenerateContentRequest</code>.</p> </section> <div class="column-container"> <div class="reference"> <section id="body.HTTP_TEMPLATE"> <h3 id="endpoint_1" data-text="Endpoint" tabindex="-1">Endpoint</h3> <span class="endpoint"> <span class="http-method"> post </span> <span class="endpoint-url"> https://generativelanguage.googleapis.com/v1beta/{model=models/*}:streamGenerateContent </span> </span> <br/> <span> </span> </section> <section id="body.PATH_PARAMETERS"> <h3 id="path-parameters_1" data-text="Path parameters" tabindex="-1">Path parameters</h3> <section id="body.PATH_PARAMETERS.model"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">model</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Required. The name of the <code translate="no" dir="ltr">Model</code> to use for generating the completion.</p><p>Format: <code translate="no" dir="ltr">name=models/{model}</code>. It takes the form <code translate="no" dir="ltr">models/{model}</code>.</p> </div> </div> </section> </section> <section id="body.request_body"> <h3 id="request-body_1" data-text="Request body" tabindex="-1">Request body</h3> <p>The request body contains data with the following structure:</p> <section id="body.request_body.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="body.request_body.FIELDS.contents"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">contents[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</code> </span> </div> <div class="field-description"> <p>Required. The content of the current conversation with the model.</p><p>For single-turn queries, this is a single instance. For multi-turn queries like <a href="https://ai.google.dev/gemini-api/docs/text-generation#chat">chat</a>, this is a repeated field that contains the conversation history and the latest request.</p> </div> </div> </section> <section id="body.request_body.FIELDS.tools"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">tools[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Tool">Tool</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. A list of <code translate="no" dir="ltr">Tools</code> the <code translate="no" dir="ltr">Model</code> may use to generate the next response.</p><p>A <code translate="no" dir="ltr">Tool</code> is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the <code translate="no" dir="ltr">Model</code>. Supported <code translate="no" dir="ltr">Tool</code>s are <code translate="no" dir="ltr">Function</code> and <code translate="no" dir="ltr">codeExecution</code>. Refer to the <a href="https://ai.google.dev/gemini-api/docs/function-calling">Function calling</a> and the <a href="https://ai.google.dev/gemini-api/docs/code-execution">Code execution</a> guides to learn more.</p> </div> </div> </section> <section id="body.request_body.FIELDS.tool_config"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">toolConfig</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#ToolConfig">ToolConfig</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Tool configuration for any <code translate="no" dir="ltr">Tool</code> specified in the request. Refer to the <a href="https://ai.google.dev/gemini-api/docs/function-calling#function_calling_mode">Function calling guide</a> for a usage example.</p> </div> </div> </section> <section id="body.request_body.FIELDS.safety_settings"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">safetySettings[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.SafetySetting">SafetySetting</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. A list of unique <code translate="no" dir="ltr">SafetySetting</code> instances for blocking unsafe content.</p><p>This will be enforced on the <code translate="no" dir="ltr">GenerateContentRequest.contents</code> and <code translate="no" dir="ltr">GenerateContentResponse.candidates</code>. There should not be more than one setting for each <code translate="no" dir="ltr">SafetyCategory</code> type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each <code translate="no" dir="ltr">SafetyCategory</code> specified in the safetySettings. If there is no <code translate="no" dir="ltr">SafetySetting</code> for a given <code translate="no" dir="ltr">SafetyCategory</code> provided in the list, the API will use the default safety setting for that category. Harm categories HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT are supported. Refer to the <a href="https://ai.google.dev/gemini-api/docs/safety-settings">guide</a> for detailed information on available safety settings. Also refer to the <a href="https://ai.google.dev/gemini-api/docs/safety-guidance">Safety guidance</a> to learn how to incorporate safety considerations in your AI applications.</p> </div> </div> </section> <section id="body.request_body.FIELDS.system_instruction"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">systemInstruction</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Developer set <a href="https://ai.google.dev/gemini-api/docs/system-instructions">system instruction(s)</a>. Currently, text only.</p> </div> </div> </section> <section id="body.request_body.FIELDS.generation_config"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">generationConfig</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.GenerationConfig">GenerationConfig</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Configuration options for model generation and outputs.</p> </div> </div> </section> <section id="body.request_body.FIELDS.cached_content"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">cachedContent</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. The name of the content <a href="https://ai.google.dev/gemini-api/docs/caching">cached</a> to use as context to serve the prediction. Format: <code translate="no" dir="ltr">cachedContents/{cachedContent}</code></p> </div> </div> </section> </section> </section> </div> <div class="second-column"> <h3 id="example-request_1" data-text="Example request" tabindex="-1">Example request</h3> <devsite-selector> <section> <h3 id="text_1" data-text="Text" tabindex="-1">Text</h3> <devsite-selector> <section> <h3 id="text_gen_text_only_prompt_streaming-PYTHON" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">stream</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">True</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L33-L37" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-JAVASCRIPT" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Print text as it comes in.</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">of</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stream</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L43-L56" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-GO" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Next</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iterator</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Done</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">break</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L355-L366" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-SHELL" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?alt=sse&key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--no-buffer<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{ "contents":[{"parts":[{"text": "Write a story about a magic backpack."}]}]}'</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L33-L37" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-KOTLIN" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Write a story about a magic backpack."</span> <span class="devsite-syntax-c1">// Use streaming with text-only input</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">collect</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#L50-L60" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-SWIFT" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/TextGeneration.swift/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">prompt</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-s">"Write a story about a magic backpack."</span> <span class="devsite-syntax-c1">// Use streaming with text-only input</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-k">in</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/TextGeneration.swift#L55-L70" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/TextGeneration.swift#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-n">TextGeneration</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-DART" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Write a story about a magic backpack.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContentStream</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)]);</span> <span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/text_generation.dart#L45-L56" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_text_only_prompt_streaming-JAVA" track-metadata-position="text_gen_text_only_prompt_streaming" track-metadata-region-tag="text_gen_text_only_prompt_streaming" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java/HEAD/text_gen_text_only_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-git-revision="HEAD" data-region-tag="text_gen_text_only_prompt_streaming" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Write a story about a magic backpack."</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">Publisher<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">streamingResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">StringBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">StringBuilder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">streamingResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">subscribe</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onNext</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generateContentResponse</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generateContentResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">append</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onComplete</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSubscribe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">request</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Long</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MAX_VALUE</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#L80-L118" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#text_gen_text_only_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-indented-block="" data-regexp="" data-region-tag="text_gen_text_only_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="image_1" data-text="Image" tabindex="-1">Image</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-PYTHON" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">PIL.Image</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">organ</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">PIL</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Image</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">open</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"organ.jpg"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"Tell me about this instrument"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">organ</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">stream</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">True</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L52-L59" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-JAVASCRIPT" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileToGenerativePart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">path</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">inlineData</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">data</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Buffer</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-kr">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">fs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">readFileSync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">path</span><span class="devsite-syntax-p">)).</span><span class="devsite-syntax-nx">toString</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"base64"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Describe how this product might be manufactured."</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-c1">// Note: The only accepted mime types are some image types, image/*.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">imagePart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileToGenerativePart</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">mediaPath</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">/jetpack.jpg`</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"image/jpeg"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContentStream</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">imagePart</span><span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-c1">// Print text as it comes in.</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">of</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stream</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L90-L117" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-GO" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">imgData</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">os</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ReadFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">filepath</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">testDataDir</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"organ.jpg"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Tell me about this instrument"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ImageData</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"jpeg"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">imgData</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Next</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iterator</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Done</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">break</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L379-L397" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-SHELL" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">cat<span class="devsite-syntax-w"> > </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-nv">$TEMP_JSON</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> << </span>EOF <span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"contents"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"parts"</span>:<span class="devsite-syntax-o">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span><span class="devsite-syntax-s2">"text"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Tell me about this instrument"</span><span class="devsite-syntax-o">}</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"inline_data"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"mime_type"</span>:<span class="devsite-syntax-s2">"image/jpeg"</span>, <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"data"</span>:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">$(</span>cat<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-nv">$TEMP_B64</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">]</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">}]</span> <span class="devsite-syntax-o">}</span> EOF curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?alt=sse&key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-nv">$TEMP_JSON</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L74-L94" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-KOTLIN" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">image</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Bitmap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BitmapFactory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">decodeResource</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">resources</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">R</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">drawable</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">image</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">inputContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What's in this picture?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">inputContent</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">collect</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#L86-L99" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/text_generation.kt" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-SWIFT" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/TextGeneration.swift/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">guard</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">image</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-bp">UIImage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">systemName</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"cloud.sun"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">else</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">fatalError</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">prompt</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-s">"What's in this picture?"</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-k">in</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/TextGeneration.swift#L98-L115" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/TextGeneration.swift#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/TextGeneration.swift" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-n">TextGeneration</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-DART" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Future<DataPart></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fileToPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">mimeType</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DataPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">mimeType</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">File</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">readAsBytes</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Describe how this product might be manufactured.'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">fileToPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'image/jpeg'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'resources/jetpack.jpg'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">generateContentStream</span><span class="devsite-syntax-p">([</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">multi</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">TextPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">prompt</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/text_generation.dart#L85-L104" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/text_generation.dart" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_one_image_prompt_streaming-JAVA" track-metadata-position="text_gen_multimodal_one_image_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_one_image_prompt_streaming" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java/HEAD/text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_one_image_prompt_streaming" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Bitmap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image1</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BitmapFactory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">decodeResource</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getResources</span><span class="devsite-syntax-p">(),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">R</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">drawable</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">image1</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Bitmap</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">image2</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BitmapFactory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">decodeResource</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getResources</span><span class="devsite-syntax-p">(),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">R</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">drawable</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">image2</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"What's different between these pictures?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addImage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image1</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addImage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">image2</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// For illustrative purposes only. You should use an executor that fits your needs.</span> <span class="devsite-syntax-n">Executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executor</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Executors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newSingleThreadExecutor</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">Publisher<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">streamingResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">StringBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">StringBuilder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">streamingResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">subscribe</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onNext</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generateContentResponse</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generateContentResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">append</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onComplete</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printStackTrace</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSubscribe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">request</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Long</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MAX_VALUE</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#L165-L213" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java#text_gen_multimodal_one_image_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/text_generation.java" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_one_image_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="audio_1" data-text="Audio" tabindex="-1">Audio</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_audio_streaming-PYTHON" track-metadata-position="text_gen_multimodal_audio_streaming" track-metadata-region-tag="text_gen_multimodal_audio_streaming" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_audio_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_audio_streaming" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">sample_audio</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"sample.mp3"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"Give me a summary of this audio file."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">sample_audio</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L101-L107" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_audio_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_audio_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_audio_streaming-SHELL" track-metadata-position="text_gen_multimodal_audio_streaming" track-metadata-region-tag="text_gen_multimodal_audio_streaming" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_audio_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_audio_streaming" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># Use File API to upload audio data to API request.</span> <span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>file<span class="devsite-syntax-w"> </span>-b<span class="devsite-syntax-w"> </span>--mime-type<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">AUDIO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>wc<span class="devsite-syntax-w"> </span>-c<span class="devsite-syntax-w"> < </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">AUDIO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-o">=</span>AUDIO <span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-o">=</span>upload-header.tmp <span class="devsite-syntax-c1"># Initial resumable request defining metadata.</span> <span class="devsite-syntax-c1"># The upload url is in the response headers dump them to a file.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">BASE_URL</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">/upload/v1beta/files?key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-D<span class="devsite-syntax-w"> </span>upload-header.tmp<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Protocol: resumable"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: start"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Type: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Type: application/json"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"{'file': {'display_name': '</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">'}}"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null <span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>grep<span class="devsite-syntax-w"> </span>-i<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"x-goog-upload-url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>cut<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-s2">" "</span><span class="devsite-syntax-w"> </span>-f2<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>tr<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"\r"</span><span class="devsite-syntax-k">)</span> rm<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Upload the actual bytes.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Offset: 0"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: upload, finalize"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--data-binary<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">AUDIO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.uri"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$file_uri</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?alt=sse&key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Please describe this file."},</span> <span class="devsite-syntax-s1"> {"file_data":{"mime_type": "audio/mpeg", "file_uri": '</span><span class="devsite-syntax-nv">$file_uri</span><span class="devsite-syntax-s1">'}}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>response.json cat<span class="devsite-syntax-w"> </span>response.json echo<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L224-L268" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_audio_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_audio_streaming">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="video_1" data-text="Video" tabindex="-1">Video</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_video_prompt_streaming-PYTHON" track-metadata-position="text_gen_multimodal_video_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_video_prompt_streaming" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt_streaming" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">time</span> <span class="devsite-syntax-c1"># Video clip (CC BY 3.0) from https://peach.blender.org/download/</span> <span class="devsite-syntax-n">myfile</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"Big_Buck_Bunny.mp4"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-si">=}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Videos need to be processed before you can use them.</span> <span class="devsite-syntax-k">while</span> <span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">state</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">==</span> <span class="devsite-syntax-s2">"PROCESSING"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"processing video..."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">time</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">sleep</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">5</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">myfile</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">myfile</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"Describe this video clip"</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L131-L148" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_video_prompt_streaming-JAVASCRIPT" track-metadata-position="text_gen_multimodal_video_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_video_prompt_streaming" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/text_generation.js/HEAD/text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt_streaming" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-c1">// import { GoogleAIFileManager, FileState } from "@google/generative-ai/server";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleAIFileManager</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">uploadFile</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">mediaPath</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">/Big_Buck_Bunny.mp4`</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"video/mp4"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-k">while</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">state</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">===</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">FileState</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">PROCESSING</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Sleep for 10 seconds</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">Promise</span><span class="devsite-syntax-p">((</span><span class="devsite-syntax-nx">resolve</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setTimeout</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resolve</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">10</span><span class="devsite-syntax-nx">_000</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Fetch the file from the API again</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileManager</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">name</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">state</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">===</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">FileState</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">FAILED</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">throw</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ne">Error</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Video processing failed."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Describe this video clip"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">videoPart</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileData</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fileUri</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">uri</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">uploadResult</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">mimeType</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">};</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">generateContentStream</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-nx">prompt</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">videoPart</span><span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-c1">// Print text as it comes in.</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">of</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stream</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/text_generation.js#L262-L301" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/text_generation.js#text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/text_generation.js" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt_streaming"><span class="devsite-syntax-nx">text_generation</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_video_prompt_streaming-GO" track-metadata-position="text_gen_multimodal_video_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_video_prompt_streaming" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt_streaming" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">UploadFileFromPath</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">filepath</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">testDataDir</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"earth.mp4"</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">DeleteFile</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Name</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Describe this video clip"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">FileData</span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">URI</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">file</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">URI</span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Next</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iterator</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Done</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">break</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L410-L430" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt_streaming"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_video_prompt_streaming-SHELL" track-metadata-position="text_gen_multimodal_video_prompt_streaming" track-metadata-region-tag="text_gen_multimodal_video_prompt_streaming" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_video_prompt_streaming" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># Use File API to upload audio data to API request.</span> <span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>file<span class="devsite-syntax-w"> </span>-b<span class="devsite-syntax-w"> </span>--mime-type<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">VIDEO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>wc<span class="devsite-syntax-w"> </span>-c<span class="devsite-syntax-w"> < </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">VIDEO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-o">=</span>VIDEO_PATH <span class="devsite-syntax-c1"># Initial resumable request defining metadata.</span> <span class="devsite-syntax-c1"># The upload url is in the response headers dump them to a file.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">BASE_URL</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">/upload/v1beta/files?key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-D<span class="devsite-syntax-w"> </span>upload-header.tmp<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Protocol: resumable"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: start"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Type: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Type: application/json"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"{'file': {'display_name': '</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">'}}"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null <span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>grep<span class="devsite-syntax-w"> </span>-i<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"x-goog-upload-url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>cut<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-s2">" "</span><span class="devsite-syntax-w"> </span>-f2<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>tr<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"\r"</span><span class="devsite-syntax-k">)</span> rm<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Upload the actual bytes.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Offset: 0"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: upload, finalize"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--data-binary<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">VIDEO_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.uri"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$file_uri</span> <span class="devsite-syntax-nv">state</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.state"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">state</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$state</span> <span class="devsite-syntax-k">while</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[[</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"(</span><span class="devsite-syntax-nv">$state</span><span class="devsite-syntax-s2">)"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span>*<span class="devsite-syntax-s2">"PROCESSING"</span>*<span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">]]</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">do</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Processing video..."</span> <span class="devsite-syntax-w"> </span>sleep<span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">5</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Get the file of interest to check state</span> <span class="devsite-syntax-w"> </span>curl<span class="devsite-syntax-w"> </span>https://generativelanguage.googleapis.com/v1beta/files/<span class="devsite-syntax-nv">$name</span><span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">state</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.state"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-k">done</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?alt=sse&key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Please describe this file."},</span> <span class="devsite-syntax-s1"> {"file_data":{"mime_type": "video/mp4", "file_uri": '</span><span class="devsite-syntax-nv">$file_uri</span><span class="devsite-syntax-s1">'}}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>response.json cat<span class="devsite-syntax-w"> </span>response.json echo<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L335-L389" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_video_prompt_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_video_prompt_streaming">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="pdf_1" data-text="PDF" tabindex="-1">PDF</h3> <devsite-selector> <section> <h3 id="text_gen_multimodal_pdf_streaming-PYTHON" track-metadata-position="text_gen_multimodal_pdf_streaming" track-metadata-region-tag="text_gen_multimodal_pdf_streaming" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/text_generation.py/HEAD/text_gen_multimodal_pdf_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_pdf_streaming" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">sample_pdf</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">upload_file</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">media</span> <span class="devsite-syntax-o">/</span> <span class="devsite-syntax-s2">"test.pdf"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">generate_content</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"Give me a summary of this document:"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">sample_pdf</span><span class="devsite-syntax-p">])</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/text_generation.py#L161-L167" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/text_generation.py#text_gen_multimodal_pdf_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/text_generation.py" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_pdf_streaming"><span class="devsite-syntax-n">text_generation</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="text_gen_multimodal_pdf_streaming-SHELL" track-metadata-position="text_gen_multimodal_pdf_streaming" track-metadata-region-tag="text_gen_multimodal_pdf_streaming" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/text_generation.sh/HEAD/text_gen_multimodal_pdf_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-git-revision="HEAD" data-region-tag="text_gen_multimodal_pdf_streaming" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>file<span class="devsite-syntax-w"> </span>-b<span class="devsite-syntax-w"> </span>--mime-type<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">PDF_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>wc<span class="devsite-syntax-w"> </span>-c<span class="devsite-syntax-w"> < </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">PDF_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-o">=</span>TEXT <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">$MIME_TYPE</span> <span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-o">=</span>upload-header.tmp <span class="devsite-syntax-c1"># Initial resumable request defining metadata.</span> <span class="devsite-syntax-c1"># The upload url is in the response headers dump them to a file.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">BASE_URL</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">/upload/v1beta/files?key=</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">GOOGLE_API_KEY</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-D<span class="devsite-syntax-w"> </span>upload-header.tmp<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Protocol: resumable"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: start"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Header-Content-Type: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">MIME_TYPE</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Type: application/json"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"{'file': {'display_name': '</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">DISPLAY_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">'}}"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null <span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>grep<span class="devsite-syntax-w"> </span>-i<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"x-goog-upload-url: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>cut<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-s2">" "</span><span class="devsite-syntax-w"> </span>-f2<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>tr<span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"\r"</span><span class="devsite-syntax-k">)</span> rm<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">tmp_header_file</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-c1"># Upload the actual bytes.</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">upload_url</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Content-Length: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">NUM_BYTES</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Offset: 0"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"X-Goog-Upload-Command: upload, finalize"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>--data-binary<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"@</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nv">PDF_PATH</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>file_info.json <span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-k">$(</span>jq<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">".file.uri"</span><span class="devsite-syntax-w"> </span>file_info.json<span class="devsite-syntax-k">)</span> <span class="devsite-syntax-nb">echo</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">file_uri</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$file_uri</span> <span class="devsite-syntax-c1"># Now generate content using that file</span> curl<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?alt=sse&key=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [{</span> <span class="devsite-syntax-s1"> "parts":[</span> <span class="devsite-syntax-s1"> {"text": "Can you add a few more lines to this poem?"},</span> <span class="devsite-syntax-s1"> {"file_data":{"mime_type": "application/pdf", "file_uri": '</span><span class="devsite-syntax-nv">$file_uri</span><span class="devsite-syntax-s1">'}}]</span> <span class="devsite-syntax-s1"> }]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> > </span>response.json cat<span class="devsite-syntax-w"> </span>response.json echo<div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/text_generation.sh#L445-L491" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/text_generation.sh#text_gen_multimodal_pdf_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/text_generation.sh" data-indented-block="" data-regexp="" data-region-tag="text_gen_multimodal_pdf_streaming">text_generation.sh</a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> <section> <h3 id="chat_1" data-text="Chat" tabindex="-1">Chat</h3> <devsite-selector> <section> <h3 id="chat_streaming-PYTHON" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Python" tabindex="-1">Python</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/chat.py/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/chat.py" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">model</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">genai</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">chat</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">model</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start_chat</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">history</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-p">{</span><span class="devsite-syntax-s2">"role"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"parts"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"Hello"</span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">{</span><span class="devsite-syntax-s2">"role"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"parts"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s2">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">send_message</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"I have 2 dogs in my house."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">stream</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">True</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">send_message</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"How many paws are in my house?"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">stream</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">True</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"_"</span> <span class="devsite-syntax-o">*</span> <span class="devsite-syntax-mi">80</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">)</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/chat.py#L41-L57" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/chat.py#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/chat.py" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">py</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-JAVASCRIPT" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Node.js" tabindex="-1">Node.js</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-js/samples/chat.js/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-js/samples/chat.js" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include these imports:</span> <span class="devsite-syntax-c1">// import { GoogleGenerativeAI } from "@google/generative-ai";</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">GoogleGenerativeAI</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">env</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">API_KEY</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genAI</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">getGenerativeModel</span><span class="devsite-syntax-p">({</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"gemini-1.5-flash"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">startChat</span><span class="devsite-syntax-p">({</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">history</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">role</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">parts</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Hello"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">role</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">parts</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}],</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">});</span> <span class="devsite-syntax-kd">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">sendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"I have 2 dogs in my house."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">of</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stream</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">result</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">sendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"How many paws are in my house?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">of</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">result</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stream</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">text</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">process</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stdout</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">write</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">chunkText</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-js/blob/2df2af03bb07dcda23b07af1a7135a8b461ae64e/samples/chat.js#L53-L78" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-js/samples/chat.js#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-js/samples/chat.js" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-nx">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">js</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-GO" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Go" tabindex="-1">Go</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">GenerativeModel</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nx">cs</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">StartChat</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-nx">cs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">History</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Content</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Parts</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Part</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Role</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Parts</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Part</span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Role</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cs</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">genai</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iter</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Next</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">iterator</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Done</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">break</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fatal</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">printResponse</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">resp</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google/generative-ai-go/blob/f453d2d6ca8c5e76e7d8b8615b3339ce68cd378c/genai/internal/samples/docs-snippets_test.go#L1002-L1030" class="gc-analytics-event" data-category="github_link" data-label="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google/generative-ai-go/genai/internal/samples/docs-snippets_test.go" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-nx">docs</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-nx">snippets_test</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">go</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-SHELL" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Shell" tabindex="-1">Shell</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-python/samples/rest/chat.sh/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-python/samples/rest/chat.sh" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr">curl<span class="devsite-syntax-w"> </span>https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?alt<span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">sse&key</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-nv">$GOOGLE_API_KEY</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-H<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Content-Type: application/json'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-X<span class="devsite-syntax-w"> </span>POST<span class="devsite-syntax-w"> </span><span class="devsite-syntax-se">\</span> <span class="devsite-syntax-w"> </span>-d<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'{</span> <span class="devsite-syntax-s1"> "contents": [</span> <span class="devsite-syntax-s1"> {"role":"user",</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "Hello"}]},</span> <span class="devsite-syntax-s1"> {"role": "model",</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "Great to meet you. What would you like to know?"}]},</span> <span class="devsite-syntax-s1"> {"role":"user",</span> <span class="devsite-syntax-s1"> "parts":[{</span> <span class="devsite-syntax-s1"> "text": "I have two dogs in my house. How many paws are in my house?"}]},</span> <span class="devsite-syntax-s1"> ]</span> <span class="devsite-syntax-s1"> }'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">2</span>><span class="devsite-syntax-w"> </span>/dev/null<span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">|</span><span class="devsite-syntax-w"> </span>grep<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"text"</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-python/blob/a04fcd185eefeb13955f0468c86c927d52436590/samples/rest/chat.sh#L27-L43" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-python/samples/rest/chat.sh#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-python/samples/rest/chat.sh" data-indented-block="" data-regexp="" data-region-tag="chat_streaming">chat.sh</a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-KOTLIN" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Kotlin" tabindex="-1">Kotlin</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Kotlin"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Use streaming with multi-turn conversations (like chat)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">generativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key" above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">val</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nv">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startChat</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">listOf</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">content</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}))</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">sendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">collect</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">text</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt#L58-L75" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/chat.kt" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">kt</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-SWIFT" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Swift" tabindex="-1">Swift</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-swift/samples/ChatSnippets.swift/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-swift/samples/ChatSnippets.swift" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Swift"><code translate="no" dir="ltr"><span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">generativeModel</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">name</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-c1">// Access your API key from your on-demand resource .plist file (see "Set up your API key"</span> <span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">APIKey</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-k">default</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// Optionally specify existing chat history</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">history</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-p">[</span> <span class="devsite-syntax-n">ModelContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-n">ModelContent</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">role</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parts</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">]</span> <span class="devsite-syntax-c1">// Initialize the chat with optional chat history</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">chat</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">generativeModel</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">startChat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1">// To stream generated text output, call sendMessageStream and pass in the message</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">contentStream</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-k">try</span> <span class="devsite-syntax-k">await</span> <span class="devsite-syntax-n">chunk</span> <span class="devsite-syntax-k">in</span> <span class="devsite-syntax-n">contentStream</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-kd">let</span> <span class="devsite-syntax-nv">text</span> <span class="devsite-syntax-p">=</span> <span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-bp">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-swift/blob/44b8ce120425f9cf53ca756f3434ca2c2696f8bd/samples/ChatSnippets.swift#L63-L87" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-swift/samples/ChatSnippets.swift#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-swift/samples/ChatSnippets.swift" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-n">ChatSnippets</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">swift</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-DART" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Dart" tabindex="-1">Dart</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Dart"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Make sure to include this import:</span> <span class="devsite-syntax-c1">// import 'package:google_generative_ai/google_generative_ai.dart';</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">model:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'gemini-1.5-flash'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nl">apiKey:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">apiKey</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-p">);</span> <span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">startChat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nl">history:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'hello'</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-n">TextPart</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Great to meet you. What would you like to know?'</span><span class="devsite-syntax-p">)])</span> <span class="devsite-syntax-p">]);</span> <span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'I have 2 dogs in my house.'</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'_'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">80</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span> <span class="devsite-syntax-n">responses</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">sendMessageStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'How many paws are in my house?'</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-kd">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">responses</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'_'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">80</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span><div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-dart/blob/76ae8f89eed4789af0be9e7876d0089909abc419/samples/dart/bin/chat.dart#L51-L72" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-dart/samples/dart/bin/chat.dart" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">dart</span></a></div></code></pre></devsite-code> </div> </section> <section> <h3 id="chat_streaming-JAVA" track-metadata-position="chat_streaming" track-metadata-region-tag="chat_streaming" data-text="Java" tabindex="-1">Java</h3> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java/HEAD/chat_streaming" data-code-snippet="true" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java" data-git-revision="HEAD" data-region-tag="chat_streaming" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">// Specify a Gemini model appropriate for your use case</span> <span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModel</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* modelName */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"gemini-1.5-flash"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Access your API key as a Build Configuration variable (see "Set up your API key"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// above)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/* apiKey */</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BuildConfig</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">apiKey</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GenerativeModelFutures</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">from</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">gm</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// (optional) Create previous chat history for context</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Hello, I have 2 dogs in my house."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelContentBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">modelContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"model"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">modelContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Great to meet you. What would you like to know?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userContentBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">List<Content></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Arrays</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">asList</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">userContent</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">modelContent</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Initialize the chat</span> <span class="devsite-syntax-n">ChatFutures</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startChat</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">history</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Create a new user message</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Content</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setRole</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addText</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"How many paws are in my house?"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">Content</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">userMessageBuilder</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-c1">// Use streaming with text-only input</span> <span class="devsite-syntax-n">Publisher<GenerateContentResponse></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">streamingResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">model</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">generateContentStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">userMessage</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-n">StringBuilder</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">StringBuilder</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-n">streamingResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">subscribe</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber<GenerateContentResponse></span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onNext</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">GenerateContentResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generateContentResponse</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generateContentResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getText</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">append</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">chunk</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onComplete</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">outputContent</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onSubscribe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">request</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Long</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MAX_VALUE</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nd">@Override</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">onError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">t</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <div class="devsite-github-link nocode no-select"><a target="_top" href="https://github.com/google-gemini/generative-ai-android/blob/b5d2e5610d19522ad10e6dc7e98236689aaa3587/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java#L104-L162" class="gc-analytics-event" data-category="github_link" data-label="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java#chat_streaming" data-code-snippet="true" data-git-revision="HEAD" data-github-path="google-gemini/generative-ai-android/samples/src/main/java/com/google/ai/client/generative/samples/java/chat.java" data-indented-block="" data-regexp="" data-region-tag="chat_streaming"><span class="devsite-syntax-n">chat</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span></a></div></code></pre></devsite-code> </div> </section> </devsite-selector> </section> </devsite-selector> </div> </div> <div class="column-container"> <div class="reference"> <section id="body.response_body"> <h3 id="response-body_1" data-text="Response body" tabindex="-1">Response body</h3> <p>If successful, the response body contains a stream of <code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.GenerateContentResponse">GenerateContentResponse</a></code> instances.</p> </section> </div> </div> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="generatecontentresponse" data-text="GenerateContentResponse" tabindex="-1">GenerateContentResponse</h2><a name="v1beta.GenerateContentResponse"></a> <meta content="GenerateContentResponse" itemprop="name"/> <meta content="/api/rest/v1beta/GenerateContentResponse" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/GenerateContentResponse"> <ul class="toc"> <li><a href="#SCHEMA_REPRESENTATION">JSON representation</a></li><li><a href="#PromptFeedback">PromptFeedback</a> <ul> <li><a href="#PromptFeedback.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#BlockReason">BlockReason</a></li><li><a href="#UsageMetadata">UsageMetadata</a> <ul> <li><a href="#UsageMetadata.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li> </ul> <div class="column-container"> <div class="reference"> <section id="description"> <p>Response from the model supporting multiple candidate responses.</p><p>Safety ratings and content filtering are reported for both prompt in <code translate="no" dir="ltr">GenerateContentResponse.prompt_feedback</code> and for each candidate in <code translate="no" dir="ltr">finishReason</code> and in <code translate="no" dir="ltr">safetyRatings</code>. The API: - Returns either all requested candidates or none of them - Returns no candidates at all only if there was something wrong with the prompt (check <code translate="no" dir="ltr">promptFeedback</code>) - Reports feedback on each candidate in <code translate="no" dir="ltr">finishReason</code> and <code translate="no" dir="ltr">safetyRatings</code>.</p> </section> <section id="FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="FIELDS.candidates"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">candidates[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.Candidate">Candidate</a></code>)</code> </span> </div> <div class="field-description"> <p>Candidate responses from the model.</p> </div> </div> </section> <section id="FIELDS.prompt_feedback"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">promptFeedback</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#PromptFeedback">PromptFeedback</a></code>)</code> </span> </div> <div class="field-description"> <p>Returns the prompt's feedback related to the content filters.</p> </div> </div> </section> <section id="FIELDS.usage_metadata"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">usageMetadata</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#UsageMetadata">UsageMetadata</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Metadata on the generation requests' token usage.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"candidates"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.Candidate">Candidate</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"promptFeedback"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#PromptFeedback">PromptFeedback</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"usageMetadata"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#UsageMetadata">UsageMetadata</a></code>)</span> <span class="pun">}</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="PromptFeedback" data-text="PromptFeedback" tabindex="-1">PromptFeedback</h2> <div class="column-container"> <div class="reference"> <section id="PromptFeedback.description"> <p>A set of the feedback metadata the prompt specified in <code translate="no" dir="ltr">GenerateContentRequest.content</code>.</p> </section> <section id="PromptFeedback.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="PromptFeedback.FIELDS.block_reason"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">blockReason</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#BlockReason">BlockReason</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. If set, the prompt was blocked and no candidates are returned. Rephrase the prompt.</p> </div> </div> </section> <section id="PromptFeedback.FIELDS.safety_ratings"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">safetyRatings[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.SafetyRating">SafetyRating</a></code>)</code> </span> </div> <div class="field-description"> <p>Ratings for safety of the prompt. There is at most one rating per category.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="PromptFeedback.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"blockReason"</span><span class="pun">: </span><span class="kwd">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#BlockReason">BlockReason</a></code>)</span><span class="pun">,</span> <span class="str">"safetyRatings"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.SafetyRating">SafetyRating</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="BlockReason" data-text="BlockReason" tabindex="-1">BlockReason</h2> <section id="BlockReason.description"> <p>Specifies the reason why the prompt was blocked.</p> </section> <section id="BlockReason.ENUM_VALUES"> <table class="constants responsive fixed" id="BlockReason.ENUM_VALUES-table"> <colgroup> <col width="25%"/> <col/> </colgroup> <thead> <tr> <th colspan="2">Enums</th> </tr> </thead> <tbody> <tr id="BlockReason.ENUM_VALUES.BLOCK_REASON_UNSPECIFIED"> <td><code class="apitype" translate="no" dir="ltr">BLOCK_REASON_UNSPECIFIED</code></td> <td>Default value. This value is unused.</td> </tr> <tr id="BlockReason.ENUM_VALUES.SAFETY"> <td><code class="apitype" translate="no" dir="ltr">SAFETY</code></td> <td>Prompt was blocked due to safety reasons. Inspect <code translate="no" dir="ltr">safetyRatings</code> to understand which safety category blocked it.</td> </tr> <tr id="BlockReason.ENUM_VALUES.OTHER"> <td><code class="apitype" translate="no" dir="ltr">OTHER</code></td> <td>Prompt was blocked due to unknown reasons.</td> </tr> <tr id="BlockReason.ENUM_VALUES.BLOCKLIST"> <td><code class="apitype" translate="no" dir="ltr">BLOCKLIST</code></td> <td>Prompt was blocked due to the terms which are included from the terminology blocklist.</td> </tr> <tr id="BlockReason.ENUM_VALUES.PROHIBITED_CONTENT"> <td><code class="apitype" translate="no" dir="ltr">PROHIBITED_CONTENT</code></td> <td>Prompt was blocked due to prohibited content.</td> </tr> </tbody> </table> </section> <h2 id="UsageMetadata" data-text="UsageMetadata" tabindex="-1">UsageMetadata</h2> <div class="column-container"> <div class="reference"> <section id="UsageMetadata.description"> <p>Metadata on the generation request's token usage.</p> </section> <section id="UsageMetadata.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="UsageMetadata.FIELDS.prompt_token_count"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">promptTokenCount</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Number of tokens in the prompt. When <code translate="no" dir="ltr">cachedContent</code> is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content.</p> </div> </div> </section> <section id="UsageMetadata.FIELDS.cached_content_token_count"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">cachedContentTokenCount</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Number of tokens in the cached part of the prompt (the cached content)</p> </div> </div> </section> <section id="UsageMetadata.FIELDS.candidates_token_count"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">candidatesTokenCount</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Total number of tokens across all the generated response candidates.</p> </div> </div> </section> <section id="UsageMetadata.FIELDS.total_token_count"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">totalTokenCount</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Total token count for the generation request (prompt + response candidates).</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="UsageMetadata.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"promptTokenCount"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"cachedContentTokenCount"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"candidatesTokenCount"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"totalTokenCount"</span><span class="pun">: </span><span class="kwd">integer</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="candidate" data-text="Candidate" tabindex="-1">Candidate</h2><a name="v1beta.Candidate"></a> <meta content="Candidate" itemprop="name"/> <meta content="/api/rest/v1beta/Candidate" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/Candidate"> <ul class="toc"> <li><a href="#SCHEMA_REPRESENTATION">JSON representation</a></li><li><a href="#FinishReason">FinishReason</a></li><li><a href="#GroundingAttribution">GroundingAttribution</a> <ul> <li><a href="#GroundingAttribution.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#AttributionSourceId">AttributionSourceId</a> <ul> <li><a href="#AttributionSourceId.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#GroundingPassageId">GroundingPassageId</a> <ul> <li><a href="#GroundingPassageId.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#SemanticRetrieverChunk">SemanticRetrieverChunk</a> <ul> <li><a href="#SemanticRetrieverChunk.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#GroundingMetadata">GroundingMetadata</a> <ul> <li><a href="#GroundingMetadata.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#SearchEntryPoint">SearchEntryPoint</a> <ul> <li><a href="#SearchEntryPoint.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#GroundingChunk">GroundingChunk</a> <ul> <li><a href="#GroundingChunk.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#Web">Web</a> <ul> <li><a href="#Web.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#GroundingSupport">GroundingSupport</a> <ul> <li><a href="#GroundingSupport.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#Segment">Segment</a> <ul> <li><a href="#Segment.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#RetrievalMetadata">RetrievalMetadata</a> <ul> <li><a href="#RetrievalMetadata.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#LogprobsResult">LogprobsResult</a> <ul> <li><a href="#LogprobsResult.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#TopCandidates">TopCandidates</a> <ul> <li><a href="#TopCandidates.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li><li><a href="#Candidate">Candidate</a> <ul> <li><a href="#Candidate.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li> </ul> <div class="column-container"> <div class="reference"> <section id="description"> <p>A response candidate generated from the model.</p> </section> <section id="FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="FIELDS.content"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">content</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Generated content returned from the model.</p> </div> </div> </section> <section id="FIELDS.finish_reason"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">finishReason</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#FinishReason">FinishReason</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Output only. The reason why the model stopped generating tokens.</p><p>If empty, the model has not stopped generating tokens.</p> </div> </div> </section> <section id="FIELDS.safety_ratings"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">safetyRatings[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.SafetyRating">SafetyRating</a></code>)</code> </span> </div> <div class="field-description"> <p>List of ratings for the safety of a response candidate.</p><p>There is at most one rating per category.</p> </div> </div> </section> <section id="FIELDS.citation_metadata"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">citationMetadata</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.CitationMetadata">CitationMetadata</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Citation information for model-generated candidate.</p><p>This field may be populated with recitation information for any text included in the <code translate="no" dir="ltr">content</code>. These are passages that are "recited" from copyrighted material in the foundational LLM's training data.</p> </div> </div> </section> <section id="FIELDS.token_count"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">tokenCount</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Output only. Token count for this candidate.</p> </div> </div> </section> <section id="FIELDS.grounding_attributions"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">groundingAttributions[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingAttribution">GroundingAttribution</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Attribution information for sources that contributed to a grounded answer.</p><p>This field is populated for <code translate="no" dir="ltr">GenerateAnswer</code> calls.</p> </div> </div> </section> <section id="FIELDS.grounding_metadata"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">groundingMetadata</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingMetadata">GroundingMetadata</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Grounding metadata for the candidate.</p><p>This field is populated for <code translate="no" dir="ltr">GenerateContent</code> calls.</p> </div> </div> </section> <section id="FIELDS.avg_logprobs"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">avgLogprobs</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Output only. </p> </div> </div> </section> <section id="FIELDS.logprobs_result"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">logprobsResult</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#LogprobsResult">LogprobsResult</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Log-likelihood scores for the response tokens and top tokens</p> </div> </div> </section> <section id="FIELDS.index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">index</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Output only. Index of the candidate in the list of response candidates.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"content"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"finishReason"</span><span class="pun">: </span><span class="kwd">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#FinishReason">FinishReason</a></code>)</span><span class="pun">,</span> <span class="str">"safetyRatings"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.SafetyRating">SafetyRating</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"citationMetadata"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.CitationMetadata">CitationMetadata</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"tokenCount"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"groundingAttributions"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingAttribution">GroundingAttribution</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"groundingMetadata"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingMetadata">GroundingMetadata</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"avgLogprobs"</span><span class="pun">: </span><span class="kwd">number</span><span class="pun">,</span> <span class="str">"logprobsResult"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#LogprobsResult">LogprobsResult</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"index"</span><span class="pun">: </span><span class="kwd">integer</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="FinishReason" data-text="FinishReason" tabindex="-1">FinishReason</h2> <section id="FinishReason.description"> <p>Defines the reason why the model stopped generating tokens.</p> </section> <section id="FinishReason.ENUM_VALUES"> <table class="constants responsive fixed" id="FinishReason.ENUM_VALUES-table"> <colgroup> <col width="25%"/> <col/> </colgroup> <thead> <tr> <th colspan="2">Enums</th> </tr> </thead> <tbody> <tr id="FinishReason.ENUM_VALUES.FINISH_REASON_UNSPECIFIED"> <td><code class="apitype" translate="no" dir="ltr">FINISH_REASON_UNSPECIFIED</code></td> <td>Default value. This value is unused.</td> </tr> <tr id="FinishReason.ENUM_VALUES.STOP"> <td><code class="apitype" translate="no" dir="ltr">STOP</code></td> <td>Natural stop point of the model or provided stop sequence.</td> </tr> <tr id="FinishReason.ENUM_VALUES.MAX_TOKENS"> <td><code class="apitype" translate="no" dir="ltr">MAX_TOKENS</code></td> <td>The maximum number of tokens as specified in the request was reached.</td> </tr> <tr id="FinishReason.ENUM_VALUES.SAFETY"> <td><code class="apitype" translate="no" dir="ltr">SAFETY</code></td> <td>The response candidate content was flagged for safety reasons.</td> </tr> <tr id="FinishReason.ENUM_VALUES.RECITATION"> <td><code class="apitype" translate="no" dir="ltr">RECITATION</code></td> <td>The response candidate content was flagged for recitation reasons.</td> </tr> <tr id="FinishReason.ENUM_VALUES.LANGUAGE"> <td><code class="apitype" translate="no" dir="ltr">LANGUAGE</code></td> <td>The response candidate content was flagged for using an unsupported language.</td> </tr> <tr id="FinishReason.ENUM_VALUES.OTHER"> <td><code class="apitype" translate="no" dir="ltr">OTHER</code></td> <td>Unknown reason.</td> </tr> <tr id="FinishReason.ENUM_VALUES.BLOCKLIST"> <td><code class="apitype" translate="no" dir="ltr">BLOCKLIST</code></td> <td>Token generation stopped because the content contains forbidden terms.</td> </tr> <tr id="FinishReason.ENUM_VALUES.PROHIBITED_CONTENT"> <td><code class="apitype" translate="no" dir="ltr">PROHIBITED_CONTENT</code></td> <td>Token generation stopped for potentially containing prohibited content.</td> </tr> <tr id="FinishReason.ENUM_VALUES.SPII"> <td><code class="apitype" translate="no" dir="ltr">SPII</code></td> <td>Token generation stopped because the content potentially contains Sensitive Personally Identifiable Information (SPII).</td> </tr> <tr id="FinishReason.ENUM_VALUES.MALFORMED_FUNCTION_CALL"> <td><code class="apitype" translate="no" dir="ltr">MALFORMED_FUNCTION_CALL</code></td> <td>The function call generated by the model is invalid.</td> </tr> </tbody> </table> </section> <h2 id="GroundingAttribution" data-text="GroundingAttribution" tabindex="-1">GroundingAttribution</h2> <div class="column-container"> <div class="reference"> <section id="GroundingAttribution.description"> <p>Attribution for a source that contributed to an answer.</p> </section> <section id="GroundingAttribution.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="GroundingAttribution.FIELDS.source_id"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">sourceId</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#AttributionSourceId">AttributionSourceId</a></code>)</code> </span> </div> <div class="field-description"> <p>Output only. Identifier for the source contributing to this attribution.</p> </div> </div> </section> <section id="GroundingAttribution.FIELDS.content"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">content</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</code> </span> </div> <div class="field-description"> <p>Grounding source content that makes up this attribution.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="GroundingAttribution.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"sourceId"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#AttributionSourceId">AttributionSourceId</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"content"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/caching#Content">Content</a></code>)</span> <span class="pun">}</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="AttributionSourceId" data-text="AttributionSourceId" tabindex="-1">AttributionSourceId</h2> <div class="column-container"> <div class="reference"> <section id="AttributionSourceId.description"> <p>Identifier for the source contributing to this attribution.</p> </section> <section id="AttributionSourceId.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <div class="field-entry union-type"> <tr class="alt" id="AttributionSourceId.FIELDS.oneof_source"> <div class="union-type-preamble"> <td colspan="2"><p>Union field <code translate="no" dir="ltr">source</code>.</p><p><code translate="no" dir="ltr">source</code> can be only one of the following:</p></td> </div> </tr> <section id="AttributionSourceId.FIELDS.grounding_passage"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">groundingPassage</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingPassageId">GroundingPassageId</a></code>)</code> </span> </div> <div class="field-description"> <p>Identifier for an inline passage.</p> </div> </div> </section> <section id="AttributionSourceId.FIELDS.semantic_retriever_chunk"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">semanticRetrieverChunk</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#SemanticRetrieverChunk">SemanticRetrieverChunk</a></code>)</code> </span> </div> <div class="field-description"> <p>Identifier for a <code translate="no" dir="ltr">Chunk</code> fetched via Semantic Retriever.</p> </div> </div> </section> </div> </section> </div> <div class="second-column"> <section id="AttributionSourceId.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="com">// Union field <code translate="no" dir="ltr">source</code> can be only one of the following:</span> <span class="str">"groundingPassage"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingPassageId">GroundingPassageId</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"semanticRetrieverChunk"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#SemanticRetrieverChunk">SemanticRetrieverChunk</a></code>)</span> <span class="pun">}</span> <span class="com">// End of list of possible types for union field <code translate="no" dir="ltr">source</code>.</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="GroundingPassageId" data-text="GroundingPassageId" tabindex="-1">GroundingPassageId</h2> <div class="column-container"> <div class="reference"> <section id="GroundingPassageId.description"> <p>Identifier for a part within a <code translate="no" dir="ltr">GroundingPassage</code>.</p> </section> <section id="GroundingPassageId.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="GroundingPassageId.FIELDS.passage_id"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">passageId</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Output only. ID of the passage matching the <code translate="no" dir="ltr">GenerateAnswerRequest</code>'s <code translate="no" dir="ltr">GroundingPassage.id</code>.</p> </div> </div> </section> <section id="GroundingPassageId.FIELDS.part_index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">partIndex</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Output only. Index of the part within the <code translate="no" dir="ltr">GenerateAnswerRequest</code>'s <code translate="no" dir="ltr">GroundingPassage.content</code>.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="GroundingPassageId.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"passageId"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"partIndex"</span><span class="pun">: </span><span class="kwd">integer</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="SemanticRetrieverChunk" data-text="SemanticRetrieverChunk" tabindex="-1">SemanticRetrieverChunk</h2> <div class="column-container"> <div class="reference"> <section id="SemanticRetrieverChunk.description"> <p>Identifier for a <code translate="no" dir="ltr">Chunk</code> retrieved via Semantic Retriever specified in the <code translate="no" dir="ltr">GenerateAnswerRequest</code> using <code translate="no" dir="ltr">SemanticRetrieverConfig</code>.</p> </section> <section id="SemanticRetrieverChunk.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="SemanticRetrieverChunk.FIELDS.source"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">source</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Output only. Name of the source matching the request's <code translate="no" dir="ltr">SemanticRetrieverConfig.source</code>. Example: <code translate="no" dir="ltr">corpora/123</code> or <code translate="no" dir="ltr">corpora/123/documents/abc</code></p> </div> </div> </section> <section id="SemanticRetrieverChunk.FIELDS.chunk"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">chunk</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Output only. Name of the <code translate="no" dir="ltr">Chunk</code> containing the attributed text. Example: <code translate="no" dir="ltr">corpora/123/documents/abc/chunks/xyz</code></p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SemanticRetrieverChunk.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"source"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"chunk"</span><span class="pun">: </span><span class="kwd">string</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="GroundingMetadata" data-text="GroundingMetadata" tabindex="-1">GroundingMetadata</h2> <div class="column-container"> <div class="reference"> <section id="GroundingMetadata.description"> <p>Metadata returned to client when grounding is enabled.</p> </section> <section id="GroundingMetadata.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="GroundingMetadata.FIELDS.grounding_chunks"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">groundingChunks[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingChunk">GroundingChunk</a></code>)</code> </span> </div> <div class="field-description"> <p>List of supporting references retrieved from specified grounding source.</p> </div> </div> </section> <section id="GroundingMetadata.FIELDS.grounding_supports"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">groundingSupports[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingSupport">GroundingSupport</a></code>)</code> </span> </div> <div class="field-description"> <p>List of grounding support.</p> </div> </div> </section> <section id="GroundingMetadata.FIELDS.web_search_queries"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">webSearchQueries[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Web search queries for the following-up web search.</p> </div> </div> </section> <section id="GroundingMetadata.FIELDS.search_entry_point"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">searchEntryPoint</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#SearchEntryPoint">SearchEntryPoint</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Google search entry for the following-up web searches.</p> </div> </div> </section> <section id="GroundingMetadata.FIELDS.retrieval_metadata"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">retrievalMetadata</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#RetrievalMetadata">RetrievalMetadata</a></code>)</code> </span> </div> <div class="field-description"> <p>Metadata related to retrieval in the grounding flow.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="GroundingMetadata.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"groundingChunks"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingChunk">GroundingChunk</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"groundingSupports"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#GroundingSupport">GroundingSupport</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"webSearchQueries"</span><span class="pun">: </span><span class="pun">[</span> <span class="kwd">string</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"searchEntryPoint"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#SearchEntryPoint">SearchEntryPoint</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"retrievalMetadata"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#RetrievalMetadata">RetrievalMetadata</a></code>)</span> <span class="pun">}</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="SearchEntryPoint" data-text="SearchEntryPoint" tabindex="-1">SearchEntryPoint</h2> <div class="column-container"> <div class="reference"> <section id="SearchEntryPoint.description"> <p>Google search entry point.</p> </section> <section id="SearchEntryPoint.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="SearchEntryPoint.FIELDS.rendered_content"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">renderedContent</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. Web content snippet that can be embedded in a web page or an app webview.</p> </div> </div> </section> <section id="SearchEntryPoint.FIELDS.sdk_blob"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">sdkBlob</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string (<a href="https://developers.google.com/discovery/v1/type-format">bytes</a> format)</code> </span> </div> <div class="field-description"> <p>Optional. Base64 encoded JSON representing array of <search term, search url> tuple.</p><p>A base64-encoded string.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SearchEntryPoint.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"renderedContent"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"sdkBlob"</span><span class="pun">: </span><span class="kwd">string</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="GroundingChunk" data-text="GroundingChunk" tabindex="-1">GroundingChunk</h2> <div class="column-container"> <div class="reference"> <section id="GroundingChunk.description"> <p>Grounding chunk.</p> </section> <section id="GroundingChunk.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <div class="field-entry union-type"> <tr class="alt" id="GroundingChunk.FIELDS.oneof_chunk_type"> <div class="union-type-preamble"> <td colspan="2">Union field <code translate="no" dir="ltr">chunk_type</code>. Chunk type. <code translate="no" dir="ltr">chunk_type</code> can be only one of the following:</td> </div> </tr> <section id="GroundingChunk.FIELDS.web"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">web</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Web">Web</a></code>)</code> </span> </div> <div class="field-description"> <p>Grounding chunk from the web.</p> </div> </div> </section> </div> </section> </div> <div class="second-column"> <section id="GroundingChunk.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="com">// Union field <code translate="no" dir="ltr">chunk_type</code> can be only one of the following:</span> <span class="str">"web"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Web">Web</a></code>)</span> <span class="pun">}</span> <span class="com">// End of list of possible types for union field <code translate="no" dir="ltr">chunk_type</code>.</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="Web" data-text="Web" tabindex="-1">Web</h2> <div class="column-container"> <div class="reference"> <section id="Web.description"> <p>Chunk from the web.</p> </section> <section id="Web.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="Web.FIELDS.uri"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">uri</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>URI reference of the chunk.</p> </div> </div> </section> <section id="Web.FIELDS.title"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">title</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Title of the chunk.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="Web.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"uri"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"title"</span><span class="pun">: </span><span class="kwd">string</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="GroundingSupport" data-text="GroundingSupport" tabindex="-1">GroundingSupport</h2> <div class="column-container"> <div class="reference"> <section id="GroundingSupport.description"> <p>Grounding support.</p> </section> <section id="GroundingSupport.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="GroundingSupport.FIELDS.grounding_chunk_indices"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">groundingChunkIndices[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>A list of indices (into 'grounding_chunk') specifying the citations associated with the claim. For instance [1,3,4] means that grounding_chunk[1], grounding_chunk[3], grounding_chunk[4] are the retrieved content attributed to the claim.</p> </div> </div> </section> <section id="GroundingSupport.FIELDS.confidence_scores"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">confidenceScores[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Confidence score of the support references. Ranges from 0 to 1. 1 is the most confident. This list must have the same size as the groundingChunkIndices.</p> </div> </div> </section> <section id="GroundingSupport.FIELDS.segment"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">segment</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Segment">Segment</a></code>)</code> </span> </div> <div class="field-description"> <p>Segment of the content this support belongs to.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="GroundingSupport.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"groundingChunkIndices"</span><span class="pun">: </span><span class="pun">[</span> <span class="kwd">integer</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"confidenceScores"</span><span class="pun">: </span><span class="pun">[</span> <span class="kwd">number</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"segment"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Segment">Segment</a></code>)</span> <span class="pun">}</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="Segment" data-text="Segment" tabindex="-1">Segment</h2> <div class="column-container"> <div class="reference"> <section id="Segment.description"> <p>Segment of the content.</p> </section> <section id="Segment.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="Segment.FIELDS.part_index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">partIndex</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Output only. The index of a Part object within its parent Content object.</p> </div> </div> </section> <section id="Segment.FIELDS.start_index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">startIndex</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Output only. Start index in the given Part, measured in bytes. Offset from the start of the Part, inclusive, starting at zero.</p> </div> </div> </section> <section id="Segment.FIELDS.end_index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">endIndex</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Output only. End index in the given Part, measured in bytes. Offset from the start of the Part, exclusive, starting at zero.</p> </div> </div> </section> <section id="Segment.FIELDS.text"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">text</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Output only. The text corresponding to the segment from the response.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="Segment.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"partIndex"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"startIndex"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"endIndex"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"text"</span><span class="pun">: </span><span class="kwd">string</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="RetrievalMetadata" data-text="RetrievalMetadata" tabindex="-1">RetrievalMetadata</h2> <div class="column-container"> <div class="reference"> <section id="RetrievalMetadata.description"> <p>Metadata related to retrieval in the grounding flow.</p> </section> <section id="RetrievalMetadata.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="RetrievalMetadata.FIELDS.google_search_dynamic_retrieval_score"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">googleSearchDynamicRetrievalScore</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Optional. Score indicating how likely information from google search could help answer the prompt. The score is in the range [0, 1], where 0 is the least likely and 1 is the most likely. This score is only populated when google search grounding and dynamic retrieval is enabled. It will be compared to the threshold to determine whether to trigger google search.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="RetrievalMetadata.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"googleSearchDynamicRetrievalScore"</span><span class="pun">: </span><span class="kwd">number</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="LogprobsResult" data-text="LogprobsResult" tabindex="-1">LogprobsResult</h2> <div class="column-container"> <div class="reference"> <section id="LogprobsResult.description"> <p>Logprobs Result</p> </section> <section id="LogprobsResult.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="LogprobsResult.FIELDS.top_candidates"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">topCandidates[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#TopCandidates">TopCandidates</a></code>)</code> </span> </div> <div class="field-description"> <p>Length = total number of decoding steps.</p> </div> </div> </section> <section id="LogprobsResult.FIELDS.chosen_candidates"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">chosenCandidates[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Candidate">Candidate</a></code>)</code> </span> </div> <div class="field-description"> <p>Length = total number of decoding steps. The chosen candidates may or may not be in topCandidates.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="LogprobsResult.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"topCandidates"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#TopCandidates">TopCandidates</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"chosenCandidates"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Candidate">Candidate</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="TopCandidates" data-text="TopCandidates" tabindex="-1">TopCandidates</h2> <div class="column-container"> <div class="reference"> <section id="TopCandidates.description"> <p>Candidates with top log probabilities at each decoding step.</p> </section> <section id="TopCandidates.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="TopCandidates.FIELDS.candidates"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">candidates[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Candidate">Candidate</a></code>)</code> </span> </div> <div class="field-description"> <p>Sorted by log probability in descending order.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="TopCandidates.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"candidates"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#Candidate">Candidate</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="Candidate" data-text="Candidate" tabindex="-1">Candidate</h2> <div class="column-container"> <div class="reference"> <section id="Candidate.description"> <p>Candidate for the logprobs token and score.</p> </section> <section id="Candidate.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="Candidate.FIELDS.token"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">token</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>The candidate’s token string value.</p> </div> </div> </section> <section id="Candidate.FIELDS.token_id"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">tokenId</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>The candidate’s token id value.</p> </div> </div> </section> <section id="Candidate.FIELDS.log_probability"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">logProbability</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>The candidate's log probability.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="Candidate.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"token"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"tokenId"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"logProbability"</span><span class="pun">: </span><span class="kwd">number</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="citationmetadata" data-text="CitationMetadata" tabindex="-1">CitationMetadata</h2><a name="v1beta.CitationMetadata"></a> <meta content="CitationMetadata" itemprop="name"/> <meta content="/api/rest/v1beta/CitationMetadata" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/CitationMetadata"> <ul class="toc"> <li><a href="#SCHEMA_REPRESENTATION">JSON representation</a></li><li><a href="#CitationSource">CitationSource</a> <ul> <li><a href="#CitationSource.SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> </li> </ul> <div class="column-container"> <div class="reference"> <section id="description"> <p>A collection of source attributions for a piece of content.</p> </section> <section id="FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="FIELDS.citation_sources"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">citationSources[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/generate-content#CitationSource">CitationSource</a></code>)</code> </span> </div> <div class="field-description"> <p>Citations to sources for a specific response.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"citationSources"</span><span class="pun">: </span><span class="pun">[</span> <span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/generate-content#CitationSource">CitationSource</a></code>)</span> <span class="pun">}</span> <span class="pun">]</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="CitationSource" data-text="CitationSource" tabindex="-1">CitationSource</h2> <div class="column-container"> <div class="reference"> <section id="CitationSource.description"> <p>A citation to a source for a portion of a specific response.</p> </section> <section id="CitationSource.FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="CitationSource.FIELDS.start_index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">startIndex</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Optional. Start of segment of the response that is attributed to this source.</p><p>Index indicates the start of the segment, measured in bytes.</p> </div> </div> </section> <section id="CitationSource.FIELDS.end_index"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">endIndex</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Optional. End of the attributed segment, exclusive.</p> </div> </div> </section> <section id="CitationSource.FIELDS.uri"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">uri</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. URI that is attributed as a source for a portion of the text.</p> </div> </div> </section> <section id="CitationSource.FIELDS.license"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">license</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. License for the GitHub project that is attributed as a source for segment.</p><p>License info is required for code citations.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="CitationSource.SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"startIndex"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"endIndex"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"uri"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"license"</span><span class="pun">: </span><span class="kwd">string</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="generationconfig" data-text="GenerationConfig" tabindex="-1">GenerationConfig</h2><a name="v1beta.GenerationConfig"></a> <meta content="GenerationConfig" itemprop="name"/> <meta content="/api/rest/v1beta/GenerationConfig" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/GenerationConfig"> <ul class="toc"> <li><a href="#SCHEMA_REPRESENTATION">JSON representation</a></li> </ul> <div class="column-container"> <div class="reference"> <section id="description"> <p>Configuration options for model generation and outputs. Not all parameters are configurable for every model.</p> </section> <section id="FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="FIELDS.stop_sequences"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">stopSequences[]</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. The set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a <code translate="no" dir="ltr">stop_sequence</code>. The stop sequence will not be included as part of the response.</p> </div> </div> </section> <section id="FIELDS.response_mime_type"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">responseMimeType</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">string</code> </span> </div> <div class="field-description"> <p>Optional. MIME type of the generated candidate text. Supported MIME types are: <code translate="no" dir="ltr">text/plain</code>: (default) Text output. <code translate="no" dir="ltr">application/json</code>: JSON response in the response candidates. <code translate="no" dir="ltr">text/x.enum</code>: ENUM as a string response in the response candidates. Refer to the <a href="https://ai.google.dev/gemini-api/docs/prompting_with_media#plain_text_formats">docs</a> for a list of all supported text MIME types.</p> </div> </div> </section> <section id="FIELDS.response_schema"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">responseSchema</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">object (<code translate="no" dir="ltr"><a href="/api/caching#Schema">Schema</a></code>)</code> </span> </div> <div class="field-description"> <p>Optional. Output schema of the generated candidate text. Schemas must be a subset of the <a href="https://spec.openapis.org/oas/v3.0.3#schema">OpenAPI schema</a> and can be objects, primitives or arrays.</p><p>If set, a compatible <code translate="no" dir="ltr">responseMimeType</code> must also be set. Compatible MIME types: <code translate="no" dir="ltr">application/json</code>: Schema for JSON response. Refer to the <a href="https://ai.google.dev/gemini-api/docs/json-mode">JSON text generation guide</a> for more details.</p> </div> </div> </section> <section id="FIELDS.candidate_count"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">candidateCount</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Optional. Number of generated responses to return.</p><p>Currently, this value can only be set to 1. If unset, this will default to 1.</p> </div> </div> </section> <section id="FIELDS.max_output_tokens"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">maxOutputTokens</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Optional. The maximum number of tokens to include in a response candidate.</p><p>Note: The default value varies by model, see the <code translate="no" dir="ltr">Model.output_token_limit</code> attribute of the <code translate="no" dir="ltr">Model</code> returned from the <code translate="no" dir="ltr">getModel</code> function.</p> </div> </div> </section> <section id="FIELDS.temperature"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">temperature</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Optional. Controls the randomness of the output.</p><p>Note: The default value varies by model, see the <code translate="no" dir="ltr">Model.temperature</code> attribute of the <code translate="no" dir="ltr">Model</code> returned from the <code translate="no" dir="ltr">getModel</code> function.</p><p>Values can range from [0.0, 2.0].</p> </div> </div> </section> <section id="FIELDS.top_p"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">topP</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Optional. The maximum cumulative probability of tokens to consider when sampling.</p><p>The model uses combined Top-k and Top-p (nucleus) sampling.</p><p>Tokens are sorted based on their assigned probabilities so that only the most likely tokens are considered. Top-k sampling directly limits the maximum number of tokens to consider, while Nucleus sampling limits the number of tokens based on the cumulative probability.</p><p>Note: The default value varies by <code translate="no" dir="ltr">Model</code> and is specified by the<code translate="no" dir="ltr">Model.top_p</code> attribute returned from the <code translate="no" dir="ltr">getModel</code> function. An empty <code translate="no" dir="ltr">topK</code> attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting <code translate="no" dir="ltr">topK</code> on requests.</p> </div> </div> </section> <section id="FIELDS.top_k"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">topK</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Optional. The maximum number of tokens to consider when sampling.</p><p>Gemini models use Top-p (nucleus) sampling or a combination of Top-k and nucleus sampling. Top-k sampling considers the set of <code translate="no" dir="ltr">topK</code> most probable tokens. Models running with nucleus sampling don't allow topK setting.</p><p>Note: The default value varies by <code translate="no" dir="ltr">Model</code> and is specified by the<code translate="no" dir="ltr">Model.top_p</code> attribute returned from the <code translate="no" dir="ltr">getModel</code> function. An empty <code translate="no" dir="ltr">topK</code> attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting <code translate="no" dir="ltr">topK</code> on requests.</p> </div> </div> </section> <section id="FIELDS.presence_penalty"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">presencePenalty</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Optional. Presence penalty applied to the next token's logprobs if the token has already been seen in the response.</p><p>This penalty is binary on/off and not dependant on the number of times the token is used (after the first). Use <code translate="no" dir="ltr"><a href="/api/generate-content#FIELDS.frequency_penalty">frequencyPenalty</a></code> for a penalty that increases with each use.</p><p>A positive penalty will discourage the use of tokens that have already been used in the response, increasing the vocabulary.</p><p>A negative penalty will encourage the use of tokens that have already been used in the response, decreasing the vocabulary.</p> </div> </div> </section> <section id="FIELDS.frequency_penalty"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">frequencyPenalty</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">number</code> </span> </div> <div class="field-description"> <p>Optional. Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far.</p><p>A positive penalty will discourage the use of tokens that have already been used, proportional to the number of times the token has been used: The more a token is used, the more dificult it is for the model to use that token again increasing the vocabulary of responses.</p><p>Caution: A <em>negative</em> penalty will encourage the model to reuse tokens proportional to the number of times the token has been used. Small negative values will reduce the vocabulary of a response. Larger negative values will cause the model to start repeating a common token until it hits the <code translate="no" dir="ltr"><a href="/api/generate-content#FIELDS.max_output_tokens">maxOutputTokens</a></code> limit: "...the the the the the...".</p> </div> </div> </section> <section id="FIELDS.response_logprobs"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">responseLogprobs</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">boolean</code> </span> </div> <div class="field-description"> <p>Optional. If true, export the logprobs results in response.</p> </div> </div> </section> <section id="FIELDS.logprobs"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">logprobs</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">integer</code> </span> </div> <div class="field-description"> <p>Optional. Only valid if <code translate="no" dir="ltr"><a href="/api/generate-content#FIELDS.response_logprobs">responseLogprobs=True</a></code>. This sets the number of top logprobs to return at each decoding step in the <code translate="no" dir="ltr"><a href="/api/generate-content#FIELDS.logprobs_result">Candidate.logprobs_result</a></code>.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"stopSequences"</span><span class="pun">: </span><span class="pun">[</span> <span class="kwd">string</span> <span class="pun">]</span><span class="pun">,</span> <span class="str">"responseMimeType"</span><span class="pun">: </span><span class="kwd">string</span><span class="pun">,</span> <span class="str">"responseSchema"</span><span class="pun">: </span><span class="pun">{</span> <span class="kwd">object (<code translate="no" dir="ltr"><a href="/api/caching#Schema">Schema</a></code>)</span> <span class="pun">}</span><span class="pun">,</span> <span class="str">"candidateCount"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"maxOutputTokens"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"temperature"</span><span class="pun">: </span><span class="kwd">number</span><span class="pun">,</span> <span class="str">"topP"</span><span class="pun">: </span><span class="kwd">number</span><span class="pun">,</span> <span class="str">"topK"</span><span class="pun">: </span><span class="kwd">integer</span><span class="pun">,</span> <span class="str">"presencePenalty"</span><span class="pun">: </span><span class="kwd">number</span><span class="pun">,</span> <span class="str">"frequencyPenalty"</span><span class="pun">: </span><span class="kwd">number</span><span class="pun">,</span> <span class="str">"responseLogprobs"</span><span class="pun">: </span><span class="kwd">boolean</span><span class="pun">,</span> <span class="str">"logprobs"</span><span class="pun">: </span><span class="kwd">integer</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="harmcategory" data-text="HarmCategory" tabindex="-1">HarmCategory</h2><a name="v1beta.HarmCategory"></a> <meta content="HarmCategory" itemprop="name"/> <meta content="/api/rest/v1beta/HarmCategory" itemprop="path"/> <section id="/api/rest/v1beta/HarmCategory"> <section id="description"> <p>The category of a rating.</p><p>These categories cover various kinds of harms that developers may wish to adjust.</p> </section> <section id="ENUM_VALUES"> <table class="constants responsive fixed" id="ENUM_VALUES-table"> <colgroup> <col width="25%"/> <col/> </colgroup> <thead> <tr> <th colspan="2">Enums</th> </tr> </thead> <tbody> <tr id="ENUM_VALUES.HARM_CATEGORY_UNSPECIFIED"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_UNSPECIFIED</code></td> <td>Category is unspecified.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_DEROGATORY"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_DEROGATORY</code></td> <td><strong>PaLM</strong> - Negative or harmful comments targeting identity and/or protected attribute.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_TOXICITY"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_TOXICITY</code></td> <td><strong>PaLM</strong> - Content that is rude, disrespectful, or profane.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_VIOLENCE"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_VIOLENCE</code></td> <td><strong>PaLM</strong> - Describes scenarios depicting violence against an individual or group, or general descriptions of gore.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_SEXUAL"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_SEXUAL</code></td> <td><strong>PaLM</strong> - Contains references to sexual acts or other lewd content.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_MEDICAL"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_MEDICAL</code></td> <td><strong>PaLM</strong> - Promotes unchecked medical advice.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_DANGEROUS"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_DANGEROUS</code></td> <td><strong>PaLM</strong> - Dangerous content that promotes, facilitates, or encourages harmful acts.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_HARASSMENT"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_HARASSMENT</code></td> <td><strong>Gemini</strong> - Harassment content.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_HATE_SPEECH"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_HATE_SPEECH</code></td> <td><strong>Gemini</strong> - Hate speech and content.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_SEXUALLY_EXPLICIT"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_SEXUALLY_EXPLICIT</code></td> <td><strong>Gemini</strong> - Sexually explicit content.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_DANGEROUS_CONTENT"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_DANGEROUS_CONTENT</code></td> <td><strong>Gemini</strong> - Dangerous content.</td> </tr> <tr id="ENUM_VALUES.HARM_CATEGORY_CIVIC_INTEGRITY"> <td><code class="apitype" translate="no" dir="ltr">HARM_CATEGORY_CIVIC_INTEGRITY</code></td> <td><strong>Gemini</strong> - Content that may be used to harm civic integrity.</td> </tr> </tbody> </table> </section> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="safetyrating" data-text="SafetyRating" tabindex="-1">SafetyRating</h2><a name="v1beta.SafetyRating"></a> <meta content="SafetyRating" itemprop="name"/> <meta content="/api/rest/v1beta/SafetyRating" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/SafetyRating"> <ul class="toc"> <li><a href="#SCHEMA_REPRESENTATION">JSON representation</a></li><li><a href="#HarmProbability">HarmProbability</a></li> </ul> <div class="column-container"> <div class="reference"> <section id="description"> <p>Safety rating for a piece of content.</p><p>The safety rating contains the category of harm and the harm probability level in that category for a piece of content. Content is classified for safety across a number of harm categories and the probability of the harm classification is included here.</p> </section> <section id="FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="FIELDS.category"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">category</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.HarmCategory">HarmCategory</a></code>)</code> </span> </div> <div class="field-description"> <p>Required. The category for this rating.</p> </div> </div> </section> <section id="FIELDS.probability"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">probability</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#HarmProbability">HarmProbability</a></code>)</code> </span> </div> <div class="field-description"> <p>Required. The probability of harm for this content.</p> </div> </div> </section> <section id="FIELDS.blocked"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">blocked</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">boolean</code> </span> </div> <div class="field-description"> <p>Was this content blocked because of this rating?</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"category"</span><span class="pun">: </span><span class="kwd">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.HarmCategory">HarmCategory</a></code>)</span><span class="pun">,</span> <span class="str">"probability"</span><span class="pun">: </span><span class="kwd">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#HarmProbability">HarmProbability</a></code>)</span><span class="pun">,</span> <span class="str">"blocked"</span><span class="pun">: </span><span class="kwd">boolean</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="HarmProbability" data-text="HarmProbability" tabindex="-1">HarmProbability</h2> <section id="HarmProbability.description"> <p>The probability that a piece of content is harmful.</p><p>The classification system gives the probability of the content being unsafe. This does not indicate the severity of harm for a piece of content.</p> </section> <section id="HarmProbability.ENUM_VALUES"> <table class="constants responsive fixed" id="HarmProbability.ENUM_VALUES-table"> <colgroup> <col width="25%"/> <col/> </colgroup> <thead> <tr> <th colspan="2">Enums</th> </tr> </thead> <tbody> <tr id="HarmProbability.ENUM_VALUES.HARM_PROBABILITY_UNSPECIFIED"> <td><code class="apitype" translate="no" dir="ltr">HARM_PROBABILITY_UNSPECIFIED</code></td> <td>Probability is unspecified.</td> </tr> <tr id="HarmProbability.ENUM_VALUES.NEGLIGIBLE"> <td><code class="apitype" translate="no" dir="ltr">NEGLIGIBLE</code></td> <td>Content has a negligible chance of being unsafe.</td> </tr> <tr id="HarmProbability.ENUM_VALUES.LOW"> <td><code class="apitype" translate="no" dir="ltr">LOW</code></td> <td>Content has a low chance of being unsafe.</td> </tr> <tr id="HarmProbability.ENUM_VALUES.MEDIUM"> <td><code class="apitype" translate="no" dir="ltr">MEDIUM</code></td> <td>Content has a medium chance of being unsafe.</td> </tr> <tr id="HarmProbability.ENUM_VALUES.HIGH"> <td><code class="apitype" translate="no" dir="ltr">HIGH</code></td> <td>Content has a high chance of being unsafe.</td> </tr> </tbody> </table> </section> </section> </div><div itemscope="" itemtype="http://developers.google.com/ReferenceObject"><h2 id="safetysetting" data-text="SafetySetting" tabindex="-1">SafetySetting</h2><a name="v1beta.SafetySetting"></a> <meta content="SafetySetting" itemprop="name"/> <meta content="/api/rest/v1beta/SafetySetting" itemprop="path"/> <section class="prototype" id="/api/rest/v1beta/SafetySetting"> <ul class="toc"> <li><a href="#SCHEMA_REPRESENTATION">JSON representation</a></li><li><a href="#HarmBlockThreshold">HarmBlockThreshold</a></li> </ul> <div class="column-container"> <div class="reference"> <section id="description"> <p>Safety setting, affecting the safety-blocking behavior.</p><p>Passing a safety setting for a category changes the allowed probability that content is blocked.</p> </section> <section id="FIELDS"> <thead> <tr> <th colspan="2">Fields</th> </tr> </thead> <section id="FIELDS.category"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">category</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.HarmCategory">HarmCategory</a></code>)</code> </span> </div> <div class="field-description"> <p>Required. The category for this setting.</p> </div> </div> </section> <section id="FIELDS.threshold"> <div class="field-entry"> <div class="signature"> <span class="field-name"> <td><code translate="no" dir="ltr">threshold</code></td> </span> <span class="field-type"> <code translate="no" dir="ltr">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#HarmBlockThreshold">HarmBlockThreshold</a></code>)</code> </span> </div> <div class="field-description"> <p>Required. Controls the probability threshold at which harm is blocked.</p> </div> </div> </section> </section> </div> <div class="second-column"> <section id="SCHEMA_REPRESENTATION"> <table class="properties responsive fixed"> <thead> <tr> <th>JSON representation</th> </tr> </thead> <tbody> <tr> <td style="margin:0;padding:0;"><div></div><devsite-code><pre style="border: 0;margin: 0;" translate="no" dir="ltr" is-upgraded><span class="pun">{</span> <span class="str">"category"</span><span class="pun">: </span><span class="kwd">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#v1beta.HarmCategory">HarmCategory</a></code>)</span><span class="pun">,</span> <span class="str">"threshold"</span><span class="pun">: </span><span class="kwd">enum (<code translate="no" dir="ltr"><a href="/api/generate-content#HarmBlockThreshold">HarmBlockThreshold</a></code>)</span> <span class="pun">}</span></pre></devsite-code></td> </tr> </tbody> </table> </section> </div> </div> <h2 id="HarmBlockThreshold" data-text="HarmBlockThreshold" tabindex="-1">HarmBlockThreshold</h2> <section id="HarmBlockThreshold.description"> <p>Block at and beyond a specified harm probability.</p> </section> <section id="HarmBlockThreshold.ENUM_VALUES"> <table class="constants responsive fixed" id="HarmBlockThreshold.ENUM_VALUES-table"> <colgroup> <col width="25%"/> <col/> </colgroup> <thead> <tr> <th colspan="2">Enums</th> </tr> </thead> <tbody> <tr id="HarmBlockThreshold.ENUM_VALUES.HARM_BLOCK_THRESHOLD_UNSPECIFIED"> <td><code class="apitype" translate="no" dir="ltr">HARM_BLOCK_THRESHOLD_UNSPECIFIED</code></td> <td>Threshold is unspecified.</td> </tr> <tr id="HarmBlockThreshold.ENUM_VALUES.BLOCK_LOW_AND_ABOVE"> <td><code class="apitype" translate="no" dir="ltr">BLOCK_LOW_AND_ABOVE</code></td> <td>Content with NEGLIGIBLE will be allowed.</td> </tr> <tr id="HarmBlockThreshold.ENUM_VALUES.BLOCK_MEDIUM_AND_ABOVE"> <td><code class="apitype" translate="no" dir="ltr">BLOCK_MEDIUM_AND_ABOVE</code></td> <td>Content with NEGLIGIBLE and LOW will be allowed.</td> </tr> <tr id="HarmBlockThreshold.ENUM_VALUES.BLOCK_ONLY_HIGH"> <td><code class="apitype" translate="no" dir="ltr">BLOCK_ONLY_HIGH</code></td> <td>Content with NEGLIGIBLE, LOW, and MEDIUM will be allowed.</td> </tr> <tr id="HarmBlockThreshold.ENUM_VALUES.BLOCK_NONE"> <td><code class="apitype" translate="no" dir="ltr">BLOCK_NONE</code></td> <td>All content will be allowed.</td> </tr> <tr id="HarmBlockThreshold.ENUM_VALUES.OFF"> <td><code class="apitype" translate="no" dir="ltr">OFF</code></td> <td>Turn off the safety filter.</td> </tr> </tbody> </table> </section> </section> </div> </div> <devsite-thumb-rating position="footer"> </devsite-thumb-rating> <devsite-feedback position="footer" project-name="Gemini API" product-id="5292923" bucket="documentation" context="" version="t-devsite-webserver-20241114-r00-rc02.464921008191574316" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="footer" class="nocontent" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/images/touchicon-180-new.png" > <button> Send feedback </button> </devsite-feedback> <div class="devsite-floating-action-buttons"> </div> </article> <devsite-content-footer class="nocontent"> <p>Except as otherwise noted, the content of this page is licensed under the <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 License</a>. For details, see the <a href="https://developers.google.com/site-policies">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p> <p>Last updated 2024-10-31 UTC.</p> </devsite-content-footer> <devsite-notification > </devsite-notification> <div class="devsite-content-data"> <template class="devsite-thumb-rating-feedback"> <devsite-feedback position="thumb-rating" project-name="Gemini API" product-id="5292923" bucket="documentation" context="" version="t-devsite-webserver-20241114-r00-rc02.464921008191574316" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="thumb-rating" class="nocontent" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/images/touchicon-180-new.png" > <button> Need to tell us more? </button> </devsite-feedback> </template> <template class="devsite-content-data-template"> [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[],[]] </template> </div> </devsite-content> </main> <devsite-footer-promos class="devsite-footer"> </devsite-footer-promos> <devsite-footer-linkboxes class="devsite-footer"> <nav class="devsite-footer-linkboxes nocontent" aria-label="Footer links"> </nav> </devsite-footer-linkboxes> <devsite-footer-utility class="devsite-footer"> <div class="devsite-footer-utility nocontent"> <nav class="devsite-footer-utility-links" aria-label="Utility links"> <ul class="devsite-footer-utility-list"> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/terms" data-category="Site-Wide Custom Events" data-label="Footer Terms link" > Terms </a> </li> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/privacy" data-category="Site-Wide Custom Events" data-label="Footer Privacy link" > Privacy </a> </li> <li class="devsite-footer-utility-item glue-cookie-notification-bar-control"> <a class="devsite-footer-utility-link gc-analytics-event" href="#" data-category="Site-Wide Custom Events" data-label="Footer Manage cookies link" aria-hidden="true" > Manage cookies </a> </li> </ul> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pl" >Polski</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="ru" >Русский</a> </li> <li role="presentation"> <a role="menuitem" lang="he" >עברית</a> </li> <li role="presentation"> <a role="menuitem" lang="ar" >العربيّة</a> </li> <li role="presentation"> <a role="menuitem" lang="fa" >فارسی</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="bn" >বাংলা</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> </nav> </div> </devsite-footer-utility> <devsite-panel></devsite-panel> </section></section> <devsite-sitemask></devsite-sitemask> <devsite-snackbar></devsite-snackbar> <devsite-tooltip ></devsite-tooltip> <devsite-heading-link></devsite-heading-link> <devsite-analytics> <script type="application/json" analytics>[]</script> <script type="application/json" tag-management>{"at": "True", "ga4": [], "ga4p": [], "gtm": [{"id": "GTM-TC2MQKS8", "purpose": 0}], "parameters": {"internalUser": "False", "language": {"machineTranslated": "False", "requested": "en", "served": "en"}, "pageType": "reference", "projectName": "Gemini API", "signedIn": "False", "tenant": "googledevai", "recommendations": {"sourcePage": "", "sourceType": 0, "sourceRank": 0, "sourceIdenticalDescriptions": 0, "sourceTitleWords": 0, "sourceDescriptionWords": 0, "experiment": ""}, "experiment": {"ids": ""}}}</script> </devsite-analytics> <devsite-badger></devsite-badger> <script nonce="qOk7PqmnqBaRQbpxjdIJa3gTRufB/c"> (function(d,e,v,s,i,t,E){d['GoogleDevelopersObject']=i; t=e.createElement(v);t.async=1;t.src=s;E=e.getElementsByTagName(v)[0]; E.parentNode.insertBefore(t,E);})(window, document, 'script', 'https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/js/app_loader.js', '[59,"en",null,"/js/devsite_app_module.js","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai","https://googledevai-dot-devsite-v2-prod-3p.appspot.com",null,null,["/_pwa/googledevai/manifest.json","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/images/video-placeholder.svg","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/googledevai/images/favicon-new.png","/_static/googledevai/images/lockup-new.svg","https://fonts.googleapis.com/css?family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"],1,null,[1,6,8,12,14,17,21,25,50,52,63,70,75,76,80,87,91,92,93,97,98,100,101,102,103,104,105,107,108,109,110,112,113,117,118,120,122,124,125,126,127,129,130,131,132,133,134,135,136,138,140,141,147,148,149,151,152,156,157,158,159,161,163,164,168,169,170,179,180,182,183,186,191,193,196],"AIzaSyCNm9YxQumEXwGJgTDjxoxXK6m1F-9720Q","AIzaSyCc76DZePGtoyUjqKrLdsMGk_ry7sljLbY","ai.google.dev","AIzaSyB9bqgQ2t11WJsOX8qNsCQ6U-w91mmqF-I","AIzaSyAdYnStPdzjcJJtQ0mvIaeaMKj7_t6J_Fg",null,null,null,["Cloud__enable_cloudx_experiment_ids","MiscFeatureFlags__enable_firebase_utm","Search__enable_ai_eligibility_checks","Profiles__enable_developer_profiles_callout","Profiles__enable_release_notes_notifications","EngEduTelemetry__enable_engedu_telemetry","Cloud__enable_llm_concierge_chat","Profiles__enable_recognition_badges","Search__enable_page_map","CloudShell__cloud_shell_button","Profiles__enable_public_developer_profiles","Profiles__enable_complete_playlist_endpoint","Cloud__enable_legacy_calculator_redirect","Profiles__enable_profile_collections","Cloud__enable_cloud_facet_chat","DevPro__enable_developer_subscriptions","Profiles__enable_page_saving","OnSwitch__enable","CloudShell__cloud_code_overflow_menu","MiscFeatureFlags__developers_footer_image","MiscFeatureFlags__enable_variable_operator","MiscFeatureFlags__developers_footer_dark_image","Experiments__reqs_query_experiments","Profiles__enable_completecodelab_endpoint","MiscFeatureFlags__enable_explain_this_code","Analytics__enable_clearcut_logging","Search__enable_suggestions_from_borg","Profiles__enable_dashboard_curated_recommendations","Profiles__require_profile_eligibility_for_signin","Cloud__enable_cloud_shell_fte_user_flow","Cloud__enable_cloud_dlp_service","TpcFeatures__enable_required_headers","MiscFeatureFlags__enable_view_transitions","MiscFeatureFlags__emergency_css","Cloud__enable_cloud_shell","DevPro__enable_cloud_innovators_plus","Cloud__enable_free_trial_server_call","MiscFeatureFlags__enable_project_variables","Cloud__enable_cloudx_ping","Profiles__enable_awarding_url","BookNav__enable_tenant_cache_key","Concierge__enable_pushui","TpcFeatures__enable_mirror_tenant_redirects","Search__enable_dynamic_content_confidential_banner"],null,null,"AIzaSyA58TaKli1DculwmAmbpzLVGuWc8eCQgQc","https://developerscontentserving-pa.googleapis.com","AIzaSyDWBU60w0P9hEkr29kkksYs8Z7gvZ8u_wc","https://developerscontentsearch-pa.googleapis.com",2,4,null,"https://developerprofiles-pa.googleapis.com",[59,"googledevai","Google AI for Developers","ai.google.dev",null,"googledevai-dot-devsite-v2-prod-3p.appspot.com",null,null,[null,1,null,null,null,null,null,null,null,null,null,[1],null,null,null,null,null,null,[1],null,null,null,null,[1],[1,1,null,1,1]],null,[73,null,null,null,null,null,"/images/lockup-new.svg","/images/touchicon-180-new.png",null,null,null,null,1,1,null,null,null,null,null,null,null,1,null,null,null,"/images/lockup-dark-theme-new.svg",[]],[],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[[],[1,1]],[[null,null,null,null,null,["GTM-TC2MQKS8"],null,null,null,null,null,[["GTM-TC2MQKS8",1]],1]],null,4]]') </script> <devsite-a11y-announce></devsite-a11y-announce> </body> </html>