using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Threading; using LanguageDetection; using Newtonsoft.Json; namespace GTPCorrgir { internal class gtpask { private readonly string openAiApiKey = "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"; private readonly string accionGTP = "Me puedes corregir el siguiente texto y mejorarlo para que se comprenda mejor: "; private Dictionary languageMap = new Dictionary { { "en", "Inglés" }, { "es", "Español" }, { "it", "Italiano" }, { "pt", "Portugués" } // Agrega más idiomas según sea necesario }; public string IdiomaDetectado; public string TextoACorregir; public string TextoCorregido; private string DetectarIdioma(string TextoACorregir) { LanguageDetector detector = new LanguageDetector(); detector.AddLanguages("en", "es", "it", "pt"); string detectedLanguageCode = detector.Detect(TextoACorregir); string detectedLanguageName = languageMap.ContainsKey(detectedLanguageCode) ? languageMap[detectedLanguageCode] : "Desconocido"; return detectedLanguageName; } private bool DetectarIdioma() { if (TextoACorregir.Length>0) { IdiomaDetectado = DetectarIdioma(TextoACorregir); if(IdiomaDetectado != "Desconocido") return true; else return false; } return false; } public async Task CorregirTexto() { var Instrucciones = accionGTP; if (DetectarIdioma()) { if (IdiomaDetectado == "Desconocido" || this.TextoACorregir.Length == 0) { // Nada que hacer TextoCorregido = TextoACorregir; return; } TextoACorregir = "El resultado debe estar en " + IdiomaDetectado + ". " + Instrucciones + "\"" + TextoACorregir + "\""; TextoCorregido = await CallOpenAiApi(TextoACorregir); //TextoCorregido = await CallOllamaApi(TextoACorregir); // Elimina comillas al principio y al final si existen TextoCorregido = TextoCorregido.Trim('\"'); } } private async Task CallOllamaApi(string input) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {openAiApiKey}"); var requestData = new { model = "llama3", messages = new[] { new { role = "user", content = input }, }, stream = false }; var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json"); try { var response = await httpClient.PostAsync("http://127.0.0.1:11434/api/chat", content); response.EnsureSuccessStatusCode(); var jsonResponse = await response.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonResponse); if (data.done == true && data.message != null) { return data.message.content; } return "No hubo respuesta del asistente o la sesión aún no ha concluido."; } catch (HttpRequestException e) { // Captura errores en la solicitud HTTP, como problemas de red o respuestas de error HTTP. Console.WriteLine($"Error making HTTP request: {e.Message}"); return null; } catch (Exception e) { // Captura cualquier otro error Console.WriteLine($"An error occurred: {e.Message}"); return null; } } private async Task CallOpenAiApi(string input) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {openAiApiKey}"); var requestData = new { model = "gpt-4", messages = new[] { new { role = "system", content = "Este es un mensaje del sistema inicializando la conversación" }, new { role = "user", content = input } } }; var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json"); try { var response = await httpClient.PostAsync("https://api.openai.com/v1/chat/completions", content); response.EnsureSuccessStatusCode(); var jsonResponse = await response.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonResponse); return data.choices[0].message.content; } catch (HttpRequestException e) { // Captura errores en la solicitud HTTP, como problemas de red o respuestas de error HTTP. Console.WriteLine($"Error making HTTP request: {e.Message}"); return null; } catch (Exception e) { // Captura cualquier otro error Console.WriteLine($"An error occurred: {e.Message}"); return null; } } } }