llama-index-llms-google-genai

llama-index llms google genai integration

50 个版本 Python <4.0,>=3.10
安装
pip install llama-index-llms-google-genai
poetry add llama-index-llms-google-genai
pipenv install llama-index-llms-google-genai
conda install llama-index-llms-google-genai
描述

LlamaIndex Llms Integration: Google GenAI

Installation

  1. Install the required Python packages:

    %pip install llama-index-llms-google-genai
    
  2. Set the Google API key as an environment variable:

    %env GOOGLE_API_KEY=your_api_key_here
    

Usage

Basic Content Generation

To generate a poem using the Gemini model, use the following code:

from llama_index.llms.google_genai import GoogleGenAI

llm = GoogleGenAI(model="gemini-3-flash-preview")
resp = llm.complete("Write a poem about a magic backpack")
print(resp)

Chat with Messages

To simulate a conversation, send a list of messages:

from llama_index.core.llms import ChatMessage
from llama_index.llms.google_genai import GoogleGenAI

messages = [
    ChatMessage(role="user", content="Hello friend!"),
    ChatMessage(role="assistant", content="Yarr what is shakin' matey?"),
    ChatMessage(
        role="user", content="Help me decide what to have for dinner."
    ),
]

llm = GoogleGenAI(model="gemini-3-flash-preview")
resp = llm.chat(messages)
print(resp)

Streaming Responses

To stream content responses in real-time:

from llama_index.llms.google_genai import GoogleGenAI

llm = GoogleGenAI(model="gemini-3-flash-preview")
resp = llm.stream_complete(
    "The story of Sourcrust, the bread creature, is really interesting. It all started when..."
)
for r in resp:
    print(r.text, end="")

To stream chat responses:

from llama_index.core.llms import ChatMessage
from llama_index.llms.google_genai import GoogleGenAI

llm = GoogleGenAI(model="gemini-3-flash-preview")
messages = [
    ChatMessage(role="user", content="Hello friend!"),
    ChatMessage(role="assistant", content="Yarr what is shakin' matey?"),
    ChatMessage(
        role="user", content="Help me decide what to have for dinner."
    ),
]
resp = llm.stream_chat(messages)

Specific Model Usage

To use a specific model, you can configure it like this:

from llama_index.llms.google_genai import GoogleGenAI

llm = GoogleGenAI(model="gemini-3-flash-preview")
resp = llm.complete("Write a short, but joyous, ode to LlamaIndex")
print(resp)

Asynchronous API

To use the asynchronous completion API:

from llama_index.llms.google_genai import GoogleGenAI

llm = GoogleGenAI(model="gemini-3-flash-preview")
resp = await llm.acomplete("Llamas are famous for ")
print(resp)

For asynchronous streaming of responses:

resp = await llm.astream_complete("Llamas are famous for ")
async for chunk in resp:
    print(chunk.text, end="")
版本列表
0.9.5 2026-05-19
0.9.4 2026-05-14
0.9.3 2026-05-13
0.9.2 2026-04-28
0.9.1 2026-03-25
0.9.0 2026-03-18
0.8.7 2026-02-06
0.8.6 2026-02-03
0.8.5 2026-01-30
0.8.4 2026-01-22
0.8.3 2025-12-26
0.8.2 2025-12-16
0.8.1 2025-12-16
0.8.0 2025-12-08
0.7.4 2025-11-27
0.7.3 2025-11-09
0.7.2 2025-11-06
0.7.1 2025-11-03
0.7.0 2025-11-03
0.6.2 2025-09-26
0.6.1 2025-09-25
0.6.0 2025-09-24
0.5.1 2025-09-22
0.5.0 2025-09-15
0.4.0 2025-09-15
0.3.1 2025-09-08
0.3.0 2025-07-30
0.2.6 2025-07-28
0.2.5 2025-07-16
0.2.4 2025-07-09
0.2.3 2025-07-02
0.2.2 2025-06-24
0.2.1 2025-06-02
0.2.0 2025-05-30
0.1.14 2025-05-28
0.1.13 2025-05-15
0.1.12 2025-05-05
0.1.11 2025-05-03
0.1.10 2025-04-30
0.1.9 2025-04-28
0.1.8 2025-04-24
0.1.7 2025-03-27
0.1.6 2025-03-24
0.1.5 2025-03-24
0.1.4 2025-03-14
0.1.3 2025-03-12
0.1.2 2025-03-12
0.1.1 2025-03-09
0.1.0 2025-03-08
0.1.0.post1 2025-03-08