Skip to content

Commit

Permalink
Merge pull request #161 from Cirilla-zmh/lumian/fea_dashscope_o11y
Browse files Browse the repository at this point in the history
example: customize the implementation of dashscope chat model
  • Loading branch information
chickenlj authored Dec 4, 2024
2 parents 57a9b7c + 16662e5 commit 1bf628c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ target/

### IntelliJ IDEA ###
.idea/*
**/.idea/*
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.alibaba.cloud.ai.example.observability;

import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeChatProperties;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import io.micrometer.observation.ObservationRegistry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallbackContext;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@SpringBootApplication
Expand All @@ -24,6 +33,21 @@ public static void main(String[] args) {
ChatClient chatClient(ChatClient.Builder builder) {
return builder.build();
}

@Bean
@ConditionalOnProperty(prefix = DashScopeChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
public DashScopeChatModel dashscopeChatModel(DashScopeChatProperties chatProperties, List<FunctionCallback> toolFunctionCallbacks,
FunctionCallbackContext functionCallbackContext, RetryTemplate retryTemplate,
ObjectProvider<ObservationRegistry> observationRegistry, DashScopeApi dashScopeApi) {

if (!CollectionUtils.isEmpty(toolFunctionCallbacks)) {
chatProperties.getOptions().getFunctionCallbacks().addAll(toolFunctionCallbacks);
}

return new DashScopeChatModel(dashScopeApi, chatProperties.getOptions(), functionCallbackContext, retryTemplate,
observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP));
}
}

@Controller
Expand Down

0 comments on commit 1bf628c

Please sign in to comment.