File size: 6,979 Bytes
5853bf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# Text generation
Source: <https://ai.google.dev/gemini-api/docs/text-generation>
---
The Gemini API can generate text output from various inputs, including text, images, video, and audio, leveraging Gemini models.
Here's a basic example that takes a single text input:
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="How does AI work?"
)
print(response.text)
## Thinking with Gemini 2.5
2.5 Flash and Pro models have ["thinking"](/gemini-api/docs/thinking) enabled by default to enhance quality, which may take longer to run and increase token usage.
When using 2.5 Flash, you can disable thinking by setting the thinking budget to zero.
For more details, see the [thinking guide](/gemini-api/docs/thinking#set-budget).
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="How does AI work?",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
),
)
print(response.text)
## System instructions and other configurations
You can guide the behavior of Gemini models with system instructions. To do so, pass a [`GenerateContentConfig`](/api/generate-content#v1beta.GenerationConfig) object.
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
config=types.GenerateContentConfig(
system_instruction="You are a cat. Your name is Neko."),
contents="Hello there"
)
print(response.text)
The [`GenerateContentConfig`](/api/generate-content#v1beta.GenerationConfig) object also lets you override default generation parameters, such as [temperature](/api/generate-content#v1beta.GenerationConfig).
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=["Explain how AI works"],
config=types.GenerateContentConfig(
temperature=0.1
)
)
print(response.text)
Refer to the [`GenerateContentConfig`](/api/generate-content#v1beta.GenerationConfig) in our API reference for a complete list of configurable parameters and their descriptions.
## Multimodal inputs
The Gemini API supports multimodal inputs, allowing you to combine text with media files. The following example demonstrates providing an image:
from PIL import Image
from google import genai
client = genai.Client()
image = Image.open("/path/to/organ.png")
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[image, "Tell me about this instrument"]
)
print(response.text)
For alternative methods of providing images and more advanced image processing, see our [image understanding guide](/gemini-api/docs/image-understanding). The API also supports [document](/gemini-api/docs/document-processing), [video](/gemini-api/docs/video-understanding), and [audio](/gemini-api/docs/audio) inputs and understanding.
## Streaming responses
By default, the model returns a response only after the entire generation process is complete.
For more fluid interactions, use streaming to receive [`GenerateContentResponse`](/api/generate-content#v1beta.GenerateContentResponse) instances incrementally as they're generated.
from google import genai
client = genai.Client()
response = client.models.generate_content_stream(
model="gemini-2.5-flash",
contents=["Explain how AI works"]
)
for chunk in response:
print(chunk.text, end="")
## Multi-turn conversations (Chat)
Our SDKs provide functionality to collect multiple rounds of prompts and responses into a chat, giving you an easy way to keep track of the conversation history.
**Note:** Chat functionality is only implemented as part of the SDKs. Behind the scenes, it still uses the [`generateContent`](/api/generate-content#method:-models.generatecontent) API. For multi-turn conversations, the full conversation history is sent to the model with each follow-up turn.
from google import genai
client = genai.Client()
chat = client.chats.create(model="gemini-2.5-flash")
response = chat.send_message("I have 2 dogs in my house.")
print(response.text)
response = chat.send_message("How many paws are in my house?")
print(response.text)
for message in chat.get_history():
print(f'role - {message.role}',end=": ")
print(message.parts[0].text)
Streaming can also be used for multi-turn conversations.
from google import genai
client = genai.Client()
chat = client.chats.create(model="gemini-2.5-flash")
response = chat.send_message_stream("I have 2 dogs in my house.")
for chunk in response:
print(chunk.text, end="")
response = chat.send_message_stream("How many paws are in my house?")
for chunk in response:
print(chunk.text, end="")
for message in chat.get_history():
print(f'role - {message.role}', end=": ")
print(message.parts[0].text)
## Supported models
All models in the Gemini family support text generation. To learn more about the models and their capabilities, visit the [Models](/gemini-api/docs/models) page.
## Best practices
### Prompting tips
For basic text generation, a [zero-shot](/gemini-api/docs/prompting-strategies#few-shot) prompt often suffices without needing examples, system instructions or specific formatting.
For more tailored outputs:
* Use System instructions to guide the model.
* Provide few example inputs and outputs to guide the model. This is often referred to as [few-shot](/gemini-api/docs/prompting-strategies#few-shot) prompting.
Consult our [prompt engineering guide](/gemini/docs/prompting-strategies) for more tips.
### Structured output
In some cases, you may need structured output, such as JSON. Refer to our [structured output](/gemini-api/docs/structured-output) guide to learn how.
## What's next
* Try the [Gemini API getting started Colab](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Get_started.ipynb).
* Explore Gemini's [image](/gemini-api/docs/image-understanding), [video](/gemini-api/docs/video-understanding), [audio](/gemini-api/docs/audio) and [document](/gemini-api/docs/document-processing) understanding capabilities.
* Learn about multimodal [file prompting strategies](/gemini-api/docs/files#prompt-guide).
|