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

Response sim context #294

Merged
merged 2 commits into from
Apr 10, 2024
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
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.0.28.dev8
current_version = 0.0.28.dev9
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\.(?P<release>[a-z]+)(?P<build>\d+))?
serialize =
Expand Down
7 changes: 7 additions & 0 deletions langkit/metrics/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def all(prompt: bool = True, response: bool = True) -> MetricCreator:
response_refusal_similarity_metric,
response_presidio_pii_metric,
lib.response.toxicity(),
lib.response.similarity.context(),
lib.response.topics.medicine(),
]

Expand Down Expand Up @@ -512,6 +513,12 @@ def refusal(onnx: bool = True) -> MetricCreator:

return partial(response_refusal_similarity_metric, onnx=onnx)

@staticmethod
def context(onnx: bool = True) -> MetricCreator:
from langkit.metrics.input_context_similarity import input_context_similarity

return partial(input_context_similarity, onnx=onnx, input_column_name="response")

class topics:
def __init__(self, topics: List[str], hypothesis_template: Optional[str] = None, onnx: bool = True):
self.topics = topics
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langkit"
version = "0.0.28.dev8"
version = "0.0.28.dev9"
description = "A language toolkit for monitoring LLM interactions"
authors = ["WhyLabs.ai <[email protected]>"]
homepage = "https://docs.whylabs.ai/docs/large-language-model-monitoring"
Expand Down
22 changes: 22 additions & 0 deletions tests/langkit/metrics/test_input_context_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ def test_similarity():
assert metrics["prompt.similarity.context"][0] == pytest.approx(0.7447172999382019) # pyright: ignore[reportUnknownMemberType]


def test_similarity_repoonse():
wf = Workflow(metrics=[lib.response.similarity.context()])

context: InputContext = {
"entries": [
{"content": "Some source 1", "metadata": {"source": "https://internal.com/foo"}},
{"content": "Some source 2", "metadata": {"source": "https://internal.com/bar"}},
]
}

df = pd.DataFrame({"response": ["Some source"], "context": [context]})

result = wf.run(df)

metrics = result.metrics

metric_names: List[str] = metrics.columns.tolist() # pyright: ignore[reportUnknownMemberType]

assert metric_names == ["response.similarity.context", "id"]
assert metrics["response.similarity.context"][0] == pytest.approx(0.7447172999382019) # pyright: ignore[reportUnknownMemberType]


def test_similarity_missing_context():
# The metric should not be run in this case since the context is missing
wf = Workflow(metrics=[lib.prompt.similarity.context()])
Expand Down
Loading