-
-
Notifications
You must be signed in to change notification settings - Fork 381
Support for custom LLM clients #1839
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
Merged
Changes from all commits
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
035ed40
Big refactoring: break everything, and rebuild simpler
mattbit ed615b8
Start clean up of evaluators
mattbit 261a5bf
Update prompt for harmful content detector
mattbit 2d1257e
Fixing base evaluator
mattbit 9860b6b
Enforce JSON on OpenAI
mattbit 0ca7cef
Fix response format in OpenAI client
mattbit 9259921
Refactoring of generators and evaluators
mattbit ba407b4
Merge branch 'main' into feature/llm-support
mattbit 6e7251e
Fix format of evaluation results
mattbit edab964
Update test_coherency_evaluator.py
mattbit 5478529
More bug fixing
mattbit 0ff3ff4
Fix LLMClient test
mattbit 7f37c6b
Add language support
mattbit 55a94ff
Fix sycophancy test
mattbit 1385205
Improve error for missing mistralai dependency
mattbit ec153a3
Test result from evaluators
mattbit dd823f8
Update tests
mattbit 419434e
Fix faithfulness detector
mattbit 3923435
Fix correctness test
mattbit 977b540
Fix LLM seed
mattbit 9737b45
Shorten info disclosure category
mattbit f2aa8e2
Improve mistral support
mattbit bbba96d
Adding logger
mattbit 907dfb7
Fix experimental output formatting in sycophancy
mattbit 2a8248d
Fix tests
mattbit 4fb2a85
Conditionally enable json response for Mistral models
mattbit e81f2c4
Merge branch 'main' into feature/llm-support
mattbit 0f15588
Remove extraneous files
mattbit 7dcdb46
Fix type hint on CoherencyEvaluator
mattbit 7e562d0
Fixing type hints
mattbit e9c6be6
Added `details` serialization to allow upload of data to the Hub
kevinmessiaen b6164e5
Fixed typo
kevinmessiaen bbab3e2
Fixed LLM tests
kevinmessiaen 3c0eb3f
Merge branch 'main' into feature/llm-support
mattbit ad0e0c3
Extracted serialization and updated structure
kevinmessiaen 05c646f
Simplified structure
kevinmessiaen a60f17a
Simplified structure
kevinmessiaen e3715ac
Adapted `EvaluationResult` to existing `TestDetails` so that Hub feat…
kevinmessiaen 920a5b9
Fixed tests
kevinmessiaen c098d03
Fixed tests
kevinmessiaen d4313e1
Merge branch 'main' into feature/llm-support
mattbit 647cc55
Update ChatMessage in RAG toolkit
mattbit d4cc9e6
Add embeddings support
mattbit 2f57c2a
Use embeddings in the RAG knowledge base
mattbit 9412535
Remove torch import from openai embeddings
mattbit f99517e
Improve embeddings
mattbit 3c0fac7
Update RAG toolkit tests
mattbit ac9faf1
Load default embedding model with knowledge base
mattbit aa25458
Add test for Mistral
mattbit 54c6eea
Regenerating pdm.lock
9e9aeb3
Cleanup
mattbit 841357f
Merge branch 'feature/llm-support' of github.com:Giskard-AI/giskard i…
mattbit deda5e4
Fix simple test dataset generation
mattbit 36f6dc6
Add hosted requirement evaluator
luca-rossi 18f81ea
Remove uppercas F in examples, so model return proper JSON
Hartorn e4c0acb
Merge pull request #1877 from Giskard-AI/fix-typo
mattbit 2dd9a7a
Better handling of json format option
mattbit 3f72f47
Merge branch 'main' into feature/llm-support
mattbit 5ba3d04
Regenerating pdm.lock
3e9232b
Updating LLM scan outputs
mattbit 85b21a9
Small refactoring
mattbit 575272f
Better requirement parsing to tolerate for LLM errors
mattbit 58ba03c
Fix type hint
mattbit fa68891
Merge branch 'refs/heads/main' into feature/llm-support
andreybavt ed850f8
post merge `main`
andreybavt 269437a
Merge branch 'main' into feature/llm-support
andreybavt cdea946
Regenerating pdm.lock
05622ed
Merge branch 'main' into feature/llm-support
kevinmessiaen d254e50
Small improvements
kevinmessiaen 7a9a9b7
Fixed coherency test
kevinmessiaen 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 |
|---|---|---|
| @@ -1,59 +1,33 @@ | ||
| from typing import Any, Dict, List, Optional, Sequence | ||
| from typing import Optional, Sequence | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from dataclasses import dataclass | ||
|
|
||
| import numpy as np | ||
|
|
||
| from .logger import LLMLogger | ||
|
|
||
|
|
||
| @dataclass | ||
| class LLMFunctionCall: | ||
| name: str | ||
| arguments: Any | ||
|
|
||
|
|
||
| @dataclass | ||
| class LLMToolCall: | ||
| id: str | ||
| type: str | ||
| function: LLMFunctionCall | ||
|
|
||
|
|
||
| @dataclass | ||
| class LLMMessage: | ||
| class ChatMessage: | ||
| role: str | ||
| content: Optional[str] = None | ||
| function_call: Optional[LLMFunctionCall] = None | ||
| tool_calls: Optional[List[LLMToolCall]] = None | ||
|
|
||
| @staticmethod | ||
| def create_message(role: str, content: str): | ||
| return LLMMessage(role=role, content=content, function_call=None, tool_calls=None) | ||
|
|
||
| _logger = LLMLogger() | ||
|
|
||
|
|
||
| class LLMClient(ABC): | ||
| @property | ||
| @abstractmethod | ||
| def logger(self) -> LLMLogger: | ||
| ... | ||
| return _logger | ||
|
|
||
| @abstractmethod | ||
| def complete( | ||
| self, | ||
| messages: Sequence[LLMMessage], | ||
| functions=None, | ||
| temperature=0.5, | ||
| max_tokens=None, | ||
| function_call: Optional[Dict] = None, | ||
| messages: Sequence[ChatMessage], | ||
| temperature: float = 1, | ||
| max_tokens: Optional[int] = None, | ||
| caller_id: Optional[str] = None, | ||
| tools=None, | ||
| tool_choice=None, | ||
| seed: Optional[int] = None, | ||
| ) -> LLMMessage: | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def embeddings(self, text) -> np.ndarray: | ||
| format=None, | ||
| ) -> ChatMessage: | ||
| ... |
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,66 @@ | ||
| from typing import Optional, Sequence | ||
|
|
||
| from dataclasses import asdict | ||
| from logging import warning | ||
|
|
||
| from ..config import LLMConfigurationError | ||
| from ..errors import LLMImportError | ||
| from . import LLMClient | ||
| from .base import ChatMessage | ||
|
|
||
| try: | ||
| from mistralai.client import MistralClient as _MistralClient | ||
| from mistralai.models.chat_completion import ChatMessage as MistralChatMessage | ||
| except ImportError as err: | ||
| raise LLMImportError( | ||
| flavor="llm", msg="To use Mistral models, please install the `mistralai` package with `pip install mistralai`" | ||
| ) from err | ||
|
|
||
|
|
||
| class MistralClient(LLMClient): | ||
| def __init__(self, model: str = "mistral-large-latest", client: _MistralClient = None): | ||
| self.model = model | ||
| self._client = client or _MistralClient() | ||
|
|
||
| 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: str = None, | ||
| ) -> ChatMessage: | ||
| extra_params = dict() | ||
| if seed is not None: | ||
| extra_params["random_seed"] = seed | ||
|
|
||
| if format not in (None, "json", "json_object") and "large" not in self.model: | ||
| warning(f"Unsupported format '{format}', ignoring.") | ||
| format = None | ||
|
|
||
| if format == "json" or format == "json_object": | ||
| extra_params["response_format"] = {"type": "json_object"} | ||
|
|
||
| try: | ||
| completion = self._client.chat( | ||
| model=self.model, | ||
| messages=[MistralChatMessage(**asdict(m)) for m in messages], | ||
| temperature=temperature, | ||
| max_tokens=max_tokens, | ||
| **extra_params, | ||
| ) | ||
| except RuntimeError as err: | ||
| raise LLMConfigurationError("Could not get response from Mistral API") from err | ||
|
|
||
| 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format could be of
Literaltypealso pydantic's
validate_callcould do the validation job