Skip to content

Commit

Permalink
feat(LAB-3307): handle export of comparison in llm_static
Browse files Browse the repository at this point in the history
  • Loading branch information
paulruelle committed Dec 30, 2024
1 parent 9f95ea3 commit 3856366
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/kili/domain/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ProjectModelDict(TypedDict):
class ChatItem(TypedDict):
"""Dict that represents a ChatItem."""

id: str
content: str
external_id: str
model_name: Optional[str]
Expand All @@ -153,8 +154,9 @@ class ConversationLabel(TypedDict):


class Conversation(TypedDict):
"""Dict that represents a Conversation."""

external_id: Optional[str]
chat_items: List[ChatItem]
label: Optional[ConversationLabel]
labeler: str
metadata: Optional[dict]
38 changes: 27 additions & 11 deletions src/kili/llm/services/export/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ def export(
for asset in assets:
# obfuscate models here
obfuscated_models = {}
for index, asset_project_model in enumerate(asset["assetProjectModels"]):
obfuscated_models[asset_project_model["id"]] = f"{chr(65 + index)}"
if asset.get("assetProjectModels"):
for index, asset_project_model in enumerate(asset["assetProjectModels"]):
obfuscated_models[asset_project_model["id"]] = f"{chr(65 + index)}"
else:
model_names = {
chat_item.get("modelName")
for chat_item in asset["labels"][0]["chatItems"]
if chat_item.get("modelName") is not None
}
obfuscated_models = {
model_name: f"{chr(65 + index)}" for index, model_name in enumerate(model_names)
}

for label in asset["labels"]:
result = {}
chat_items = label["chatItems"]
Expand Down Expand Up @@ -81,17 +92,23 @@ def export(
if step == total_rounds - 1 and formatted_response["conversation"]:
label_data["label"]["conversation"] = formatted_response["conversation"]

if asset.get("assetProjectModels"):
models = _format_models_object(
asset["assetProjectModels"], obfuscated_models
)
else:
models = {v: k for k, v in obfuscated_models.items()}

result[f"{step}"] = {
"external_id": asset["externalId"],
"metadata": asset["jsonMetadata"],
"models": _format_models_object(
asset["assetProjectModels"], obfuscated_models
),
"models": models,
"labels": [label_data],
"raw_data": raw_data,
"status": asset["status"],
}
export_res.append(result)

return export_res

def _get_round_winner(self, completions, annotations, json_interface):
Expand Down Expand Up @@ -221,23 +238,22 @@ def _format_transcription_annotation(annotation):

def _format_comparison_annotation(annotation, completions, job, obfuscated_models):
"""Return A_X or B_X depending of the evaluation completion and its score."""
model_id = None
model_key = None
for completion in completions:
if annotation["annotationValue"]["choice"]["firstId"] == completion["id"]:
model_id = completion["modelId"]
model_key = completion.get("modelId") or completion.get("modelName")
break

if model_id is None:
# FIXME : model_id can be null on LLM_STATIC
return None
if model_key is None:
raise ValueError(f"Failed to found model of annotation {annotation['id']}")

iteration = 0
for _comparison_code, comparison_note in job["content"]["options"].items():
iteration += 1
if comparison_note["name"] == annotation["annotationValue"]["choice"]["code"]:
break

return f"{obfuscated_models[model_id]}_{iteration}"
return f"{obfuscated_models[model_key]}_{iteration}"


def _format_json_response(
Expand Down

0 comments on commit 3856366

Please sign in to comment.