Skip to content

Commit 37ff07d

Browse files
committed
divide
1 parent ad406ac commit 37ff07d

File tree

8 files changed

+53
-56
lines changed

8 files changed

+53
-56
lines changed

sample-app/generate_joke_workflow_example.go

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

87-
llmSpan, err := workflow.LogAgent(sdk.AgentAttributes{
87+
llmSpan := workflow.LogAgent(sdk.AgentAttributes{
8888
Name: "joke_translation",
8989
})
90-
if err != nil {
91-
return "", fmt.Errorf("LogPrompt error: %w", err)
92-
}
9390

94-
llmSpan, err = workflow.LogPrompt(prompt)
95-
if err != nil {
96-
return "", fmt.Errorf("LogPrompt error: %w", err)
97-
}
91+
llmSpan.LogPrompt(ctx, prompt)
92+
9893

9994
// Make API call
10095
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
@@ -152,18 +147,13 @@ func historyJokesTool(ctx context.Context, traceloop *sdk.Traceloop, workflow *s
152147
},
153148
},
154149
}
155-
156-
llmSpan, err := workflow.LogPrompt(prompt)
157-
if err != nil {
158-
return "", fmt.Errorf("LogPrompt error: %w", err)
159-
}
160150

161-
_, err = workflow.LogToolCall(sdk.ToolCallAttributes{
151+
llmSpan := workflow.LogToolCall(sdk.ToolCallAttributes{
162152
Name: "history_jokes",
163153
})
164-
if err != nil {
165-
return "", fmt.Errorf("LogToolCall error: %w", err)
166-
}
154+
155+
llmSpan.LogPrompt(ctx, prompt)
156+
167157

168158
// Make API call
169159
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{

sample-app/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,14 @@ func workflowExample() {
104104
}
105105

106106
// Log the completion
107-
err = llmSpan.LogCompletion(ctx, sdk.Completion{
107+
llmSpan.LogCompletion(ctx, sdk.Completion{
108108
Model: resp.Model,
109109
Messages: completionMsgs,
110110
}, sdk.Usage{
111111
TotalTokens: resp.Usage.TotalTokens,
112112
CompletionTokens: resp.Usage.CompletionTokens,
113113
PromptTokens: resp.Usage.PromptTokens,
114114
})
115-
if err != nil {
116-
fmt.Printf("LogCompletion error: %v\n", err)
117-
return
118-
}
119115

120116
fmt.Println(resp.Choices[0].Message.Content)
121117
}

sample-app/tool_calling.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,7 @@ func runToolCallingExample() {
175175
PromptTokens: int(resp.Usage.PromptTokens),
176176
}
177177

178-
err = llmSpan.LogCompletion(ctx, completion, usage)
179-
if err != nil {
180-
fmt.Printf("Error logging completion: %v\n", err)
181-
return
182-
}
178+
llmSpan.LogCompletion(ctx, completion, usage)
183179

184180
// If tool calls were made, execute them
185181
if len(resp.Choices[0].Message.ToolCalls) > 0 {

traceloop-sdk/llm_span.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package traceloop
2+
3+
import (
4+
"context"
5+
6+
semconvai "github.com/traceloop/go-openllmetry/semconv-ai"
7+
apitrace "go.opentelemetry.io/otel/trace"
8+
)
9+
10+
type LLMSpan struct {
11+
span apitrace.Span
12+
}
13+
14+
func (llmSpan *LLMSpan) LogPrompt(ctx context.Context, prompt Prompt) {
15+
llmSpan.span.SetAttributes(
16+
semconvai.LLMRequestModel.String(prompt.Model),
17+
semconvai.LLMRequestType.String(prompt.Mode),
18+
)
19+
20+
setMessagesAttribute(llmSpan.span, "llm.prompts", prompt.Messages)
21+
}
22+
23+
func (llmSpan *LLMSpan) LogCompletion(ctx context.Context, completion Completion, usage Usage) {
24+
llmSpan.span.SetAttributes(
25+
semconvai.LLMResponseModel.String(completion.Model),
26+
semconvai.LLMUsageTotalTokens.Int(usage.TotalTokens),
27+
semconvai.LLMUsageCompletionTokens.Int(usage.CompletionTokens),
28+
semconvai.LLMUsagePromptTokens.Int(usage.PromptTokens),
29+
)
30+
31+
setMessagesAttribute(llmSpan.span, "llm.completions", completion.Messages)
32+
33+
defer llmSpan.span.End()
34+
}

traceloop-sdk/sdk.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ type Traceloop struct {
3434
http.Client
3535
}
3636

37-
type LLMSpan struct {
38-
span apitrace.Span
39-
}
4037

4138
func NewClient(ctx context.Context, config Config) (*Traceloop, error) {
4239
instance := Traceloop{
@@ -175,22 +172,8 @@ func (instance *Traceloop) LogPrompt(ctx context.Context, prompt Prompt, workflo
175172
}, nil
176173
}
177174

178-
func (llmSpan *LLMSpan) LogCompletion(ctx context.Context, completion Completion, usage Usage) error {
179-
llmSpan.span.SetAttributes(
180-
semconvai.LLMResponseModel.String(completion.Model),
181-
semconvai.LLMUsageTotalTokens.Int(usage.TotalTokens),
182-
semconvai.LLMUsageCompletionTokens.Int(usage.CompletionTokens),
183-
semconvai.LLMUsagePromptTokens.Int(usage.PromptTokens),
184-
)
185-
186-
setMessagesAttribute(llmSpan.span, "llm.completions", completion.Messages)
187-
188-
defer llmSpan.span.End()
189-
return nil
190-
}
191-
192175
// LogToolCall logs a tool call with the specified name
193-
func (instance *Traceloop) LogToolCall(ctx context.Context, attrs ToolCallAttributes, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
176+
func (instance *Traceloop) LogToolCall(ctx context.Context, attrs ToolCallAttributes, workflowAttrs WorkflowAttributes) LLMSpan {
194177
spanName := fmt.Sprintf("%s.tool", attrs.Name)
195178
_, span := instance.getTracer().Start(ctx, spanName)
196179

@@ -209,11 +192,11 @@ func (instance *Traceloop) LogToolCall(ctx context.Context, attrs ToolCallAttrib
209192

210193
return LLMSpan{
211194
span: span,
212-
}, nil
195+
}
213196
}
214197

215198
// LogAgent logs an agent with the specified name
216-
func (instance *Traceloop) LogAgent(ctx context.Context, attrs AgentAttributes, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
199+
func (instance *Traceloop) LogAgent(ctx context.Context, attrs AgentAttributes, workflowAttrs WorkflowAttributes) LLMSpan {
217200
spanName := fmt.Sprintf("%s.agent", attrs.Name)
218201
_, span := instance.getTracer().Start(ctx, spanName)
219202

@@ -232,7 +215,7 @@ func (instance *Traceloop) LogAgent(ctx context.Context, attrs AgentAttributes,
232215

233216
return LLMSpan{
234217
span: span,
235-
}, nil
218+
}
236219
}
237220

238221
func (instance *Traceloop) Shutdown(ctx context.Context) {

traceloop-sdk/sdk_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ func TestLogPromptSpanAttributes(t *testing.T) {
103103
PromptTokens: 82,
104104
}
105105

106-
err = llmSpan.LogCompletion(context.Background(), completion, usage)
107-
if err != nil {
108-
t.Fatalf("LogCompletion failed: %v", err)
109-
}
106+
llmSpan.LogCompletion(context.Background(), completion, usage)
107+
110108

111109
// Get the recorded spans
112110
spans := exporter.GetSpans()

traceloop-sdk/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func (task *Task) LogPrompt(prompt Prompt) (LLMSpan, error) {
2020
return task.workflow.sdk.LogPrompt(task.ctx, prompt, task.workflow.Attributes)
2121
}
2222

23-
func (task *Task) LogAgent(attrs AgentAttributes) (LLMSpan, error) {
23+
func (task *Task) LogAgent(attrs AgentAttributes) LLMSpan {
2424
return task.workflow.sdk.LogAgent(task.ctx, attrs, task.workflow.Attributes)
2525
}
2626

27-
func (task *Task) LogToolCall(attrs ToolCallAttributes) (LLMSpan, error) {
27+
func (task *Task) LogToolCall(attrs ToolCallAttributes) LLMSpan {
2828
return task.workflow.sdk.LogToolCall(task.ctx, attrs, task.workflow.Attributes)
2929
}

traceloop-sdk/workflow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ func (workflow *Workflow) NewTask(name string) *Task {
5555
}
5656
}
5757

58-
func (workflow *Workflow) LogAgent(attrs AgentAttributes) (LLMSpan, error) {
58+
func (workflow *Workflow) LogAgent(attrs AgentAttributes) LLMSpan {
5959
return workflow.sdk.LogAgent(workflow.ctx, attrs, workflow.Attributes)
6060
}
6161

62-
func (workflow *Workflow) LogToolCall(attrs ToolCallAttributes) (LLMSpan, error) {
62+
func (workflow *Workflow) LogToolCall(attrs ToolCallAttributes) LLMSpan {
6363
return workflow.sdk.LogToolCall(workflow.ctx, attrs, workflow.Attributes)
6464
}

0 commit comments

Comments
 (0)