31 lines
1.7 KiB
Java
31 lines
1.7 KiB
Java
|
|
package com.company.agentfoundation.service;
|
||
|
|
import com.company.agentfoundation.api.DeepseekChatRequest;
|
||
|
|
import com.company.agentfoundation.api.DeepseekChatResponse;
|
||
|
|
import com.company.agentfoundation.api.DeepseekClient;
|
||
|
|
import com.company.agentfoundation.domain.ChatRequest;
|
||
|
|
import com.company.agentfoundation.domain.ChatResponse;
|
||
|
|
import com.company.agentfoundation.security.AgentContext;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.util.UUID;
|
||
|
|
@Service
|
||
|
|
public class ChatService {
|
||
|
|
private final DeepseekClient deepseekClient;
|
||
|
|
private final ModelRouter modelRouter;
|
||
|
|
public ChatService(DeepseekClient deepseekClient, ModelRouter modelRouter) { this.deepseekClient = deepseekClient; this.modelRouter = modelRouter; }
|
||
|
|
public ChatResponse chat(AgentContext context, ChatRequest request) {
|
||
|
|
String traceId = UUID.randomUUID().toString();
|
||
|
|
String model = modelRouter.routeModel(context.agentId(), request.model());
|
||
|
|
DeepseekChatResponse resp = deepseekClient.chat(new DeepseekChatRequest(
|
||
|
|
model,
|
||
|
|
request.messages().stream().map(m -> new DeepseekChatRequest.Message(m.role(), m.content())).toList(),
|
||
|
|
request.temperature(),
|
||
|
|
false));
|
||
|
|
String content = "";
|
||
|
|
if (resp != null && resp.choices() != null && !resp.choices().isEmpty() && resp.choices().get(0).message() != null) content = resp.choices().get(0).message().content();
|
||
|
|
ChatResponse.Usage usage = null;
|
||
|
|
if (resp != null && resp.usage() != null) usage = new ChatResponse.Usage(resp.usage().prompt_tokens(), resp.usage().completion_tokens(), resp.usage().total_tokens());
|
||
|
|
return new ChatResponse(traceId, context.agentId(), model, content, usage, Instant.now());
|
||
|
|
}
|
||
|
|
}
|