|
18 | 18 |
|
19 | 19 | package org.apache.skywalking.apm.plugin.spring.ai.v1; |
20 | 20 |
|
21 | | -import java.lang.reflect.Method; |
22 | | - |
23 | 21 | import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
24 | 22 | import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
25 | 23 | import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
26 | | -import org.springframework.ai.embedding.EmbeddingOptions; |
| 24 | +import org.springframework.ai.embedding.EmbeddingResponse; |
| 25 | +import org.springframework.ai.embedding.EmbeddingResponseMetadata; |
27 | 26 | import org.springframework.util.StringUtils; |
28 | 27 |
|
| 28 | +import java.lang.reflect.Method; |
| 29 | + |
29 | 30 | public class EmbeddingModelInterceptor implements InstanceMethodsAroundInterceptor { |
30 | 31 |
|
31 | 32 | @Override |
32 | | - public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
33 | | - MethodInterceptResult result) { |
34 | | - if (allArguments == null || allArguments.length < 2 || !(allArguments[1] instanceof EmbeddingOptions)) { |
35 | | - return; |
| 33 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) { |
| 39 | + EmbeddingResponseMetadata metadata = ((EmbeddingResponse) ret).getMetadata(); |
| 40 | + if (metadata == null) { |
| 41 | + return ret; |
36 | 42 | } |
37 | | - String model = ((EmbeddingOptions) allArguments[1]).getModel(); |
| 43 | + String model = metadata.getModel(); |
38 | 44 | if (!StringUtils.hasText(model)) { |
39 | | - return; |
| 45 | + return ret; |
40 | 46 | } |
41 | 47 | EmbeddingModelEnhanceContext context = getOrCreateContext(objInst); |
42 | 48 | context.setEmbeddingModelNameIfAbsent(model); |
43 | | - } |
44 | | - |
45 | | - @Override |
46 | | - public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
47 | | - Object ret) { |
48 | 49 | return ret; |
49 | 50 | } |
50 | 51 |
|
51 | 52 | @Override |
52 | | - public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
53 | | - Class<?>[] argumentsTypes, Throwable t) { |
| 53 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { |
54 | 54 | } |
55 | 55 |
|
56 | 56 | private EmbeddingModelEnhanceContext getOrCreateContext(EnhancedInstance objInst) { |
|
0 commit comments