Skip to main content

llama-index llms gemini integration

Project description

LlamaIndex Llms Integration: Gemini

NOTE: Gemini has largely been replaced by Google GenAI. Visit the Google GenAI page for the latest examples and documentation.

Installation

  1. Install the required Python packages:

    %pip install llama-index-llms-gemini
    !pip install -q llama-index google-generativeai
    
  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.gemini import Gemini

resp = Gemini().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.gemini import Gemini

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 = Gemini().chat(messages)
print(resp)

Streaming Responses

To stream content responses in real-time:

from llama_index.llms.gemini import Gemini

llm = Gemini()
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.llms.gemini import Gemini
from llama_index.core.llms import ChatMessage

llm = Gemini()
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)

Using Other Models

To find suitable models available in the Gemini model site:

import google.generativeai as genai

for m in genai.list_models():
    if "generateContent" in m.supported_generation_methods:
        print(m.name)

Specific Model Usage

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

from llama_index.llms.gemini import Gemini

llm = Gemini(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.gemini import Gemini

llm = Gemini()
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="")

LLM Implementation example

https://docs.llamaindex.ai/en/stable/examples/llm/gemini/

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_gemini-0.6.1.tar.gz (9.1 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_gemini-0.6.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_gemini-0.6.1.tar.gz.

File metadata

File hashes

Hashes for llama_index_llms_gemini-0.6.1.tar.gz
Algorithm Hash digest
SHA256 aa8a73aefcc02d91e68c606056bcb9b86cd03e8135405d26750933dbb484af46
MD5 a2629ce2d2446d7da59937e98d5ab457
BLAKE2b-256 416708f64c34fffb5f2fd31f48a0872a123a81ccac480318a40a20a4e703ba23

See more details on using hashes here.

File details

Details for the file llama_index_llms_gemini-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_gemini-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56a60680ea0ccb51f8950106e6325b8f585b81d8bd1481fe4123f7b37db868fc
MD5 6b269b3afc95340553ffb569f3eaad6d
BLAKE2b-256 dc8b749cd1d51ad3497e12f389caf27ee3ed135bff3d8052c00bf2c7c6dcb18d

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