vacuityv / ai-java-sdk

java sdk for ai apis. Support claude ai and google gemini and part of openai (chatgpt). 支持chatgpt/gemini/claude的java sdk。
29 stars 9 forks source link

gemini是否可以支持System Instructions? #11

Closed fantasyjqc closed 2 months ago

fantasyjqc commented 2 months ago

官方的kotlin中有这个配置,想问下大佬的这个sdk是否可以支持?

val model = GenerativeModel( "gemini-1.5-pro", // Retrieve API key as an environmental variable defined in a Build Configuration // see https://github.com/google/secrets-gradle-plugin for further instructions BuildConfig.geminiApiKey, generationConfig = generationConfig { temperature = 1f topK = 64 topP = 0.95f maxOutputTokens = 8192 responseMimeType = "text/plain" }, // safetySettings = Adjust safety settings // See https://ai.google.dev/gemini-api/docs/safety-settings systemInstruction = content { text("请你将所给的日语小说翻译为中文。") }, )

vacuityv commented 2 months ago

@fantasyjqc 我明天看下,加好了会在这里通知你

vacuityv commented 2 months ago

@fantasyjqc 已在 1.7.8 版本添加,使用如下:

@Test
public void testSystemInstruction() {
    GeminiClient client = new GeminiClient(API_KEY);
    ChatMessage systemInstruction = new ChatMessage("You are a assistant named koi, you can help me with my daily work.");

    List<ChatMessage> messages = new ArrayList<>();
    messages.add(new ChatMessage("user", "what's your name"));

    List<ChatRequest.SafetySetting> safetySettings = new ArrayList<>();
    safetySettings.add(new ChatRequest.SafetySetting(HarmCategory.HARM_CATEGORY_HATE_SPEECH.toString(), HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE.toString()));

    ChatRequest request = ChatRequest.builder()
        .model("gemini-1.5-pro")
        .contents(messages)
        .safetySettings(safetySettings)
        .systemInstruction(systemInstruction)
        .build();
    try {
        ChatResponse response = client.chat(request);
        System.out.println(response);
    } catch (VacSdkException e) {
        if (e.getDetails() != null) {
            System.out.println(e.getDetails().get(0).getError().getMessage());
        }
    }
}