All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.zhangjh.gemini.service.GeminiServiceImpl Maven / Gradle / Ivy

The newest version!
package me.zhangjh.gemini.service;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhangjh.gemini.client.GeminiService;
import me.zhangjh.gemini.common.MimeTypeEnum;
import me.zhangjh.gemini.common.RoleEnum;
import me.zhangjh.gemini.pojo.*;
import me.zhangjh.gemini.request.*;
import me.zhangjh.gemini.response.TextResponse;
import me.zhangjh.gemini.response.VisionResponse;
import me.zhangjh.gemini.util.HttpClientUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

/**
 * @author [email protected]
 * @date 21:08 2023/12/22
 * @Description
 */
@Slf4j
@Component
public class GeminiServiceImpl implements GeminiService {

    @Value("${API_KEY}")
    private String apiKey;

    @Value("${URL_BASE}")
    private String urlBase;

    @Override
    public TextResponse generateByText(TextRequest request) {
        List contents = request.getContents();
        Assert.isTrue(CollectionUtils.isNotEmpty(contents), "Empty contents");
        HttpRequest httpRequest = new HttpRequest(
                urlBase + "/" + request.getVersion() + request.getUrlPath() + "?key=" + apiKey);
        String reqData = JSONObject.toJSONString(request);
        log.info("generateByText request: {}", reqData);
        httpRequest.setReqData(reqData);
        String res = HttpClientUtil.sendSync(httpRequest);
        Assert.isTrue(StringUtils.isNotEmpty(res), "empty res returned");
        return JSONObject.parseObject(res, TextResponse.class);
    }

    @Override
    public VisionResponse generateByMix(MixRequest request) {
        List contents = request.getContents();
        Assert.isTrue(CollectionUtils.isNotEmpty(contents), "Empty contents");
        // mime check
        for (Content content : contents) {
            for (Part part : content.getParts()) {
                if(part instanceof ImagePart) {
                    MimeTypeEnum.getByCode(((ImagePart)part).getInlineData().getMimeType());
                }
            }
        }
        HttpRequest httpRequest = new HttpRequest(
                urlBase + "/" + request.getVersion() + request.getUrlPath() + "?key=" + apiKey);
        httpRequest.setReqData(JSONObject.toJSONString(request));
        String res = HttpClientUtil.sendSync(httpRequest);
        log.info("generateByMix: {}", res);
        Assert.isTrue(StringUtils.isNotEmpty(res), "empty res returned");
        return JSONObject.parseObject(res, VisionResponse.class);
    }

    @Override
    public TextResponse generateByText(String text) {
        Assert.isTrue(StringUtils.isNotEmpty(text), "text input empty");
        TextRequest textRequest = new TextRequest();
        List contents = new ArrayList<>();
        ChatContent content = ChatContent.buildBySingleText(text);
        contents.add(content);
        textRequest.setContents(contents);
        return this.generateByText(textRequest);
    }

    @Override
    public VisionResponse generateByMix(String text, String image, String mimeType) {
        Assert.isTrue(StringUtils.isNotEmpty(text), "text input empty");
        Assert.isTrue(StringUtils.isNotEmpty(image), "image base64 content empty");
        Assert.isTrue(StringUtils.isNotEmpty(mimeType), "image mimeType empty");
        MimeTypeEnum.getByCode(mimeType);
        MixRequest mixRequest = new MixRequest();
        List contents = new ArrayList<>();
        Content content = new Content();
        List parts = new ArrayList<>();
        TextPart textPart = new TextPart(text);
        parts.add(textPart);
        ImagePart imagePart = new ImagePart();
        InlineData inlineData = new InlineData(mimeType, image);
        imagePart.setInlineData(inlineData);
        parts.add(imagePart);
        content.setParts(parts);
        contents.add(content);
        mixRequest.setContents(contents);
        return this.generateByMix(mixRequest);
    }

    @Override
    public TextResponse multiTurnChat(MultiTurnRequest request) {
        List contents = request.getContents();
        Assert.isTrue(CollectionUtils.isNotEmpty(contents), "request empty");
        HttpRequest httpRequest = new HttpRequest(urlBase + "/" + request.getVersion()
                + request.getUrlPath() + "?key=" + apiKey);
        httpRequest.setReqData(JSONObject.toJSONString(request));
        String res = HttpClientUtil.sendSync(httpRequest);
        Assert.isTrue(StringUtils.isNotEmpty(res), "empty res returned");
        return JSONObject.parseObject(res, TextResponse.class);
    }

    @Override
    public String multiTurnChat(String question, List context) {
        MultiTurnRequest multiTurnRequest = new MultiTurnRequest();
        List contents = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(context)) {
            contents.addAll(context);
        }
        ChatContent chatContent = new ChatContent();
        chatContent.setRole(RoleEnum.user.name());
        chatContent.setParts(Collections.singletonList(new TextPart(question)));
        contents.add(chatContent);
        multiTurnRequest.setContents(contents);
        TextResponse textResponse = this.multiTurnChat(multiTurnRequest);
        if (CollectionUtils.isEmpty(textResponse.getCandidates())) {
            return null;
        }
        List res = new ArrayList<>();
        for (Candidate candidate : textResponse.getCandidates()) {
            for (Part part : candidate.getContent().getParts()) {
                res.add(part.getText());
            }
        }
        return String.join("", res);
    }

    @Override
    public void streamChat(StreamRequest request, Function cb) {
        List contents = request.getContents();
        Assert.isTrue(CollectionUtils.isNotEmpty(contents), "contents empty");
        HttpRequest httpRequest = new HttpRequest(urlBase + "/" + request.getVersion()
                + request.getUrlPath() + "?key=" + apiKey);
        httpRequest.setReqData(JSONObject.toJSONString(request));
        HttpClientUtil.sendStream(httpRequest, cb);
    }

    @Override
    public void streamChat(String question, List context, Function cb) {
        Assert.isTrue(StringUtils.isNotEmpty(question), "question empty");
        StreamRequest streamRequest = new StreamRequest<>();
        ChatContent chatContent = new ChatContent();
        List contents = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(context)) {
            contents.addAll(context);
        }
        chatContent.setRole(RoleEnum.user.name());
        chatContent.setParts(Collections.singletonList(new TextPart(question)));
        contents.add(chatContent);
        streamRequest.setContents(contents);
        this.streamChat(streamRequest, cb);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy