22
22
23
23
import com .openai .models .ChatCompletion ;
24
24
import com .openai .models .ChatCompletionAssistantMessageParam ;
25
- import com .openai .models .ChatCompletionContentPart ;
26
25
import com .openai .models .ChatCompletionContentPartText ;
27
26
import com .openai .models .ChatCompletionCreateParams ;
28
27
import com .openai .models .ChatCompletionMessage ;
40
39
import java .util .HashMap ;
41
40
import java .util .List ;
42
41
import java .util .Map ;
42
+ import java .util .Objects ;
43
43
import java .util .stream .Collectors ;
44
44
45
45
public class ChatCompletionEventsHelper {
@@ -54,24 +54,28 @@ public static void emitPromptLogEvents(
54
54
if (!settings .emitEvents ) {
55
55
return ;
56
56
}
57
+
57
58
for (ChatCompletionMessageParam msg : request .messages ()) {
58
59
String eventType ;
59
60
MapValueBuilder bodyBuilder = new MapValueBuilder ();
60
- if (msg .isChatCompletionSystemMessageParam ()) {
61
- ChatCompletionSystemMessageParam sysMsg = msg .asChatCompletionSystemMessageParam ();
61
+ Object concreteMessageParam = ApiAdapter .get ().extractConcreteCompletionMessageParam (msg );
62
+ if (concreteMessageParam instanceof ChatCompletionSystemMessageParam ) {
63
+ ChatCompletionSystemMessageParam sysMsg =
64
+ (ChatCompletionSystemMessageParam ) concreteMessageParam ;
62
65
eventType = "gen_ai.system.message" ;
63
66
if (settings .captureMessageContent ) {
64
67
putIfNotEmpty (bodyBuilder , "content" , contentToString (sysMsg .content ()));
65
68
}
66
- } else if (msg .isChatCompletionUserMessageParam ()) {
67
- ChatCompletionUserMessageParam userMsg = msg .asChatCompletionUserMessageParam ();
69
+ } else if (concreteMessageParam instanceof ChatCompletionUserMessageParam ) {
70
+ ChatCompletionUserMessageParam userMsg =
71
+ (ChatCompletionUserMessageParam ) concreteMessageParam ;
68
72
eventType = "gen_ai.user.message" ;
69
73
if (settings .captureMessageContent ) {
70
74
putIfNotEmpty (bodyBuilder , "content" , contentToString (userMsg .content ()));
71
75
}
72
- } else if (msg . isChatCompletionAssistantMessageParam () ) {
76
+ } else if (concreteMessageParam instanceof ChatCompletionAssistantMessageParam ) {
73
77
ChatCompletionAssistantMessageParam assistantMsg =
74
- msg . asChatCompletionAssistantMessageParam () ;
78
+ ( ChatCompletionAssistantMessageParam ) concreteMessageParam ;
75
79
eventType = "gen_ai.assistant.message" ;
76
80
if (settings .captureMessageContent ) {
77
81
assistantMsg
@@ -89,8 +93,9 @@ public static void emitPromptLogEvents(
89
93
bodyBuilder .put ("tool_calls" , Value .of (toolCallsJson ));
90
94
});
91
95
}
92
- } else if (msg .isChatCompletionToolMessageParam ()) {
93
- ChatCompletionToolMessageParam toolMsg = msg .asChatCompletionToolMessageParam ();
96
+ } else if (concreteMessageParam instanceof ChatCompletionToolMessageParam ) {
97
+ ChatCompletionToolMessageParam toolMsg =
98
+ (ChatCompletionToolMessageParam ) concreteMessageParam ;
94
99
eventType = "gen_ai.tool.message" ;
95
100
if (settings .captureMessageContent ) {
96
101
putIfNotEmpty (bodyBuilder , "content" , contentToString (toolMsg .content ()));
@@ -110,8 +115,9 @@ private static void putIfNotEmpty(MapValueBuilder bodyBuilder, String key, Strin
110
115
}
111
116
112
117
private static String contentToString (ChatCompletionToolMessageParam .Content content ) {
113
- if (content .isTextContent ()) {
114
- return content .asTextContent ();
118
+ String text = ApiAdapter .get ().asText (content );
119
+ if (text != null ) {
120
+ return text ;
115
121
} else if (content .isArrayOfContentParts ()) {
116
122
return content .asArrayOfContentParts ().stream ()
117
123
.map (ChatCompletionContentPartText ::text )
@@ -122,28 +128,23 @@ private static String contentToString(ChatCompletionToolMessageParam.Content con
122
128
}
123
129
124
130
private static String contentToString (ChatCompletionAssistantMessageParam .Content content ) {
125
- if (content .isTextContent ()) {
126
- return content .asTextContent ();
131
+ String text = ApiAdapter .get ().asText (content );
132
+ if (text != null ) {
133
+ return text ;
127
134
} else if (content .isArrayOfContentParts ()) {
128
135
return content .asArrayOfContentParts ().stream ()
129
- .map (
130
- cnt -> {
131
- if (cnt .isChatCompletionContentPartText ()) {
132
- return cnt .asChatCompletionContentPartText ().text ();
133
- } else if (cnt .isChatCompletionContentPartRefusal ()) {
134
- return cnt .asChatCompletionContentPartRefusal ().refusal ();
135
- }
136
- return "" ;
137
- })
136
+ .map (ApiAdapter .get ()::extractTextOrRefusal )
137
+ .filter (Objects ::nonNull )
138
138
.collect (Collectors .joining ());
139
139
} else {
140
140
throw new IllegalStateException ("Unhandled content type for " + content );
141
141
}
142
142
}
143
143
144
144
private static String contentToString (ChatCompletionSystemMessageParam .Content content ) {
145
- if (content .isTextContent ()) {
146
- return content .asTextContent ();
145
+ String text = ApiAdapter .get ().asText (content );
146
+ if (text != null ) {
147
+ return text ;
147
148
} else if (content .isArrayOfContentParts ()) {
148
149
return content .asArrayOfContentParts ().stream ()
149
150
.map (ChatCompletionContentPartText ::text )
@@ -154,13 +155,13 @@ private static String contentToString(ChatCompletionSystemMessageParam.Content c
154
155
}
155
156
156
157
private static String contentToString (ChatCompletionUserMessageParam .Content content ) {
157
- if (content .isTextContent ()) {
158
- return content .asTextContent ();
158
+ String text = ApiAdapter .get ().asText (content );
159
+ if (text != null ) {
160
+ return text ;
159
161
} else if (content .isArrayOfContentParts ()) {
160
162
return content .asArrayOfContentParts ().stream ()
161
- .filter (ChatCompletionContentPart ::isChatCompletionContentPartText )
162
- .map (ChatCompletionContentPart ::asChatCompletionContentPartText )
163
- .map (ChatCompletionContentPartText ::text )
163
+ .map (ApiAdapter .get ()::extractText )
164
+ .filter (Objects ::nonNull )
164
165
.collect (Collectors .joining ());
165
166
} else {
166
167
throw new IllegalStateException ("Unhandled content type for " + content );
0 commit comments