Skip to content
Open
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
4 changes: 3 additions & 1 deletion js/app/src/pages/trace/span/LLMToolSchemasList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SpanKindIcon } from "@phoenix/components/trace";

import { defaultCardProps } from "./constants";
import { MimeTypeCodeBlock } from "./MimeTypeCodeBlock";
import { getToolSchemaName } from "./utils";

/**
* A card displaying a single tool JSON schema available to the LLM.
Expand All @@ -22,10 +23,11 @@ function LLMToolSchema({
toolSchema: string;
index: number;
}) {
const name = getToolSchemaName(toolSchema);
const titleEl = (
<Flex direction="row" gap="size-100" alignItems="center">
<SpanKindIcon spanKind="tool" />
<Text weight="heavy">Tool</Text>
<Text weight="heavy">{name == null ? "Tool" : `Tool: ${name}`}</Text>
</Flex>
);

Expand Down
31 changes: 31 additions & 0 deletions js/app/src/pages/trace/span/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getRerankerAttributes,
getRetrieverAttributes,
getToolAttributes,
getToolSchemaName,
groupDocumentEvaluationsByPosition,
parseSpanAttributes,
} from "../utils";
Expand Down Expand Up @@ -382,6 +383,36 @@ describe("getToolAttributes", () => {
});
});

describe("getToolSchemaName", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add google, bedrock, and OpenAI response API

it("reads the name of an openai style tool definition", () => {
expect(
getToolSchemaName(
'{"type":"function","function":{"name":"get_weather","parameters":{"type":"object"}}}'
)
).toBe("get_weather");
});

it("reads the name of an anthropic style tool definition", () => {
expect(
getToolSchemaName(
'{"name":"get_weather","input_schema":{"type":"object"}}'
)
).toBe("get_weather");
});

it("falls back to the title of a bare parameter schema", () => {
expect(getToolSchemaName('{"title":"get_weather","type":"object"}')).toBe(
"get_weather"
);
});

it("returns undefined when the schema names no tool", () => {
expect(getToolSchemaName('{"type":"object"}')).toBeUndefined();
expect(getToolSchemaName('{"name":""}')).toBeUndefined();
expect(getToolSchemaName("not json")).toBeUndefined();
});
});

describe("groupDocumentEvaluationsByPosition", () => {
const makeEvaluation = (
documentPosition: number,
Expand Down
22 changes: 21 additions & 1 deletion js/app/src/pages/trace/span/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
toRecordPreview,
toToolCallsPreview,
} from "@phoenix/utils/contentPreviewUtils";
import { safelyParseJSON } from "@phoenix/utils/jsonUtils";
import {
safelyParseJSON,
safelyParseJSONObjectString,
} from "@phoenix/utils/jsonUtils";

import type {
AttributeObject,
Expand Down Expand Up @@ -290,6 +293,23 @@ export function getEmbeddingAttributes(spanAttributes: AttributeObject): {
};
}

/**
* The name of the tool a JSON schema recorded on an LLM span describes.
*
* Makes a best-effort attempt to parse the name out of the schema.
*/
export function getToolSchemaName(toolSchema: string): string | undefined {
const schema = safelyParseJSONObjectString(toolSchema) as
| Record<string, unknown>
| undefined;
if (schema == null) {
return undefined;
}
const fn = (schema.function ?? {}) as Record<string, unknown>;
const name = fn.name ?? schema.name ?? schema.title;
return typeof name === "string" && name !== "" ? name : undefined;
}

/**
* The attributes describing the tool of a tool span.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ def _response_to_oi_message(msg: ModelResponse) -> Message:
def _to_oi_tools(params: ModelRequestParameters) -> list[Tool]:
tools: list[Tool] = []
for tool_def in params.function_tools or []:
schema: dict[str, Any] = {**tool_def.parameters_json_schema}
schema.setdefault("title", tool_def.name)
function: dict[str, Any] = {"name": tool_def.name}
if tool_def.description:
schema.setdefault("description", tool_def.description)
tools.append({"json_schema": schema})
function["description"] = tool_def.description
function["parameters"] = tool_def.parameters_json_schema
tools.append({"json_schema": {"type": "function", "function": function}})
return tools


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ async def test_request_emits_llm_span_for_tool_call_response(
tool_schema_attr = attributes.pop(f"{LLM_TOOLS}.0.{TOOL_JSON_SCHEMA}")
assert isinstance(tool_schema_attr, str)
tool_schema = json.loads(tool_schema_attr)
assert tool_schema["title"] == "get_weather"
assert tool_schema["description"] == "Look up the current weather for a city."
assert tool_schema["required"] == ["city"]
assert tool_schema["type"] == "function"
assert tool_schema["function"]["name"] == "get_weather"
assert tool_schema["function"]["description"] == "Look up the current weather for a city."
assert tool_schema["function"]["parameters"] == weather_tool.parameters_json_schema

assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_ROLE}") == "system"
assert isinstance(attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_CONTENT}"), str)
Expand Down Expand Up @@ -581,9 +582,10 @@ async def test_request_emits_tool_return_message_in_history(
tool_schema_attr = attributes.pop(f"{LLM_TOOLS}.0.{TOOL_JSON_SCHEMA}")
assert isinstance(tool_schema_attr, str)
tool_schema = json.loads(tool_schema_attr)
assert tool_schema["title"] == "get_weather"
assert tool_schema["description"] == "Look up the current weather for a city."
assert tool_schema["required"] == ["city"]
assert tool_schema["type"] == "function"
assert tool_schema["function"]["name"] == "get_weather"
assert tool_schema["function"]["description"] == "Look up the current weather for a city."
assert tool_schema["function"]["parameters"] == weather_tool.parameters_json_schema

# Message 0: system prompt.
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_ROLE}") == "system"
Expand Down
Loading