-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from quotient-ai/jamesliounis/autojudge
Add new `AutoJudge` class for custom judges
- Loading branch information
Showing
8 changed files
with
987 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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,78 @@ | ||
import logging | ||
|
||
import instructor | ||
import openai | ||
|
||
from pydantic import BaseModel | ||
from tenacity import retry, wait_random_exponential, stop_after_attempt | ||
|
||
openai._utils._logs.logger.setLevel(logging.WARNING) | ||
openai._utils._logs.httpx_logger.setLevel(logging.WARNING) | ||
|
||
|
||
def llm_client(): | ||
try: | ||
import litellm | ||
except ImportError: | ||
# fallback to openai | ||
client = openai.OpenAI() | ||
return client | ||
else: | ||
return litellm | ||
|
||
|
||
@retry( | ||
wait=wait_random_exponential(min=1, max=60), | ||
stop=stop_after_attempt(5), | ||
) | ||
def get_completion( | ||
messages: list[dict[str, str]], | ||
model: str, | ||
temperature: float, | ||
max_tokens: int, | ||
seed: int, | ||
response_format: dict = None, | ||
response_model: BaseModel = None, | ||
): | ||
client = llm_client() | ||
|
||
if response_format and response_model: | ||
raise Exception("response_format and response_model cannot both be provided. please provide only one.") | ||
|
||
if response_model and response_format is None: | ||
if client.__class__.__name__ == "OpenAI": | ||
client = instructor.from_openai(client) | ||
elif hasattr(client, "__name__") and client.__name__ == "litellm": | ||
client = instructor.from_litellm(client.completion) | ||
else: | ||
raise Exception("unknown client. please create an issue on GitHub if you see this message.") | ||
|
||
response = client.chat.completions.create( | ||
model=model, | ||
max_tokens=max_tokens, | ||
temperature=temperature, | ||
messages=messages, | ||
seed=seed, | ||
response_model=response_model, | ||
) | ||
else: | ||
if client.__class__.__name__ == "OpenAI": | ||
response = client.chat.completions.create( | ||
model=model, | ||
max_tokens=max_tokens, | ||
temperature=temperature, | ||
messages=messages, | ||
seed=seed, | ||
response_format=response_format, | ||
) | ||
elif hasattr(client, "__name__") and client.__name__ == "litellm": | ||
response = client.completion( | ||
model=model, | ||
max_tokens=max_tokens, | ||
temperature=temperature, | ||
messages=messages, | ||
seed=seed, | ||
response_format=response_format, | ||
) | ||
|
||
return response |
This file contains 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 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,3 @@ | ||
from judges.classifiers.auto.core import AutoJudge | ||
|
||
__all__ = ["AutoJudge"] |
Oops, something went wrong.