Skip to content

Commit 6cfec4e

Browse files
committed
wip
1 parent 37ff07d commit 6cfec4e

File tree

11 files changed

+105
-49
lines changed

11 files changed

+105
-49
lines changed

sample-app/generate_joke_workflow_example.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ func createJoke(ctx context.Context, workflow *sdk.Workflow, client *openai.Clie
2727
},
2828
}
2929

30-
llmSpan, err := task.LogPrompt(prompt)
31-
if err != nil {
32-
return "", fmt.Errorf("LogPrompt error: %w", err)
33-
}
30+
llmSpan := task.LogPrompt(prompt)
3431

3532
// Make API call
3633
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
@@ -84,13 +81,10 @@ func translateJokeToPirate(ctx context.Context, traceloop *sdk.Traceloop, workfl
8481
},
8582
}
8683

87-
llmSpan := workflow.LogAgent(sdk.AgentAttributes{
88-
Name: "joke_translation",
89-
})
84+
agent := workflow.NewAgent("joke_translation")
9085

91-
llmSpan.LogPrompt(ctx, prompt)
86+
llmSpan := agent.LogPrompt(prompt)
9287

93-
9488
// Make API call
9589
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
9690
Model: "gpt-3.5-turbo",
@@ -125,15 +119,15 @@ func translateJokeToPirate(ctx context.Context, traceloop *sdk.Traceloop, workfl
125119
})
126120

127121
// Call history jokes tool
128-
_, err = historyJokesTool(ctx, traceloop, workflow, client)
122+
_, err = historyJokesTool(ctx, agent, client)
129123
if err != nil {
130124
fmt.Printf("Warning: history_jokes_tool error: %v\n", err)
131125
}
132126

133127
return resp.Choices[0].Message.Content, nil
134128
}
135129

136-
func historyJokesTool(ctx context.Context, traceloop *sdk.Traceloop, workflow *sdk.Workflow, client *openai.Client) (string, error) {
130+
func historyJokesTool(ctx context.Context, agent *sdk.Agent, client *openai.Client) (string, error) {
137131
// Log prompt
138132
prompt := sdk.Prompt{
139133
Vendor: "openai",
@@ -148,12 +142,13 @@ func historyJokesTool(ctx context.Context, traceloop *sdk.Traceloop, workflow *s
148142
},
149143
}
150144

151-
llmSpan := workflow.LogToolCall(sdk.ToolCallAttributes{
152-
Name: "history_jokes",
145+
tool := agent.NewTool("history_jokes", "function", sdk.ToolFunction{
146+
Name: "history_jokes",
147+
Description: "Get some history jokes",
148+
Parameters: map[string]interface{}{},
153149
})
154150

155-
llmSpan.LogPrompt(ctx, prompt)
156-
151+
llmSpan := tool.LogPrompt(prompt)
157152

158153
// Make API call
159154
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
@@ -211,10 +206,7 @@ func generateSignature(ctx context.Context, workflow *sdk.Workflow, client *open
211206
},
212207
}
213208

214-
llmSpan, err := task.LogPrompt(prompt)
215-
if err != nil {
216-
return "", fmt.Errorf("LogPrompt error: %w", err)
217-
}
209+
llmSpan := task.LogPrompt(prompt)
218210

219211
// Make API call
220212
resp, err := client.CreateCompletion(ctx, openai.CompletionRequest{

sample-app/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func workflowExample() {
6262
}
6363

6464
// Log the prompt
65-
llmSpan, err := traceloop.LogPrompt(
65+
llmSpan := traceloop.LogPrompt(
6666
ctx,
6767
sdk.Prompt{
6868
Vendor: "openai",
@@ -104,7 +104,7 @@ func workflowExample() {
104104
}
105105

106106
// Log the completion
107-
llmSpan.LogCompletion(ctx, sdk.Completion{
107+
llmSpan.LogCompletion(ctx, sdk.Completion{
108108
Model: resp.Model,
109109
Messages: completionMsgs,
110110
}, sdk.Usage{

sample-app/tool_calling.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ func runToolCallingExample() {
9191
fmt.Printf("User: %s\n", userPrompt)
9292

9393
// Log the prompt
94-
llmSpan, err := traceloop.LogPrompt(ctx, prompt, workflowAttrs)
95-
if err != nil {
96-
fmt.Printf("Error logging prompt: %v\n", err)
97-
return
98-
}
94+
llmSpan := traceloop.LogPrompt(ctx, prompt, workflowAttrs)
9995

10096
// Make API call to OpenAI
10197
startTime := time.Now()

sample-app/workflow_example.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,14 @@ func workflowMain() {
4747
})
4848
}
4949

50-
llmSpan, err := factGenTask.LogPrompt(
50+
llmSpan := factGenTask.LogPrompt(
5151
tlp.Prompt{
5252
Vendor: "openai",
5353
Mode: "chat",
5454
Model: request.Model,
5555
Messages: promptMsgs,
5656
},
5757
)
58-
if err != nil {
59-
fmt.Printf("LogPrompt error: %v\n", err)
60-
return
61-
}
6258

6359
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
6460
resp, err := client.CreateChatCompletion(
@@ -91,7 +87,7 @@ func workflowMain() {
9187
someOtherTask := wf.NewTask("some_other_task")
9288
defer someOtherTask.End()
9389

94-
otherPrompt, _ := someOtherTask.LogPrompt(tlp.Prompt{
90+
otherPrompt := someOtherTask.LogPrompt(tlp.Prompt{
9591
Vendor: "openai",
9692
Mode: "chat",
9793
Model: request.Model,

traceloop-sdk/agent.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package traceloop
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
semconvai "github.com/traceloop/go-openllmetry/semconv-ai"
8+
"go.opentelemetry.io/otel/trace"
9+
"github.com/traceloop/go-openllmetry/traceloop-sdk/model"
10+
)
11+
12+
type Agent struct {
13+
sdk *Traceloop
14+
workflow *Workflow
15+
ctx context.Context
16+
Name string `json:"name"`
17+
}
18+
19+
func (agent *Agent) End() {
20+
trace.SpanFromContext(agent.ctx).End()
21+
}
22+
23+
func (agent *Agent) LogPrompt(prompt Prompt) LLMSpan {
24+
if agent.workflow != nil {
25+
return agent.workflow.LogPrompt(prompt)
26+
}
27+
return agent.sdk.LogPrompt(agent.ctx, prompt, WorkflowAttributes{})
28+
}
29+
30+
func (agent *Agent) NewTool(name string, toolType string, toolFunction ToolFunction) *Tool {
31+
toolCtx, span := agent.sdk.getTracer().Start(agent.ctx, fmt.Sprintf("%s.tool", name))
32+
span.SetAttributes(
33+
semconvai.LLMAgentName.String(agent.Name),
34+
semconvai.TraceloopSpanKind.String(string(model.SpanKindTool)),
35+
semconvai.TraceloopEntityName.String(name),
36+
)
37+
38+
return &Tool{
39+
agent: agent,
40+
ctx: toolCtx,
41+
Name: name,
42+
Type: toolType,
43+
Function: toolFunction,
44+
}
45+
}
46+
47+

traceloop-sdk/sdk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (instance *Traceloop) getTracer() apitrace.Tracer {
147147
}
148148

149149
// New workflow-based API
150-
func (instance *Traceloop) LogPrompt(ctx context.Context, prompt Prompt, workflowAttrs WorkflowAttributes) (LLMSpan, error) {
150+
func (instance *Traceloop) LogPrompt(ctx context.Context, prompt Prompt, workflowAttrs WorkflowAttributes) LLMSpan {
151151
spanName := fmt.Sprintf("%s.%s", prompt.Vendor, prompt.Mode)
152152
_, span := instance.getTracer().Start(ctx, spanName)
153153

@@ -169,7 +169,7 @@ func (instance *Traceloop) LogPrompt(ctx context.Context, prompt Prompt, workflo
169169

170170
return LLMSpan{
171171
span: span,
172-
}, nil
172+
}
173173
}
174174

175175
// LogToolCall logs a tool call with the specified name

traceloop-sdk/sdk_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ func TestLogPromptSpanAttributes(t *testing.T) {
7070
}
7171

7272
// Log the prompt using new workflow API
73-
llmSpan, err := tl.LogPrompt(context.Background(), prompt, workflowAttrs)
74-
if err != nil {
75-
t.Fatalf("LogPrompt failed: %v", err)
76-
}
73+
llmSpan := tl.LogPrompt(context.Background(), prompt, workflowAttrs)
7774

7875
// Log completion with tool calls
7976
completion := Completion{

traceloop-sdk/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (task *Task) End() {
1616
trace.SpanFromContext(task.ctx).End()
1717
}
1818

19-
func (task *Task) LogPrompt(prompt Prompt) (LLMSpan, error) {
19+
func (task *Task) LogPrompt(prompt Prompt) LLMSpan {
2020
return task.workflow.sdk.LogPrompt(task.ctx, prompt, task.workflow.Attributes)
2121
}
2222

traceloop-sdk/tool.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package traceloop
2+
3+
import (
4+
"context"
5+
6+
"go.opentelemetry.io/otel/trace"
7+
)
8+
9+
type Tool struct {
10+
agent *Agent
11+
ctx context.Context
12+
Name string `json:"name"`
13+
Type string `json:"type"`
14+
Function ToolFunction `json:"function,omitempty"`
15+
}
16+
17+
func (tool *Tool) End() {
18+
trace.SpanFromContext(tool.ctx).End()
19+
}
20+
21+
func (tool *Tool) LogPrompt(prompt Prompt) LLMSpan {
22+
return tool.agent.LogPrompt(prompt)
23+
}

traceloop-sdk/tracing_types.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ type ToolFunction struct {
4242
Parameters interface{} `json:"parameters"`
4343
}
4444

45-
type Tool struct {
46-
Type string `json:"type"`
47-
Function ToolFunction `json:"function,omitempty"`
48-
}
4945

5046
type ToolCall struct {
5147
ID string `json:"id"`

0 commit comments

Comments
 (0)