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

[pull] main from explodinggradients:main #102

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Objective metrics, intelligent test generation, and data-driven insights for LLM
Ragas is your ultimate toolkit for evaluating and optimizing Large Language Model (LLM) applications. Say goodbye to time-consuming, subjective assessments and hello to data-driven, efficient evaluation workflows.
Don't have a test dataset ready? We also do production-aligned test set generation.

> [!NOTE]
> Need help setting up Evals for your AI application? We'd love to help! We are conducting Office Hours every week. You can sign up [here](https://cal.com/team/ragas/office-hours).

## Key Features

- 🎯 Objective Metrics: Evaluate your LLM applications with precision using both LLM-based and traditional metrics.
Expand Down
11 changes: 11 additions & 0 deletions docs/extra/overrides/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}

{% block announce %}
<!-- Add announcement here, including arbitrary HTML -->
<div class="announce">
<p>
<strong>Ragas Office Hours</strong> - If you need help setting up Evals for your AI application, sign up for our
Office Hours <a href="https://cal.com/team/ragas/office-hours">here</a>.
</p>
</div>
{% endblock %}
44 changes: 23 additions & 21 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,27 @@ validation:
# Material-Docs Theme
theme:
name: material
custom_dir: docs/extra/overrides
logo: _static/imgs/ragas-logo.png
favicon: _static/favicon.ico
features:
- announce.dismiss
- content.tabs.link
- content.code.annotate
- content.code.copy
- announce.dismiss
- navigation.tabs
- navigation.path
- navigation.instant
- navigation.instant.prefetch
- navigation.instant.preview
- navigation.sections
- navigation.top
- navigation.tracking
- navigation.indexes
- navigation.footer
- search.suggest
- search.highlight
palette:
# Palette toggle for automatic mode
- media: "(prefers-color-scheme)"
Expand All @@ -160,23 +179,7 @@ theme:
toggle:
icon: material/brightness-4
name: Switch to system preference
features:
- content.tabs.link
- content.code.annotate
- content.code.copy
- announce.dismiss
- navigation.tabs
- navigation.path
- navigation.instant
- navigation.instant.prefetch
- navigation.instant.preview
- navigation.sections
- navigation.top
- navigation.tracking
- navigation.indexes
- navigation.footer
- search.suggest
- search.highlight

markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
Expand Down Expand Up @@ -215,7 +218,6 @@ extra:
property: !ENV GOOGLE_ANALYTICS_KEY
plugins:
- search
- social
- git-revision-date-localized:
enabled: !ENV [MKDOCS_CI, false]
enable_creation_date: true
Expand All @@ -238,9 +240,9 @@ plugins:
show_signature_annotations: true
signature_crossrefs: true
- glightbox
- gen-files:
scripts:
- docs/ipynb_to_md.py
# - gen-files:
# scripts:
# - docs/ipynb_to_md.py
extra_javascript:
- _static/js/mathjax.js
- _static/js/header_border.js
Expand Down
13 changes: 13 additions & 0 deletions src/ragas/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import requests

from ragas._analytics import get_userid
from ragas._version import __version__
from ragas.exceptions import UploadException
from ragas.utils import base_logger

# endpoint for uploading results
RAGAS_API_URL = "https://api.ragas.io"
Expand Down Expand Up @@ -46,6 +48,7 @@ def upload_packet(path: str, data_json_string: str):
"x-app-token": app_token,
"x-source": RAGAS_API_SOURCE,
"x-app-version": __version__,
"x-ragas-lib-user-uuid": get_userid(),
},
)
check_api_response(response)
Expand All @@ -67,6 +70,11 @@ def check_api_response(response: requests.Response) -> None:
If authentication fails or other API errors occur
"""
if response.status_code == 403:
base_logger.error(
"[AUTHENTICATION_ERROR] The app token is invalid. "
"Please check your RAGAS_APP_TOKEN environment variable. "
f"Response Status: {response.status_code}, URL: {response.url}"
)
raise UploadException(
status_code=response.status_code,
message="AUTHENTICATION_ERROR: The app token is invalid. Please check your RAGAS_APP_TOKEN environment variable.",
Expand All @@ -85,6 +93,11 @@ def check_api_response(response: requests.Response) -> None:
except Exception as _e:
error_msg = f"\nStatus Code: {response.status_code}"

base_logger.error(
f"[API_ERROR] Request failed. "
f"Status Code: {response.status_code}, URL: {response.url}, "
f"Error Message: {error_msg}"
)
raise UploadException(
status_code=response.status_code, message=f"Request failed: {error_msg}"
)
Expand Down
59 changes: 55 additions & 4 deletions src/ragas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import tiktoken
from datasets import Dataset

from datetime import datetime

if t.TYPE_CHECKING:
from ragas.metrics.base import Metric

Expand Down Expand Up @@ -238,20 +240,69 @@ def batched(iterable: t.Iterable, n: int) -> t.Iterator[t.Tuple]:
yield batch


def set_logging_level(logger_name: str, level: int):
_LOGGER_DATE_TIME = "%Y-%m-%d %H:%M:%S"


def set_logging_level(logger_name: str = __name__, level: int = logging.DEBUG):
"""
Set the logging level for a logger. Useful for debugging.
"""
logger = logging.getLogger(logger_name)
logger.setLevel(level)

log_format = (
"[%(local_time)s - (%(utc_time)s UTC)] "
"[%(levelname)s] [%(name)s] "
"[RagasID: %(ragas_id)s, App-Version: %(app_version)s] %(message)s"
)

# Create a formatter with the custom formatter
formatter = _ContextualFormatter(log_format, datefmt=_LOGGER_DATE_TIME)

# Create a console handler and set its level
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setLevel(level)

# Create a formatter and add it to the handler
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
# Apply the formatter to the handler
console_handler.setFormatter(formatter)

# Add the handler to the logger
logger.addHandler(console_handler)

return logger


class _ContextualFormatter(logging.Formatter):
"""
Custom logging formatter that adds context to the log records.
"""

def format(self, record):
from ragas._analytics import get_userid
from ragas import __version__

# Add UTC time
record.utc_time = self.format_time(record, _LOGGER_DATE_TIME)
# Add local time
record.local_time = self.format_time(record, _LOGGER_DATE_TIME, local_time=True)
# Add additional context
record.ragas_id = get_userid()
record.app_version = __version__
return super().format(record)

def format_time(self, record, datefmt=None, local_time=False):
dt = (
self.utc_converter(record.created)
if not local_time
else datetime.fromtimestamp(record.created)
)
if datefmt:
return dt.strftime(datefmt)
return dt.isoformat()

@staticmethod
def utc_converter(timestamp):
return datetime.utcfromtimestamp(timestamp) # UTC time conversion


base_logger = set_logging_level()
Loading