-
-
Notifications
You must be signed in to change notification settings - Fork 377
Add Groq model support to LLMClient (#1977) #2165
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
Merged
henchaves
merged 20 commits into
Giskard-AI:main
from
kunjanshah0811:add-groq-client-support
Jul 3, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
930099f
feat(llm): add GroqClient support to set any Groq model as default LL…
10a4b68
"Address PR feedback: make JSON format support less hardcoded and fix…
97ac6c6
Fix: Issues of previous comments
b9d3f01
Docs: Added Groq Client
97dc016
Fix: Linting Error
1659045
Fix: Resolve test failures and ensure all tests pass locally
3bc5b74
Merge branch 'main' into add-groq-client-support
kunjanshah0811 4f27293
Add get_config in groq_client.py file
ef817bf
Fix: code formatting via pre-commit
3a6362a
Fix: ValueError: Could not load model sentence-transformers/paraphras…
ff02a9e
Fix: Remove duplicate dependencies and organize LLM-related packages
8af4202
Apply suggestions from code review
davidberenstein1957 a70fbb6
fix: removed extra spaces in pyproject.toml
1fb5174
Merge branch 'add-groq-client-support' of https://github.com/kunjansh…
7c86562
refactor(llm): Revert making Groq a default LLM client
ff8d141
docs: Add note about Groq embedding support
c0f3a5b
Merge branch 'main' into add-groq-client-support
henchaves 9ad79ec
undo changes in llm/client/__init__
henchaves 92981aa
update docs order
henchaves 7c5b277
update pdm.lock
henchaves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from typing import Optional, Sequence | ||
|
|
||
| import logging | ||
| from dataclasses import asdict | ||
|
|
||
| from ..config import LLMConfigurationError | ||
| from ..errors import LLMImportError | ||
| from . import LLMClient | ||
| from .base import ChatMessage | ||
|
|
||
| try: | ||
| import groq | ||
| from groq import Groq | ||
| except ImportError as err: | ||
| raise LLMImportError(flavor="llm") from err | ||
|
|
||
| AUTH_ERROR_MESSAGE = ( | ||
| "Could not authenticate with Groq API. Please make sure you have configured the API key by " | ||
| "setting GROQ_API_KEY in the environment." | ||
| ) | ||
|
|
||
| JSON_MODE_GUIDANCE = ( | ||
| "To use JSON mode, make sure:\n" | ||
| "1. Pass format='json' or format='json_object' to the `complete()` method.\n" | ||
| "2. You describe the expected JSON structure clearly in the system prompt.\n" | ||
| "3. The selected model supports JSON output.\n" | ||
| "See: https://console.groq.com/docs/text-chat#json-mode" | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class GroqClient(LLMClient): | ||
| def __init__( | ||
| self, | ||
| model: str = "llama-3.3-70b-versatile", # Default model for Groq | ||
| client: Groq = None, | ||
| # json_mode: Optional[bool] = None | ||
| ): | ||
| logger.info(f"Initializing GroqClient with model: {model}") | ||
| self.model = model | ||
| self._client = client or Groq() | ||
| logger.info("GroqClient initialized successfully") | ||
|
|
||
| def get_config(self) -> dict: | ||
| """Return the configuration of the LLM client.""" | ||
| return {"client_type": self.__class__.__name__, "model": self.model} | ||
|
|
||
| def complete( | ||
| self, | ||
| messages: Sequence[ChatMessage], | ||
| temperature: float = 1.0, | ||
| max_tokens: Optional[int] = None, | ||
| caller_id: Optional[str] = None, | ||
| seed: Optional[int] = None, | ||
| format: Optional[str] = None, | ||
| ) -> ChatMessage: | ||
| logger.info(f"GroqClient.complete called with model: {self.model}") | ||
| logger.info(f"Messages: {messages}") | ||
|
|
||
| extra_params = dict() | ||
|
|
||
| extra_params["seed"] = seed | ||
|
|
||
| if format in {"json", "json_object"}: | ||
| extra_params["response_format"] = {"type": "json_object"} | ||
|
|
||
| try: | ||
| completion = self._client.chat.completions.create( | ||
| model=self.model, | ||
| messages=[asdict(m) for m in messages], | ||
| temperature=temperature, | ||
| max_tokens=max_tokens, | ||
| **extra_params, | ||
| ) | ||
|
|
||
| except groq.AuthenticationError as err: | ||
| raise LLMConfigurationError(AUTH_ERROR_MESSAGE) from err | ||
|
|
||
| except groq.BadRequestError as err: | ||
| if format in {"json", "json_object"}: | ||
| raise LLMConfigurationError( | ||
| f"Model '{self.model}' does not support JSON output or the request format is incorrect.\n\n{JSON_MODE_GUIDANCE}" | ||
| ) from err | ||
| raise | ||
|
|
||
| self.logger.log_call( | ||
| prompt_tokens=completion.usage.prompt_tokens, | ||
| sampled_tokens=completion.usage.completion_tokens, | ||
| model=self.model, | ||
| client_class=self.__class__.__name__, | ||
| caller_id=caller_id, | ||
| ) | ||
|
|
||
| msg = completion.choices[0].message | ||
|
|
||
| return ChatMessage(role=msg.role, content=msg.content) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.