_Trace
Internal class for creating and managing traces in the NeuralTrust system. This class captures details about interactions or events, including metadata, timing information, and content.
Constructor
_Trace(
client: TraceClient,
trace_id: Optional[str] = None,
interaction_id: Optional[str] = None,
conversation_id: Optional[str] = None,
channel_id: Optional[str] = None,
session_id: Optional[str] = None,
user: Optional[User] = None,
metadata: Optional[Metadata] = None,
custom: Optional[dict] = None
)
Parameters:
client
(TraceClient): The TraceClient instance used to send tracestrace_id
(Optional[str]): Unique identifier for this specific traceinteraction_id
(Optional[str]): ID that can be shared across multiple related tracesconversation_id
(Optional[str]): Unique identifier for the conversationchannel_id
(Optional[str]): Optional identifier for the communication channelsession_id
(Optional[str]): Optional identifier for the user sessionuser
(Optional[User]): Optional User object containing user informationmetadata
(Optional[Metadata]): Optional metadata dictionarycustom
(Optional[dict]): Optional custom data dictionary
Methods
message()
def message(input: str) -> "_Trace"
Start a message trace.
Parameters:
input
(str): The input message to trace
Returns:
_Trace
: A new Trace object initialized with the message event
tool()
def tool(input: str, parent_id: Optional[str] = None) -> "_Trace"
Start a tool trace.
Parameters:
input
(str): The input for the tool eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized with the tool event
agent()
def agent(input: str, parent_id: Optional[str] = None) -> "_Trace"
Start an agent trace.
Parameters:
input
(str): The input for the agent eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized with the agent event
retrieval()
def retrieval(input: str, parent_id: Optional[str] = None) -> "_Trace"
Start a retrieval trace.
Parameters:
input
(str): The input for the retrieval eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized with the retrieval event
generation()
def generation(input: str, parent_id: Optional[str] = None) -> "_Trace"
Start a generation trace.
Parameters:
input
(str): The input for the generation eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized with the generation event
router()
def router(input: str, parent_id: Optional[str] = None) -> "_Trace"
Start and automatically end a router trace.
Parameters:
input
(str): The input for the router eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized and completed with the router event
event()
def event(input: str, parent_id: Optional[str] = None) -> "_Trace"
Record a custom event trace.
Parameters:
input
(str): The input for the custom eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized and completed with the custom event
system()
def system(prompt: Union[str, object], parent_id: Optional[str] = None) -> "_Trace"
Record a system trace.
Parameters:
prompt
(Union[str, object]): The input for the system eventparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized and completed with the system event
feedback()
def feedback(
feedback_tag: FeedbackTag,
feedback_text: str,
parent_id: Optional[str] = None
) -> "_Trace"
Record a feedback trace.
Parameters:
feedback_tag
(FeedbackTag): The type/category of feedback being providedfeedback_text
(str): The actual feedback contentparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: A new Trace object initialized and completed with the feedback event
end()
def end(output: Union[str, object]) -> "_Trace"
End the current trace and record its output.
Parameters:
output
(Union[str, object]): The output/result of the trace
Returns:
_Trace
: The current Trace object with completed trace data
send()
def send(
event_type: EventType,
input: str,
output: Optional[str] = None,
latency: Optional[int] = None,
start_timestamp: Optional[int] = None,
end_timestamp: Optional[int] = None,
parent_id: Optional[str] = None
) -> "_Trace"
Send an atomic event to NeuralTrust.
Parameters:
event_type
(EventType): The type of event being recordedinput
(str): The input data for the eventoutput
(Optional[str]): Optional output data for the eventlatency
(Optional[int]): Optional processing time in millisecondsstart_timestamp
(Optional[int]): Optional event start time in millisecondsend_timestamp
(Optional[int]): Optional event end time in millisecondsparent_id
(Optional[str]): The ID of the parent trace
Returns:
_Trace
: The current Trace object with the recorded event data
Usage Example
# Create a trace
trace = client.trace(conversation_id="conv123")
# Record a message event
message_trace = trace.message("User input")
response = message_trace.end("Bot response")
# Record a tool event
tool_trace = trace.tool("Processing data", parent_id=message_trace.trace_id)
result = tool_trace.end("Data processed")
# Send feedback
trace.feedback(FeedbackTag.POSITIVE, "Great response!", parent_id=message_trace.trace_id)