package org.hzhub.service.embed.impl;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.output.Response;
import org.hzhub.common.chat.domain.vo.chat.ChatModelVo;
import org.hzhub.enums.ModalityType;
import org.hzhub.service.embed.BaseEmbedModelService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * @Author: Robust_H
 * @Date: 2025-09-30-下午3:59
 * @Description: OpenAi嵌入模型
 */
@Component("openai")
public class OpenAiEmbeddingProvider implements BaseEmbedModelService {
    protected ChatModelVo chatModelVo;

    @Override
    public void configure(ChatModelVo config) {
        this.chatModelVo = config;
    }

    @Override
    public Set<ModalityType> getSupportedModalities() {
        return Set.of(ModalityType.TEXT);
    }

    @Override
    public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {
        return OpenAiEmbeddingModel.builder()
                .baseUrl(chatModelVo.getApiHost())
                .apiKey(chatModelVo.getApiKey())
                .modelName(chatModelVo.getModelName())
                .dimensions(chatModelVo.getModelDimension())
                .build()
                .embedAll(textSegments);
    }
}
