129 lines
3.4 KiB
Java
129 lines
3.4 KiB
Java
package com.homme.demo.service;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
import org.springframework.web.reactive.function.client.WebClientResponseException;
|
|
|
|
import com.azure.core.credential.AccessToken;
|
|
import com.azure.core.credential.TokenRequestContext;
|
|
import com.azure.identity.DefaultAzureCredential;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
@Service
|
|
public class AzureWebSearchAgentService {
|
|
|
|
@Value("${azure.web-agent.project-endpoint}")
|
|
private String projectEndpoint;
|
|
|
|
@Value("${azure.web-agent.name}")
|
|
private String agentName;
|
|
|
|
@Autowired
|
|
private WebClient webClient;
|
|
|
|
@Autowired
|
|
private ObjectMapper objectMapper;
|
|
|
|
@Autowired
|
|
private DefaultAzureCredential credential;
|
|
|
|
public String search(String question) {
|
|
|
|
try {
|
|
String token = getAccessToken();
|
|
String url = projectEndpoint +"/openai/v1/responses" ;
|
|
Map<String, Object> requestBody = Map.of(
|
|
"input", question,
|
|
"store", false,
|
|
"agent_reference", Map.of(
|
|
"name", agentName,
|
|
"type","agent_reference")
|
|
);
|
|
|
|
System.out.println("requestBody = "+requestBody);
|
|
String response = webClient.post()
|
|
.uri(url)
|
|
.header("Authorization", "Bearer "+token)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.bodyValue(requestBody)
|
|
.retrieve()
|
|
.bodyToMono(String.class)
|
|
.block();
|
|
System.out.println("response = "+response);
|
|
return extractText(response);
|
|
|
|
} catch (WebClientResponseException e) {
|
|
System.out.println("STATUS = " + e.getStatusCode());
|
|
System.out.println("BODY = " + e.getResponseBodyAsString());
|
|
return "Keine zuverlässigen Webinformationen gefunden.";
|
|
} catch (Exception e) {
|
|
System.out.println("WebSearch Fehler = " + e.getMessage());
|
|
return "Keine zuverlässigen Webinformationen gefunden.";
|
|
}
|
|
}
|
|
|
|
|
|
public String getAccessToken() {
|
|
|
|
String scope = "https://ai.azure.com/.default";
|
|
TokenRequestContext requestContext = new TokenRequestContext()
|
|
.addScopes(scope);
|
|
|
|
|
|
AccessToken accessToken = credential.getToken(requestContext).block();
|
|
|
|
if(accessToken == null || accessToken.getToken() == null) {
|
|
|
|
throw new RuntimeException("Das Azure-Zugriffstoken konnte nicht abgerufen werden. Bitte versuchen Sie es später erneut");
|
|
|
|
}
|
|
return accessToken.getToken();
|
|
|
|
}
|
|
|
|
public String extractText(String response) throws Exception{
|
|
|
|
JsonNode root = objectMapper.readTree(response);
|
|
String outputText = root.path("output_text").asText("");
|
|
if(!outputText.isBlank()) {
|
|
|
|
return outputText;
|
|
}
|
|
List<String> texts = new ArrayList<>();
|
|
JsonNode output = root.path("output");
|
|
|
|
if(output.isArray()) {
|
|
|
|
for(JsonNode item: output) {
|
|
|
|
JsonNode content = item.path("content");
|
|
if(content.isArray()) {
|
|
|
|
for(JsonNode c: content) {
|
|
|
|
String text = c.path("text").asText();
|
|
if(!text.isBlank()) {
|
|
texts.add(text);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(texts.isEmpty()) {
|
|
|
|
return "Keine zuverlässigen Webinformatinen gefunden.";
|
|
}
|
|
return String.join("\n", texts);
|
|
|
|
}
|
|
|
|
}
|