Skip to content

Commit 15a092e

Browse files
authored
[feat] otel support opentelemetry msg parts and tool_call (#438)
* support opentelemetry msg parts tool_call
1 parent d773c66 commit 15a092e

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import (
1111
type Literal string
1212

1313
const (
14-
TextLiteral Literal = "text"
15-
ImageLiteral Literal = "image"
16-
ImageUrlLiteral Literal = "image_url"
14+
TextLiteral Literal = "text"
15+
ImageLiteral Literal = "image"
16+
ImageUrlLiteral Literal = "image_url"
17+
ReasoningLiteral Literal = "reasoning"
18+
ToolCallLiteral Literal = "tool_call"
19+
ToolCallResponseLiteral Literal = "tool_call_response"
1720
)
1821

1922
type ModelMessagePartType string
@@ -78,6 +81,7 @@ func convertModelMsg(msg map[string]interface{}) map[string]interface{} {
7881
}
7982
if len(contents) > 0 {
8083
parts := make([]interface{}, 0, len(contents))
84+
toolCalls := make([]interface{}, 0)
8185
for _, content := range contents {
8286
if mc, ok := content.(map[string]interface{}); ok {
8387
mcContent, ok := mc["message_content"].(map[string]interface{})
@@ -102,11 +106,42 @@ func convertModelMsg(msg map[string]interface{}) map[string]interface{} {
102106
url, _ := imageMap["url"]
103107
part["image_url"] = map[string]interface{}{"url": url}
104108
}
109+
case string(ReasoningLiteral):
110+
modelMsg["reasoning_content"] = text
111+
part = nil
112+
case string(ToolCallLiteral):
113+
toolCallId, _ := mcContent["id"]
114+
toolCallName, _ := mcContent["name"]
115+
toolCallArguments, _ := mcContent["arguments"]
116+
modelCall := map[string]interface{}{
117+
"type": "function",
118+
"id": toolCallId,
119+
"function": map[string]interface{}{
120+
"name": toolCallName,
121+
"arguments": toolCallArguments,
122+
},
123+
}
124+
toolCalls = append(toolCalls, modelCall)
125+
part = nil
126+
case string(ToolCallResponseLiteral):
127+
toolCallId, _ := mcContent["id"]
128+
toolCallResult, _ := mcContent["response"]
129+
if toolCallResult == nil {
130+
toolCallResult, _ = mcContent["result"]
131+
}
132+
modelMsg["content"] = toolCallResult
133+
modelMsg["tool_call_id"] = toolCallId
134+
part = nil
105135
default:
106136
}
107-
parts = append(parts, part)
137+
if part != nil {
138+
parts = append(parts, part)
139+
}
108140
}
109141
}
142+
if len(toolCalls) > 0 {
143+
modelMsg["tool_calls"] = toolCalls
144+
}
110145
if len(parts) > 0 {
111146
modelMsg["parts"] = parts
112147
}

0 commit comments

Comments
 (0)