Class: Langfuse::Trace

Inherits:
Object
  • Object
show all
Extended by:
SpanWrappers
Includes:
PartialUpdates
Defined in:
lib/langfuse/trace.rb

Constant Summary

Constants included from SpanWrappers

SpanWrappers::TYPES

Constants included from PartialUpdates

PartialUpdates::UPDATE_IDENTITY_FIELDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SpanWrappers

define_span_wrappers

Constructor Details

#initialize(client:, id:, name: nil, user_id: nil, session_id: nil, version: nil, release: nil, input: nil, output: nil, metadata: nil, tags: nil, timestamp: nil, public: nil, **kwargs) ⇒ Trace

Returns a new instance of Trace.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/langfuse/trace.rb', line 10

def initialize(client:, id:, name: nil, user_id: nil, session_id: nil, version: nil,
               release: nil, input: nil, output: nil, metadata: nil, tags: nil,
               timestamp: nil, public: nil, **kwargs)
  @client = client
  @id = id
  @name = name
  @user_id = user_id
  @session_id = session_id
  @version = version
  @release = release
  @input = input
  @output = output
  @metadata =  || {}
  @tags = tags || []
  @timestamp = timestamp
  @public = public
  @kwargs = kwargs

  # Create the trace
  create_trace
end

Instance Attribute Details

#client ⇒ Object (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def client
  @client
end

#id ⇒ Object (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def id
  @id
end

#input ⇒ Object (readonly)

Returns the value of attribute input.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def input
  @input
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def 
  @metadata
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def name
  @name
end

#output ⇒ Object (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def output
  @output
end

#public ⇒ Object (readonly)

Returns the value of attribute public.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def public
  @public
end

#release ⇒ Object (readonly)

Returns the value of attribute release.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def release
  @release
end

#session_id ⇒ Object (readonly)

Returns the value of attribute session_id.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def session_id
  @session_id
end

#tags ⇒ Object (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def tags
  @tags
end

#timestamp ⇒ Object (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def timestamp
  @timestamp
end

#user_id ⇒ Object (readonly)

Returns the value of attribute user_id.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def user_id
  @user_id
end

#version ⇒ Object (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/langfuse/trace.rb', line 7

def version
  @version
end

Instance Method Details

#embedding(name: nil, start_time: nil, end_time: nil, input: nil, output: nil, model: nil, usage: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object

Create a child embedding observation



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/langfuse/trace.rb', line 107

def embedding(name: nil, start_time: nil, end_time: nil, input: nil, output: nil,
              model: nil, usage: nil, metadata: nil, level: nil, status_message: nil,
              parent_observation_id: nil, version: nil, **kwargs)
   = ( || {}).merge(
    { model: model, usage: usage }.compact
  )
  span(
    name: name,
    start_time: start_time,
    end_time: end_time,
    input: input,
    output: output,
    metadata: .empty? ? nil : ,
    level: level,
    status_message: status_message,
    parent_observation_id: parent_observation_id,
    version: version,
    as_type: ObservationType::EMBEDDING,
    **kwargs
  )
end

#event(name:, start_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object

Create a child event



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/langfuse/trace.rb', line 83

def event(name:, start_time: nil, input: nil, output: nil, metadata: nil,
          level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs)
  @client.event(
    trace_id: @id,
    name: name,
    start_time: start_time,
    input: input,
    output: output,
    metadata: ,
    level: level,
    status_message: status_message,
    parent_observation_id: parent_observation_id,
    version: version,
    **kwargs
  )
end

#generation(name: nil, start_time: nil, end_time: nil, completion_start_time: nil, model: nil, model_parameters: nil, input: nil, output: nil, usage: nil, usage_details: nil, cost_details: nil, prompt: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, **kwargs) ⇒ Object

Create a child generation



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/langfuse/trace.rb', line 54

def generation(name: nil, start_time: nil, end_time: nil, completion_start_time: nil,
               model: nil, model_parameters: nil, input: nil, output: nil, usage: nil,
               usage_details: nil, cost_details: nil, prompt: nil,
               metadata: nil, level: nil, status_message: nil, parent_observation_id: nil,
               version: nil, **kwargs)
  @client.generation(
    trace_id: @id,
    name: name,
    start_time: start_time,
    end_time: end_time,
    completion_start_time: completion_start_time,
    model: model,
    model_parameters: model_parameters,
    input: input,
    output: output,
    usage: usage,
    usage_details: usage_details,
    cost_details: cost_details,
    prompt: prompt,
    metadata: ,
    level: level,
    status_message: status_message,
    parent_observation_id: parent_observation_id,
    version: version,
    **kwargs
  )
end

#get_url ⇒ Object



167
168
169
# File 'lib/langfuse/trace.rb', line 167

def get_url
  "#{@client.host}/trace/#{@id}"
end

#score(name:, value:, data_type: nil, comment: nil, **kwargs) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/langfuse/trace.rb', line 129

def score(name:, value:, data_type: nil, comment: nil, **kwargs)
  @client.score(
    trace_id: @id,
    name: name,
    value: value,
    data_type: data_type,
    comment: comment,
    **kwargs
  )
end

#span(name: nil, start_time: nil, end_time: nil, input: nil, output: nil, metadata: nil, level: nil, status_message: nil, parent_observation_id: nil, version: nil, as_type: nil, **kwargs) ⇒ Object

Create a child span with optional type



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/langfuse/trace.rb', line 33

def span(name: nil, start_time: nil, end_time: nil, input: nil, output: nil,
         metadata: nil, level: nil, status_message: nil, parent_observation_id: nil,
         version: nil, as_type: nil, **kwargs)
  @client.span(
    trace_id: @id,
    name: name,
    start_time: start_time,
    end_time: end_time,
    input: input,
    output: output,
    metadata: ,
    level: level,
    status_message: status_message,
    parent_observation_id: parent_observation_id,
    version: version,
    as_type: as_type,
    **kwargs
  )
end

#to_dict ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/langfuse/trace.rb', line 171

def to_dict
  {
    id: @id,
    name: @name,
    user_id: @user_id,
    session_id: @session_id,
    version: @version,
    release: @release,
    input: @input,
    output: @output,
    metadata: @metadata,
    tags: @tags,
    timestamp: @timestamp,
    public: @public
  }.merge(@kwargs).compact
end

#update(name: nil, user_id: nil, session_id: nil, version: nil, release: nil, input: nil, output: nil, metadata: nil, tags: nil, public: nil, **kwargs) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/langfuse/trace.rb', line 140

def update(name: nil, user_id: nil, session_id: nil, version: nil,
           release: nil, input: nil, output: nil, metadata: nil, tags: nil,
           public: nil, **kwargs)
  # 更新实例变量
  @name = name unless name.nil?
  @user_id = user_id unless user_id.nil?
  @session_id = session_id unless session_id.nil?
  @version = version unless version.nil?
  @release = release unless release.nil?
  @input = input unless input.nil?
  @output = output unless output.nil?
  @metadata.merge!() if 
  @tags = tags unless tags.nil?
  @public = public unless public.nil?
  @kwargs.merge!(kwargs) if kwargs.any?

  track_changes(
    { name: name, user_id: user_id, session_id: session_id, version: version,
      release: release, input: input, output: output, metadata: ,
      tags: tags, public: public },
    kwargs.keys
  )
  # 触发 trace-update 事件
  update_trace
  self
end