-
Notifications
You must be signed in to change notification settings - Fork 1
VPC Chat completions API #74
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3978e47
add skeleton wrapper
huiwengoh 53430bd
naming changes
huiwengoh f4c7f66
move to vpc subfolder
huiwengoh 6d8663c
Merge branch 'main' into vpc-api
huiwengoh 4c8b95c
vpc updates
huiwengoh f6a0e3b
better unify kwargs
huiwengoh 977057d
add docstring
huiwengoh 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
Empty file.
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,99 @@ | ||
""" | ||
Real-time evaluation of responses from OpenAI Chat Completions API. | ||
|
||
If you are using OpenAI's Chat Completions API, this module allows you to incorporate TLM trust scoring without any change to your existing code. | ||
It works for any OpenAI LLM model, as well as the many other non-OpenAI LLMs that are also usable via Chat Completions API (Gemini, DeepSeek, Llama, etc). | ||
|
||
This module is specifically for VPC users of TLM, the BASE_URL environment variable must be set to the VPC endpoint. | ||
If you are not using VPC, use the `cleanlab_tlm.utils.chat_completions` module instead. | ||
""" | ||
|
||
import os | ||
from typing import TYPE_CHECKING, Any, Optional | ||
huiwengoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import requests | ||
|
||
from cleanlab_tlm.internal.base import BaseTLM | ||
from cleanlab_tlm.internal.constants import _VALID_TLM_QUALITY_PRESETS_CHAT_COMPLETIONS | ||
from cleanlab_tlm.internal.types import JSONDict | ||
from cleanlab_tlm.tlm import TLMOptions | ||
|
||
if TYPE_CHECKING: | ||
from openai.types.chat import ChatCompletion | ||
|
||
|
||
class TLMChatCompletion(BaseTLM): | ||
""" | ||
Represents a Trustworthy Language Model (TLM) instance specifically designed for evaluating OpenAI Chat Completions responses. | ||
|
||
This class provides a TLM wrapper that can be used to evaluate the quality and trustworthiness of responses from any OpenAI model | ||
by passing in the inputs to OpenAI's Chat Completions API and the ChatCompletion response object. | ||
|
||
This module is specifically for VPC users of TLM, the BASE_URL environment variable must be set to the VPC endpoint. | ||
If you are not using VPC, use the `cleanlab_tlm.utils.chat_completions` module instead. | ||
|
||
Args: | ||
quality_preset ({"base", "low", "medium"}, default = "medium"): an optional preset configuration to control | ||
the quality of TLM trustworthiness scores vs. latency/costs. | ||
|
||
options ([TLMOptions](#class-tlmoptions), optional): a typed dict of configurations you can optionally specify. | ||
See detailed documentation under [TLMOptions](#class-tlmoptions). | ||
|
||
timeout (float, optional): timeout (in seconds) to apply to each TLM evaluation. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
quality_preset: str = "medium", | ||
*, | ||
options: Optional[TLMOptions] = None, | ||
timeout: Optional[float] = None, | ||
): | ||
""" | ||
lazydocs: ignore | ||
""" | ||
super().__init__( | ||
quality_preset=quality_preset, | ||
valid_quality_presets=_VALID_TLM_QUALITY_PRESETS_CHAT_COMPLETIONS, | ||
support_custom_eval_criteria=True, | ||
api_key=".", | ||
options=options, | ||
timeout=timeout, | ||
verbose=False, | ||
) | ||
|
||
def score( | ||
self, | ||
huiwengoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*, | ||
response: "ChatCompletion", | ||
**openai_kwargs: Any, | ||
) -> JSONDict: | ||
"""Score the trustworthiness of an OpenAI ChatCompletion response. | ||
|
||
Args: | ||
response (ChatCompletion): The OpenAI ChatCompletion response object to evaluate | ||
**openai_kwargs (Any): The original kwargs passed to OpenAI's create() method, must include 'messages' | ||
|
||
Returns: | ||
TLMScore: A dict containing the trustworthiness score and optional logs | ||
""" | ||
if (base_url := os.environ.get("BASE_URL")) is None: | ||
raise ValueError("BASE_URL is not set. Please set it in the environment variables.") | ||
|
||
# replace the model used for scoring with the specified model in options | ||
openai_kwargs["model"] = self._options["model"] | ||
|
||
res = requests.post( | ||
f"{base_url}/chat/score", | ||
json={ | ||
"quality_preset": self._quality_preset, | ||
"options": self._options, | ||
"completion": response.model_dump(), | ||
**openai_kwargs, | ||
}, | ||
timeout=self._timeout, | ||
) | ||
|
||
res_json = res.json() | ||
|
||
return {"trustworthiness_score": res_json["tlm_metadata"]["trustworthiness_score"]} |
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.
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.
TODO: Confirm this VPC version matches our SaaS API as closely as we are easily able to. The analogous SaaS API is defined here:
#75