llama-index llms google genai integration
Project description
LlamaIndex Llms Integration: Google GenAI
Installation
-
Install the required Python packages:
%pip install llama-index-llms-google-genai
-
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llama_index_llms_google_genai-0.1.14.tar.gz.
File metadata
- Download URL: llama_index_llms_google_genai-0.1.14.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
792a9a4006de36c9274a6f6b7d484c8a49908af323a4c55192c5d9fac8f9f0ff
|
|
| MD5 |
c6642f83358bc95ec1449a5c85529bac
|
|
| BLAKE2b-256 |
be246ec245f9d135abcaa41eaad318ceed98393983afa98de76abb78cc96ea05
|
File details
Details for the file llama_index_llms_google_genai-0.1.14-py3-none-any.whl.
File metadata
- Download URL: llama_index_llms_google_genai-0.1.14-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5734fd32d92076ecfb42abfc850c969deebe13b136302b75e3b626f52802b2c
|
|
| MD5 |
cd3bc0b9fd62bbde640339f200ca7b30
|
|
| BLAKE2b-256 |
636d6bf3dd7db17b0d0e0fa1375e07f132eb25e3f8492a0b58a20469e5bfc89f
|