llama-index-llms-gemini


Namellama-index-llms-gemini JSON
Version 0.4.2 PyPI version JSON
download
home_pageNone
Summaryllama-index llms gemini integration
upload_time2024-12-18 22:12:30
maintainerNone
docs_urlNone
authorYour Name
requires_python<4.0,>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # LlamaIndex Llms Integration: Gemini

## Installation

1. Install the required Python packages:

   ```bash
   %pip install llama-index-llms-gemini
   !pip install -q llama-index google-generativeai
   ```

2. Set the Google API key as an environment variable:

   ```bash
   %env GOOGLE_API_KEY=your_api_key_here
   ```

## Usage

### Basic Content Generation

To generate a poem using the Gemini model, use the following code:

```python
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:

```python
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:

```python
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:

```python
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:

```python
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:

```python
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:

```python
from llama_index.llms.gemini import Gemini

llm = Gemini()
resp = await llm.acomplete("Llamas are famous for ")
print(resp)
```

For asynchronous streaming of responses:

```python
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/

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "llama-index-llms-gemini",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Your Name",
    "author_email": "you@example.com",
    "download_url": "https://files.pythonhosted.org/packages/07/59/7c31ebfa2e297eb7eeb5e42faf686788db778ffd0b44532d5b1c21e8b835/llama_index_llms_gemini-0.4.2.tar.gz",
    "platform": null,
    "description": "# LlamaIndex Llms Integration: Gemini\n\n## Installation\n\n1. Install the required Python packages:\n\n   ```bash\n   %pip install llama-index-llms-gemini\n   !pip install -q llama-index google-generativeai\n   ```\n\n2. Set the Google API key as an environment variable:\n\n   ```bash\n   %env GOOGLE_API_KEY=your_api_key_here\n   ```\n\n## Usage\n\n### Basic Content Generation\n\nTo generate a poem using the Gemini model, use the following code:\n\n```python\nfrom llama_index.llms.gemini import Gemini\n\nresp = Gemini().complete(\"Write a poem about a magic backpack\")\nprint(resp)\n```\n\n### Chat with Messages\n\nTo simulate a conversation, send a list of messages:\n\n```python\nfrom llama_index.core.llms import ChatMessage\nfrom llama_index.llms.gemini import Gemini\n\nmessages = [\n    ChatMessage(role=\"user\", content=\"Hello friend!\"),\n    ChatMessage(role=\"assistant\", content=\"Yarr what is shakin' matey?\"),\n    ChatMessage(\n        role=\"user\", content=\"Help me decide what to have for dinner.\"\n    ),\n]\nresp = Gemini().chat(messages)\nprint(resp)\n```\n\n### Streaming Responses\n\nTo stream content responses in real-time:\n\n```python\nfrom llama_index.llms.gemini import Gemini\n\nllm = Gemini()\nresp = llm.stream_complete(\n    \"The story of Sourcrust, the bread creature, is really interesting. It all started when...\"\n)\nfor r in resp:\n    print(r.text, end=\"\")\n```\n\nTo stream chat responses:\n\n```python\nfrom llama_index.llms.gemini import Gemini\nfrom llama_index.core.llms import ChatMessage\n\nllm = Gemini()\nmessages = [\n    ChatMessage(role=\"user\", content=\"Hello friend!\"),\n    ChatMessage(role=\"assistant\", content=\"Yarr what is shakin' matey?\"),\n    ChatMessage(\n        role=\"user\", content=\"Help me decide what to have for dinner.\"\n    ),\n]\nresp = llm.stream_chat(messages)\n```\n\n### Using Other Models\n\nTo find suitable models available in the Gemini model site:\n\n```python\nimport google.generativeai as genai\n\nfor m in genai.list_models():\n    if \"generateContent\" in m.supported_generation_methods:\n        print(m.name)\n```\n\n### Specific Model Usage\n\nTo use a specific model, you can configure it like this:\n\n```python\nfrom llama_index.llms.gemini import Gemini\n\nllm = Gemini(model=\"models/gemini-pro\")\nresp = llm.complete(\"Write a short, but joyous, ode to LlamaIndex\")\nprint(resp)\n```\n\n### Asynchronous API\n\nTo use the asynchronous completion API:\n\n```python\nfrom llama_index.llms.gemini import Gemini\n\nllm = Gemini()\nresp = await llm.acomplete(\"Llamas are famous for \")\nprint(resp)\n```\n\nFor asynchronous streaming of responses:\n\n```python\nresp = await llm.astream_complete(\"Llamas are famous for \")\nasync for chunk in resp:\n    print(chunk.text, end=\"\")\n```\n\n### LLM Implementation example\n\nhttps://docs.llamaindex.ai/en/stable/examples/llm/gemini/\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "llama-index llms gemini integration",
    "version": "0.4.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a8e9ebcf230847c53cbbf58b1ef9ff42425a1dcef07b73d12819d5b8d01fa5e0",
                "md5": "cf7f1f57d5617b88ca7bd62805a8e311",
                "sha256": "5a82c78cabec0f1869051203cea603068eabb0256093a9fe51e5f3b20a34056d"
            },
            "downloads": -1,
            "filename": "llama_index_llms_gemini-0.4.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cf7f1f57d5617b88ca7bd62805a8e311",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 6325,
            "upload_time": "2024-12-18T22:12:27",
            "upload_time_iso_8601": "2024-12-18T22:12:27.882313Z",
            "url": "https://files.pythonhosted.org/packages/a8/e9/ebcf230847c53cbbf58b1ef9ff42425a1dcef07b73d12819d5b8d01fa5e0/llama_index_llms_gemini-0.4.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "07597c31ebfa2e297eb7eeb5e42faf686788db778ffd0b44532d5b1c21e8b835",
                "md5": "04cdb6a3f0f606286ac9acc858210813",
                "sha256": "d4fad64f8e6f8eb5657e493e853bf448fdd7e83d1eab68a94428433119bd4e4d"
            },
            "downloads": -1,
            "filename": "llama_index_llms_gemini-0.4.2.tar.gz",
            "has_sig": false,
            "md5_digest": "04cdb6a3f0f606286ac9acc858210813",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 5432,
            "upload_time": "2024-12-18T22:12:30",
            "upload_time_iso_8601": "2024-12-18T22:12:30.192649Z",
            "url": "https://files.pythonhosted.org/packages/07/59/7c31ebfa2e297eb7eeb5e42faf686788db778ffd0b44532d5b1c21e8b835/llama_index_llms_gemini-0.4.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-18 22:12:30",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "llama-index-llms-gemini"
}
        
Elapsed time: 0.55008s