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
Gemini clent test
  • Loading branch information
abdellah257 committed Jun 8, 2024
commit 477891b57dc591a9b4384a363470ce9c1bcd964e
32 changes: 32 additions & 0 deletions tests/llm/test_llm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from openai.types import CompletionUsage
from openai.types.chat import ChatCompletion, ChatCompletionMessage
from openai.types.chat.chat_completion import Choice
from google.generativeai.types import ContentDict

from giskard.llm.client import ChatMessage
from giskard.llm.client.bedrock import ClaudeBedrockClient
from giskard.llm.client.mistral import MistralClient
from giskard.llm.client.openai import OpenAIClient
from giskard.llm.client.gemini import GeminiClient

DEMO_OPENAI_RESPONSE = ChatCompletion(
id="chatcmpl-abc123",
Expand Down Expand Up @@ -119,3 +121,33 @@ def test_claude_bedrock_client():
# Assert that the response is a ChatMessage and has the correct content
assert isinstance(res, ChatMessage)
assert res.content == "This is a test!"

def test_gemini_client():
# Mock the Gemini client
gemini_api_client = Mock()
gemini_api_client.generate_content = MagicMock(
return_value=Mock(
text="This is a test!",
candidates=[Mock(content=Mock(role="assistant"))]
)
)
gemini_api_client.count_tokens = MagicMock(
side_effect=lambda text: sum(len(t.split()) for t in text) if isinstance(text, list) else len(text.split())
)

# Initialize the GeminiClient with the mocked gemini_api_client
client = GeminiClient(model="gemini-pro", _client=gemini_api_client)

# Call the complete method
res = client.complete([ChatMessage(role="user", content="Hello")], temperature=0.11, max_tokens=12)
print(res)

# Assert that the generate_content method was called with the correct arguments
gemini_api_client.generate_content.assert_called_once()
assert gemini_api_client.generate_content.call_args[1]["contents"] == ([ContentDict(role="user", parts="Hello")])
assert gemini_api_client.generate_content.call_args[1]["generation_config"].temperature == 0.11
assert gemini_api_client.generate_content.call_args[1]["generation_config"].max_output_tokens == 12

# Assert that the response is a ChatMessage and has the correct content
assert isinstance(res, ChatMessage)
assert res.content == "This is a test!"