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.1.8.tar.gz (9.3 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.1.8-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llama_index_llms_google_genai-0.1.8.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.11.0-1012-azure

File hashes

Hashes for llama_index_llms_google_genai-0.1.8.tar.gz
Algorithm Hash digest
SHA256 60e4667e9c24e9be5c6305d82db7cc38330a8fde844ac6cfb89e0bfdb557fc8d
MD5 2672ccee8b86506dd61cc0500e595c01
BLAKE2b-256 f7310f3bbd35c9a4ffa340c445ed68d801869c05181483e0c4cf5a9860a84751

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for llama_index_llms_google_genai-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f6e2d96a2bf00d5878d6c95b0202100be7c563d691f9d5b94bd96934f761ca77
MD5 892fb1b6eaa4e3b3e088baf909b5ee53
BLAKE2b-256 71e108277a1155a25d985b9c0c9c379e0a7235f6dd5a66312cf9510535fad491

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