Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 src/generative_ai_toolkit/metrics/modules/conciseness.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def evaluate_conversation(self, conversation_traces, **kwargs):
Example output:
{{ "score": 9, "reasoning": "The agent's responses are concise, and it does not provide superfluous examples or useless encouragements."}}

Only return the valid JSON object.
Only return the valid JSON object. Do not wrap it in markdown code blocks or any other formatting.
"""
)
.format(conversation=json.dumps(user_conversation))
Expand Down
2 changes: 1 addition & 1 deletion src/generative_ai_toolkit/metrics/modules/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def evaluate_conversation(self, conversation_traces, **kwargs):
Example output:
{{ "score": 9, "reasoning": "The agent succeeded in helping the user as expected"}}

Only return the JSON object.
Only return the JSON object. Do not wrap it in markdown code blocks or any other formatting.
"""
)
.format(
Expand Down
2 changes: 2 additions & 0 deletions src/generative_ai_toolkit/metrics/modules/latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def evaluate_trace(self, trace, **kwargs):
dimensions.append({"ConversationHistory": "add-message"})
elif trace_type in {"converse", "converse-stream"}:
dimensions.append({"Converse": trace_type})
elif trace_type == "cycle":
dimensions.append({"Cycle": "agent-cycle"})
else:
logger.warn("Unknown trace type", trace_type=trace_type)

Expand Down
16 changes: 16 additions & 0 deletions src/generative_ai_toolkit/utils/llm_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ def get_text(response: "ConverseResponseTypeDef"):

def json_parse(response: "ConverseResponseTypeDef"):
text = get_text(response).strip()

# Handle markdown code blocks
if text.startswith("```json"):
# Find the closing ``` and extract content between
end_marker = text.rfind("```")
if end_marker > 7: # Make sure we found a closing marker after ```json
text = text[7:end_marker].strip() # Remove ```json and closing ```
elif text.startswith("```") and text.count("```") >= 2:
# Handle generic code blocks that might contain JSON
first_newline = text.find('\n')
if first_newline != -1:
# Skip the opening ``` line
end_marker = text.rfind("```")
if end_marker > first_newline:
text = text[first_newline+1:end_marker].strip()

try:
return json.loads(text.replace("\n", " "))
except json.decoder.JSONDecodeError as e:
Expand Down