a ia esta a dar eero
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
package com.example.cuida.services;
|
||||
|
||||
// Imports básicos do SDK do Google AI
|
||||
import com.google.ai.client.generativeai.GenerativeModel;
|
||||
import com.google.ai.client.generativeai.java.GenerativeModelFutures;
|
||||
|
||||
// Imports de tipos e configurações
|
||||
import com.google.ai.client.generativeai.type.BlockThreshold;
|
||||
import com.google.ai.client.generativeai.type.Content;
|
||||
import com.google.ai.client.generativeai.type.GenerateContentResponse;
|
||||
import com.google.ai.client.generativeai.type.GenerationConfig;
|
||||
import com.google.ai.client.generativeai.type.HarmCategory;
|
||||
import com.google.ai.client.generativeai.type.SafetySetting;
|
||||
|
||||
// Imports do Guava para processamento assíncrono
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import com.google.ai.client.generativeai.type.BlockThreshold;
|
||||
import com.google.ai.client.generativeai.type.HarmCategory;
|
||||
import com.google.ai.client.generativeai.type.SafetySetting;
|
||||
|
||||
// Imports standard de Java
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Gemini {
|
||||
private final GenerativeModelFutures modelo;
|
||||
|
||||
public Gemini() {
|
||||
// 1. Configurar os SafetySettings para permitir termos médicos e partes do corpo
|
||||
// 1. Configurar Segurança
|
||||
List<SafetySetting> safetySettings = Arrays.asList(
|
||||
new SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.NONE),
|
||||
new SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.NONE),
|
||||
@@ -31,39 +35,43 @@ public class Gemini {
|
||||
new SafetySetting(HarmCategory.DANGEROUS_CONTENT, BlockThreshold.NONE)
|
||||
);
|
||||
|
||||
// 2. Configurar o modelo (usa a tua API Key do Google AI Studio)
|
||||
// 2. Configuração de Geração
|
||||
GenerationConfig config = new GenerationConfig.Builder()
|
||||
.setTemperature(0.7f)
|
||||
.build();
|
||||
|
||||
// 3. Inicialização do Modelo (Gemini 1.5 Flash)
|
||||
GenerativeModel generativeModel = new GenerativeModel(
|
||||
"gemini-2.5-flash",
|
||||
"gemini-1.5-flash",
|
||||
"AIzaSyBmLgn-SHaTDvAeDWsw2iTZRR9gahhOu7k",
|
||||
null, // generationConfig
|
||||
config,
|
||||
safetySettings
|
||||
);
|
||||
|
||||
this.modelo = GenerativeModelFutures.from(generativeModel);
|
||||
}
|
||||
|
||||
public interface GeminiCallback {
|
||||
void onSuccess(String result);
|
||||
|
||||
void onError(Throwable t);
|
||||
}
|
||||
|
||||
public void fazerPergunta(String promptUtilizador, GeminiCallback callback) {
|
||||
// 2. Preparar o conteúdo da pergunta
|
||||
Content conteudo = new Content.Builder()
|
||||
.addText(promptUtilizador)
|
||||
.build();
|
||||
|
||||
// 3. Chamar a IA de forma assíncrona
|
||||
ListenableFuture<GenerateContentResponse> respostaFuture = modelo.generateContent(conteudo);
|
||||
|
||||
Executor executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
Futures.addCallback(respostaFuture, new FutureCallback<GenerateContentResponse>() {
|
||||
@Override
|
||||
public void onSuccess(GenerateContentResponse resultado) {
|
||||
// Aqui recebes o texto da IA
|
||||
String textoResposta = resultado.getText();
|
||||
callback.onSuccess(textoResposta);
|
||||
if (resultado != null && resultado.getText() != null) {
|
||||
callback.onSuccess(resultado.getText());
|
||||
} else {
|
||||
callback.onError(new Exception("IA não devolveu texto."));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user