|
| 1 | +package langwatch |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/0xdeafcafe/bloefish/libraries/ptr" |
| 8 | +) |
| 9 | + |
| 10 | +type SpanLLM interface { |
| 11 | + AddOutput(ctx context.Context, output string) |
| 12 | + AddError(ctx context.Context, err error) |
| 13 | + End(ctx context.Context) |
| 14 | +} |
| 15 | + |
| 16 | +type LLMSpanInputChatMessage struct { |
| 17 | + Role string `json:"role"` |
| 18 | + Message string `json:"message"` |
| 19 | +} |
| 20 | + |
| 21 | +type LLMSpanOutputChatMessage struct { |
| 22 | + Role string `json:"role"` |
| 23 | + Message string `json:"message"` |
| 24 | + FunctionCall interface{} `json:"function_call"` |
| 25 | + ToolCalls []interface{} `json:"tool_calls"` |
| 26 | +} |
| 27 | + |
| 28 | +type StartLLMSpanParams struct { |
| 29 | + Vendor string |
| 30 | + Model string |
| 31 | + Name *string |
| 32 | + Input StartLLMSpanChatMessageInput |
| 33 | +} |
| 34 | + |
| 35 | +type StartLLMSpanChatMessageInput struct { |
| 36 | + Messages []LLMSpanInputChatMessage |
| 37 | +} |
| 38 | + |
| 39 | +type spanLLM struct { |
| 40 | + client *client |
| 41 | + trace Trace |
| 42 | + spanID string |
| 43 | + name *string |
| 44 | + vendor *string |
| 45 | + model *string |
| 46 | + input *spanLLMInput |
| 47 | + output *spanLLMOutput |
| 48 | + error error |
| 49 | + timestamps *spanLLMTimestamps |
| 50 | + metrics *spanLLMMetrics |
| 51 | +} |
| 52 | + |
| 53 | +type spanLLMInput struct { |
| 54 | + Type string `json:"type"` |
| 55 | + Value interface{} `json:"value"` |
| 56 | +} |
| 57 | +type spanLLMOutput struct { |
| 58 | + Type string `json:"type"` |
| 59 | + Value interface{} `json:"value"` |
| 60 | +} |
| 61 | +type spanLLMParams struct { |
| 62 | + Temperature *float64 `json:"temperature,omitempty"` |
| 63 | + Stream *bool `json:"stream,omitempty"` |
| 64 | +} |
| 65 | +type spanLLMTimestamps struct { |
| 66 | + StartedAt *int64 `json:"started_at,omitempty"` |
| 67 | + FinishedAt *int64 `json:"finished_at,omitempty"` |
| 68 | +} |
| 69 | +type spanLLMMetrics struct { |
| 70 | + PromptTokens *int `json:"prompt_tokens,omitempty"` |
| 71 | + CompletionTokens *int `json:"completion_tokens,omitempty"` |
| 72 | +} |
| 73 | + |
| 74 | +type spanCollectorRequest struct { |
| 75 | + TraceID string `json:"trace_id"` |
| 76 | + Spans []*spanCollectorRequestSpan `json:"spans"` |
| 77 | + Metadata map[string]string `json:"metadata"` |
| 78 | +} |
| 79 | +type spanCollectorRequestSpan struct { |
| 80 | + Type string |
| 81 | + SpanID string |
| 82 | + Vendor string |
| 83 | + Model string |
| 84 | + Input interface{} |
| 85 | + Output interface{} |
| 86 | + Params interface{} |
| 87 | + Metrics interface{} |
| 88 | + Timestamps interface{} |
| 89 | +} |
| 90 | +type spanCollectorRequestMetadata struct { |
| 91 | +} |
| 92 | + |
| 93 | +func (t *trace) StartLLMSpan(ctx context.Context, spanID string, params StartLLMSpanParams) (context.Context, SpanLLM) { |
| 94 | + return ctx, &spanLLM{ |
| 95 | + client: t.client, |
| 96 | + trace: t, |
| 97 | + |
| 98 | + spanID: spanID, |
| 99 | + vendor: ¶ms.Vendor, |
| 100 | + model: ¶ms.Model, |
| 101 | + |
| 102 | + input: &spanLLMInput{ |
| 103 | + Type: "chat_messages", |
| 104 | + Value: params.Input.Messages, |
| 105 | + }, |
| 106 | + |
| 107 | + timestamps: &spanLLMTimestamps{ |
| 108 | + StartedAt: ptr.P(time.Now().UnixNano()), |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func (s *spanLLM) AddOutput(ctx context.Context, output string) { |
| 114 | + s.output = &spanLLMOutput{ |
| 115 | + Type: "chat_messages", |
| 116 | + Value: []LLMSpanOutputChatMessage{{ |
| 117 | + Role: "bot", |
| 118 | + Message: output, |
| 119 | + FunctionCall: nil, |
| 120 | + ToolCalls: []interface{}{}, |
| 121 | + }}, |
| 122 | + } |
| 123 | + s.timestamps.FinishedAt = ptr.P(time.Now().UnixNano()) |
| 124 | +} |
| 125 | + |
| 126 | +func (s *spanLLM) AddError(ctx context.Context, err error) { |
| 127 | + s.error = err |
| 128 | +} |
| 129 | + |
| 130 | +func (s *spanLLM) End(ctx context.Context) { |
| 131 | + s.client.Collect(ctx) |
| 132 | +} |
0 commit comments