Skip to content

Commit

Permalink
fix: coherence model name for gemini (run-llama#13791)
Browse files Browse the repository at this point in the history
  • Loading branch information
titouv authored May 30, 2024
1 parent 0d04437 commit 3113bec
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import typing
from typing import Any, Dict, Optional, Sequence
import warnings

from llama_index.core.base.llms.types import (
ChatMessage,
Expand Down Expand Up @@ -51,23 +52,22 @@


class Gemini(CustomLLM):
"""Gemini LLM.
"""
Gemini LLM.
Examples:
`pip install llama-index-llms-gemini`
```python
from llama_index.llms.gemini import Gemini
llm = Gemini(model_name="models/gemini-ultra", api_key="YOUR_API_KEY")
llm = Gemini(model="models/gemini-ultra", api_key="YOUR_API_KEY")
resp = llm.complete("Write a poem about a magic backpack")
print(resp)
```
"""

model_name: str = Field(
default=GEMINI_MODELS[0], description="The Gemini model to use."
)
model: str = Field(default=GEMINI_MODELS[0], description="The Gemini model to use.")
temperature: float = Field(
default=DEFAULT_TEMPERATURE,
description="The temperature to use during generation.",
Expand All @@ -89,14 +89,15 @@ class Gemini(CustomLLM):
def __init__(
self,
api_key: Optional[str] = None,
model_name: Optional[str] = GEMINI_MODELS[0],
model: Optional[str] = GEMINI_MODELS[0],
temperature: float = DEFAULT_TEMPERATURE,
max_tokens: Optional[int] = None,
generation_config: Optional["genai.types.GenerationConfigDict"] = None,
safety_settings: "genai.types.SafetySettingOptions" = None,
callback_manager: Optional[CallbackManager] = None,
api_base: Optional[str] = None,
transport: Optional[str] = None,
model_name: Optional[str] = None,
**generate_kwargs: Any,
):
"""Creates a new Gemini model interface."""
Expand All @@ -107,6 +108,13 @@ def __init__(
"Gemini is not installed. Please install it with "
"`pip install 'google-generativeai>=0.3.0'`."
)
if model_name is not None:
warnings.warn(
"model_name is deprecated, please use model instead",
DeprecationWarning,
)

model = model_name

# API keys are optional. The API can be authorised via OAuth (detected
# environmentally) or by the GOOGLE_API_KEY environment variable.
Expand All @@ -125,17 +133,17 @@ def __init__(
final_gen_config = {"temperature": temperature, **base_gen_config}

self._model = genai.GenerativeModel(
model_name=model_name,
model_name=model,
generation_config=final_gen_config,
safety_settings=safety_settings,
)

self._model_meta = genai.get_model(model_name)
self._model_meta = genai.get_model(model)

supported_methods = self._model_meta.supported_generation_methods
if "generateContent" not in supported_methods:
raise ValueError(
f"Model {model_name} does not support content generation, only "
f"Model {model} does not support content generation, only "
f"{supported_methods}."
)

Expand All @@ -145,7 +153,7 @@ def __init__(
max_tokens = min(max_tokens, self._model_meta.output_token_limit)

super().__init__(
model_name=model_name,
model=model,
temperature=temperature,
max_tokens=max_tokens,
generate_kwargs=generate_kwargs,
Expand All @@ -162,7 +170,7 @@ def metadata(self) -> LLMMetadata:
return LLMMetadata(
context_window=total_tokens,
num_output=self.max_tokens,
model_name=self.model_name,
model_name=self.model,
is_chat_model=True,
)

Expand Down

0 comments on commit 3113bec

Please sign in to comment.