Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemini client #1953

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
dict structure change
  • Loading branch information
Abelaid00 committed May 12, 2024
commit a3d5201a757c6ed6d313094427f98f76c23dab90
23 changes: 19 additions & 4 deletions giskard/llm/client/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from ..errors import LLMImportError
from . import LLMClient
from .base import ChatMessage
from google.generativeai.types import ContentDict
import os

try:
import google.generativeai as genai
Expand All @@ -13,6 +15,16 @@
flavor="llm", msg="To use Gemini models, please install the `genai` package with `pip install google-generativeai`"
) from err

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

AUTH_ERROR_MESSAGE = (
"Could not authenticate with Gemini API. Please make sure you have configured the API key by "
"setting GOOGLE_API_KEY in the environment."
)

def _to_content_dict(messages: Sequence[ChatMessage]) -> Sequence[ContentDict]:
return [ContentDict(role=m.role, parts=m.content) for m in messages]

class GeminiClient(LLMClient):
def __init__(self, model: str = "gemini-pro", _client=None ):
self.model = model
Expand All @@ -37,21 +49,24 @@ def complete(

try:
completion = self._client.generate_content(
messages=[asdict(m) for m in messages],
messages=_to_content_dict(messages),
temperature=temperature,
max_tokens=max_tokens,
**extra_params,
)
except RuntimeError as err:
raise LLMConfigurationError("Could not get response from Gemini API") from err

prompt_tokens = self._client.count_tokens(messages)
completion_tokens = self._client.count_tokens(completion.text)

self.logger.log_call(
prompt_tokens=completion.usage.prompt_tokens,
sampled_tokens=completion.usage.completion_tokens,
prompt_tokens=prompt_tokens,
sampled_tokens=completion_tokens,
model=self.model,
client_class=self.__class__.__name__,
caller_id=caller_id,
)

# Assuming the response structure is similar to the ChatMessage structure
return ChatMessage(role=completion.candidates[0].content.role, content=completion.candidates[0].content.parts)
return ChatMessage(role=completion.candidates[0].content.role, content=completion.text)