llama-index llms gemini integration
Project description
LlamaIndex Llms Integration: Gemini
Installation
-
Install the required Python packages:
%pip install llama-index-llms-gemini !pip install -q llama-index google-generativeai
-
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
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
File details
Details for the file llama_index_llms_gemini-0.4.0.tar.gz
.
File metadata
- Download URL: llama_index_llms_gemini-0.4.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1fa5a38eddbbe5313dfa9528a8fb38698d05b2f5ab33ad09710ea61719d491e |
|
MD5 | 931130b810f07aeb7f47d313cabf0c0e |
|
BLAKE2b-256 | 7d3b90a43f479a50cfb772945518c28766727be945f265be8bb5d4969757c9d3 |
File details
Details for the file llama_index_llms_gemini-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: llama_index_llms_gemini-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7369dfb979c136482c06b8b24c3cedaa74aa92210407491ae1fff761e1f85c0 |
|
MD5 | 6a3b41818e4e58bf35942927f39e1ed3 |
|
BLAKE2b-256 | 6b39ad724a5f4c59cac9634068736520864714fb3a00f49650fa4841df705bdf |