Skip to content

Commit ad406ac

Browse files
committed
split task
1 parent cb234b1 commit ad406ac

File tree

5 files changed

+72
-34
lines changed

5 files changed

+72
-34
lines changed

sample-app/generate_joke_workflow_example.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ func translateJokeToPirate(ctx context.Context, traceloop *sdk.Traceloop, workfl
8484
},
8585
}
8686

87-
llmSpan, err := traceloop.LogAgent(ctx, sdk.AgentAttributes{
87+
llmSpan, err := workflow.LogAgent(sdk.AgentAttributes{
8888
Name: "joke_translation",
89-
}, prompt, workflow.Attributes)
89+
})
90+
if err != nil {
91+
return "", fmt.Errorf("LogPrompt error: %w", err)
92+
}
93+
94+
llmSpan, err = workflow.LogPrompt(prompt)
9095
if err != nil {
9196
return "", fmt.Errorf("LogPrompt error: %w", err)
9297
}
@@ -148,12 +153,17 @@ func historyJokesTool(ctx context.Context, traceloop *sdk.Traceloop, workflow *s
148153
},
149154
}
150155

151-
llmSpan, err := traceloop.LogToolCall(ctx, sdk.ToolCallAttributes{
152-
Name: "history_jokes",
153-
}, prompt, workflow.Attributes)
156+
llmSpan, err := workflow.LogPrompt(prompt)
154157
if err != nil {
155158
return "", fmt.Errorf("LogPrompt error: %w", err)
156159
}
160+
161+
_, err = workflow.LogToolCall(sdk.ToolCallAttributes{
162+
Name: "history_jokes",
163+
})
164+
if err != nil {
165+
return "", fmt.Errorf("LogToolCall error: %w", err)
166+
}
157167

158168
// Make API call
159169
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
@@ -257,7 +267,6 @@ func runJokeWorkflow() {
257267
}
258268
defer func() { traceloop.Shutdown(ctx) }()
259269

260-
// Create OpenAI client
261270
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
262271

263272
// Create workflow

traceloop-sdk/model/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package model
2+
3+
type SpanKind string
4+
5+
const (
6+
SpanKindTool SpanKind = "tool"
7+
SpanKindAgent SpanKind = "agent"
8+
SpanKindTask SpanKind = "task"
9+
SpanKindWorkflow SpanKind = "workflow"
10+
)

traceloop-sdk/sdk.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121

2222
const PromptsPath = "/v1/traceloop/prompts"
2323

24+
const (
25+
ToolType = "tool"
26+
AgentType = "agent"
27+
)
28+
2429
type Traceloop struct {
2530
config Config
2631
promptRegistry model.PromptRegistry
@@ -184,17 +189,14 @@ func (llmSpan *LLMSpan) LogCompletion(ctx context.Context, completion Completion
184189
return nil
185190
}
186191

187-
// LogToolCall logs a tool call with the specified name and prompt
188-
func (instance *Traceloop) LogToolCall(ctx context.Context, attrs ToolCallAttributes, prompt Prompt, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
192+
// LogToolCall logs a tool call with the specified name
193+
func (instance *Traceloop) LogToolCall(ctx context.Context, attrs ToolCallAttributes, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
189194
spanName := fmt.Sprintf("%s.tool", attrs.Name)
190195
_, span := instance.getTracer().Start(ctx, spanName)
191196

192197
spanAttrs := []attribute.KeyValue{
193-
semconvai.LLMVendor.String(prompt.Vendor),
194-
semconvai.LLMRequestModel.String(prompt.Model),
195-
semconvai.LLMRequestType.String(prompt.Mode),
196198
semconvai.TraceloopWorkflowName.String(workflowAttrs.Name),
197-
semconvai.TraceloopSpanKind.String("tool"),
199+
semconvai.TraceloopSpanKind.String(ToolType),
198200
semconvai.TraceloopEntityName.String(attrs.Name),
199201
}
200202

@@ -204,25 +206,20 @@ func (instance *Traceloop) LogToolCall(ctx context.Context, attrs ToolCallAttrib
204206
}
205207

206208
span.SetAttributes(spanAttrs...)
207-
setMessagesAttribute(span, "llm.prompts", prompt.Messages)
208-
setToolsAttribute(span, prompt.Tools)
209209

210210
return LLMSpan{
211211
span: span,
212212
}, nil
213213
}
214214

215-
// LogAgent logs an agent with the specified name and prompt
216-
func (instance *Traceloop) LogAgent(ctx context.Context, attrs AgentAttributes, prompt Prompt, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
215+
// LogAgent logs an agent with the specified name
216+
func (instance *Traceloop) LogAgent(ctx context.Context, attrs AgentAttributes, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
217217
spanName := fmt.Sprintf("%s.agent", attrs.Name)
218218
_, span := instance.getTracer().Start(ctx, spanName)
219219

220220
spanAttrs := []attribute.KeyValue{
221-
semconvai.LLMVendor.String(prompt.Vendor),
222-
semconvai.LLMRequestModel.String(prompt.Model),
223-
semconvai.LLMRequestType.String(prompt.Mode),
224221
semconvai.TraceloopWorkflowName.String(workflowAttrs.Name),
225-
semconvai.TraceloopSpanKind.String("agent"),
222+
semconvai.TraceloopSpanKind.String(AgentType),
226223
semconvai.LLMAgentName.String(attrs.Name),
227224
}
228225

@@ -232,8 +229,6 @@ func (instance *Traceloop) LogAgent(ctx context.Context, attrs AgentAttributes,
232229
}
233230

234231
span.SetAttributes(spanAttrs...)
235-
setMessagesAttribute(span, "llm.prompts", prompt.Messages)
236-
setToolsAttribute(span, prompt.Tools)
237232

238233
return LLMSpan{
239234
span: span,

traceloop-sdk/task.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package traceloop
2+
3+
import (
4+
"context"
5+
6+
"go.opentelemetry.io/otel/trace"
7+
)
8+
9+
type Task struct {
10+
workflow *Workflow
11+
ctx context.Context
12+
Name string `json:"name"`
13+
}
14+
15+
func (task *Task) End() {
16+
trace.SpanFromContext(task.ctx).End()
17+
}
18+
19+
func (task *Task) LogPrompt(prompt Prompt) (LLMSpan, error) {
20+
return task.workflow.sdk.LogPrompt(task.ctx, prompt, task.workflow.Attributes)
21+
}
22+
23+
func (task *Task) LogAgent(attrs AgentAttributes) (LLMSpan, error) {
24+
return task.workflow.sdk.LogAgent(task.ctx, attrs, task.workflow.Attributes)
25+
}
26+
27+
func (task *Task) LogToolCall(attrs ToolCallAttributes) (LLMSpan, error) {
28+
return task.workflow.sdk.LogToolCall(task.ctx, attrs, task.workflow.Attributes)
29+
}

traceloop-sdk/workflow.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/traceloop/go-openllmetry/traceloop-sdk/model"
78
semconvai "github.com/traceloop/go-openllmetry/semconv-ai"
89
"go.opentelemetry.io/otel/trace"
910
)
@@ -14,18 +15,12 @@ type Workflow struct {
1415
Attributes WorkflowAttributes `json:"workflow_attributes"`
1516
}
1617

17-
type Task struct {
18-
workflow *Workflow
19-
ctx context.Context
20-
Name string `json:"name"`
21-
}
22-
2318
func (instance *Traceloop) NewWorkflow(ctx context.Context, attrs WorkflowAttributes) *Workflow {
2419
wCtx, span := instance.getTracer().Start(ctx, fmt.Sprintf("%s.workflow", attrs.Name), trace.WithNewRoot())
2520

2621
span.SetAttributes(
2722
semconvai.TraceloopWorkflowName.String(attrs.Name),
28-
semconvai.TraceloopSpanKind.String("workflow"),
23+
semconvai.TraceloopSpanKind.String(string(model.SpanKindWorkflow)),
2924
semconvai.TraceloopEntityName.String(attrs.Name),
3025
)
3126

@@ -49,7 +44,7 @@ func (workflow *Workflow) NewTask(name string) *Task {
4944

5045
span.SetAttributes(
5146
semconvai.TraceloopWorkflowName.String(workflow.Attributes.Name),
52-
semconvai.TraceloopSpanKind.String("task"),
47+
semconvai.TraceloopSpanKind.String(string(model.SpanKindTask)),
5348
semconvai.TraceloopEntityName.String(name),
5449
)
5550

@@ -60,10 +55,10 @@ func (workflow *Workflow) NewTask(name string) *Task {
6055
}
6156
}
6257

63-
func (task *Task) End() {
64-
trace.SpanFromContext(task.ctx).End()
58+
func (workflow *Workflow) LogAgent(attrs AgentAttributes) (LLMSpan, error) {
59+
return workflow.sdk.LogAgent(workflow.ctx, attrs, workflow.Attributes)
6560
}
6661

67-
func (task *Task) LogPrompt(prompt Prompt) (LLMSpan, error) {
68-
return task.workflow.sdk.LogPrompt(task.ctx, prompt, task.workflow.Attributes)
62+
func (workflow *Workflow) LogToolCall(attrs ToolCallAttributes) (LLMSpan, error) {
63+
return workflow.sdk.LogToolCall(workflow.ctx, attrs, workflow.Attributes)
6964
}

0 commit comments

Comments
 (0)