Skip to content

Commit e243259

Browse files
committed
otel tools and tool_calls
1 parent fc956b2 commit e243259

4 files changed

Lines changed: 80 additions & 5 deletions

File tree

backend/modules/observability/lib/otel/consts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ const (
9494
otelAttributeToolsPrefix = "gen_ai.request.functions" // tools
9595
)
9696

97+
// otel attribute key
98+
const (
99+
otelAttributeModelInputTools = "gen_ai.tool.definitions"
100+
)
101+
97102
var otelMessageEventNameMap = []string{
98103
otelEventModelSystemMessage,
99104
otelEventModelUserMessage,

backend/modules/observability/lib/otel/open_inference/openinference.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,17 @@ func convertModelMsg(msg map[string]interface{}) map[string]interface{} {
115115
// get tool_call
116116
toolCall, ok := tc["tool_call"].(map[string]interface{})
117117
if !ok {
118-
continue
118+
toolCall = tc // maybe no tool_call key, it has been a raw tool_call
119119
}
120120
// get function from tool_call
121121
function, ok := toolCall["function"].(map[string]interface{})
122122
if !ok {
123123
continue
124124
}
125-
125+
id, _ := toolCall["id"]
126126
modelCall := map[string]interface{}{
127127
"type": "function",
128+
"id": id,
128129
"function": map[string]interface{}{
129130
"name": function["name"],
130131
},
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2026 coze-dev Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package open_telemetry
5+
6+
import "fmt"
7+
8+
func AddTools2ModelInput(input interface{}, tools interface{}) (interface{}, error) {
9+
modelInput, ok := input.(map[string]interface{})
10+
if !ok {
11+
return nil, fmt.Errorf("input is not a map")
12+
}
13+
14+
toolsSlice, ok := tools.([]interface{})
15+
if !ok {
16+
return modelInput, nil
17+
}
18+
19+
modelTools := make([]interface{}, 0, len(toolsSlice))
20+
for _, tool := range toolsSlice {
21+
toolMap, ok := tool.(map[string]interface{})
22+
if !ok {
23+
continue
24+
}
25+
26+
function, ok := toolMap["function"].(map[string]interface{})
27+
if !ok {
28+
function = toolMap // maybe no function key, it has been a raw function
29+
}
30+
31+
name, _ := function["name"]
32+
if name == nil {
33+
name = ""
34+
}
35+
description, _ := function["description"]
36+
if description == nil {
37+
description = ""
38+
}
39+
parameters, _ := function["parameters"]
40+
if parameters == nil {
41+
parameters = "{}"
42+
}
43+
modelTool := map[string]interface{}{
44+
"type": "function",
45+
"function": map[string]interface{}{
46+
"name": name,
47+
"description": description,
48+
"parameters": parameters,
49+
},
50+
}
51+
52+
modelTools = append(modelTools, modelTool)
53+
}
54+
55+
if len(modelTools) > 0 {
56+
modelInput["tools"] = modelTools
57+
}
58+
59+
return modelInput, nil
60+
}

backend/modules/observability/lib/otel/otel_convert.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/bytedance/sonic"
1515
"github.com/coze-dev/coze-loop/backend/modules/observability/lib/otel/litellm"
1616
"github.com/coze-dev/coze-loop/backend/modules/observability/lib/otel/open_inference"
17+
"github.com/coze-dev/coze-loop/backend/modules/observability/lib/otel/open_telemetry"
1718
"github.com/coze-dev/cozeloop-go/spec/tracespec"
1819
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
1920

@@ -673,9 +674,17 @@ func processAttributePrefix(ctx context.Context, fieldKey string, conf FieldConf
673674
}
674675
// pack tools
675676
srcTools := aggregateAttributesByPrefix(attributeMap, openInferenceAttributeModelInputTools)
676-
toBeMarshalObject, err = open_inference.AddTools2ModelInput(srcInput, srcTools)
677-
if err != nil {
678-
continue
677+
if srcTools != nil { // openInference tools
678+
toBeMarshalObject, err = open_inference.AddTools2ModelInput(srcInput, srcTools)
679+
if err != nil {
680+
continue
681+
}
682+
} else { // otel tools
683+
srcTools = aggregateAttributesByPrefix(attributeMap, otelAttributeModelInputTools)
684+
toBeMarshalObject, err = open_telemetry.AddTools2ModelInput(srcInput, srcTools)
685+
if err != nil {
686+
continue
687+
}
679688
}
680689
case openInferenceAttributeModelOutputMessages: // openInference output message
681690
resObject, err := open_inference.ConvertToModelOutput(srcAttrAggrRes)

0 commit comments

Comments
 (0)