Skip to main content

llama-index llms google genai integration

Project description

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-2.0-flash")
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-2.0-flash")
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-2.0-flash")
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-2.0-flash")
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="models/gemini-pro")
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="models/gemini-pro")
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="")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_llms_google_genai-0.2.3.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_index_llms_google_genai-0.2.3-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_google_genai-0.2.3.tar.gz.

File metadata

File hashes

Hashes for llama_index_llms_google_genai-0.2.3.tar.gz
Algorithm Hash digest
SHA256 ff999b8ab649a30d955421cd0ec7bfdd1dc68392514515dc1a88a20bb02945f1
MD5 2df43a30d6d5bed968a2ca8028541c4a
BLAKE2b-256 f1e7a4d171dd7158a4a947657024436a3c73adfd53539b0d05d112ba2a304986

See more details on using hashes here.

File details

Details for the file llama_index_llms_google_genai-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_google_genai-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 260bac0c53d14a795a4672b88a0aeb630ca6fb84953bcc1362465dc9dc1a48a8
MD5 7add2bce0ea96d554f7943005d9d1964
BLAKE2b-256 6f331f0d2f175d944649442cbcd1c276e1303edecadbf214e4674ac305c9d8cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page