Con el agregado de logging

This commit is contained in:
Miguel 2024-04-25 18:11:48 +02:00
parent 575f8c7a61
commit ca93488be3
2 changed files with 44 additions and 4 deletions

29
Logger.cs Normal file
View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace GTPCorrgir
{
using System.Diagnostics;
public class Logger
{
public Logger()
{
string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logfile.log");
Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
Trace.AutoFlush = true;
}
public void Log(string message)
{
Trace.WriteLine(message);
}
}
}

View File

@ -8,12 +8,14 @@ using System.Windows.Threading;
using LanguageDetection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
namespace GTPCorrgir
{
internal class gtpask
{
private readonly string openAiApiKey = "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";
public Logger Log = new Logger();
private Dictionary<string, string> languageMap = new Dictionary<string, string>
{
{ "en", "English" },
@ -58,6 +60,7 @@ namespace GTPCorrgir
if (TextoACorregir.Length>0)
{
IdiomaDetectado = DetectarIdioma(TextoACorregir);
Log.Log("Idioma: " + IdiomaDetectado);
if(IdiomaDetectado != "Desconocido")
return true;
else return false;
@ -67,7 +70,7 @@ namespace GTPCorrgir
public async Task CorregirTexto()
{
Log.Log("Texto a corregir: " + TextoACorregir);
if (DetectarIdioma()) {
if (IdiomaDetectado == "Desconocido" || this.TextoACorregir.Length == 0)
{
@ -81,13 +84,19 @@ namespace GTPCorrgir
TextoACorregir = md.MarkTechnicalTerms(TextoACorregir);
//TextoCorregido = await CallOpenAiApi(TextoACorregir);
string RespuestaLLM = await CallOllamaApi(TextoACorregir);
Log.Log("Texto a marcado: " + TextoACorregir);
string RespuestaLLM = await CallOpenAiApi(TextoACorregir);
//string RespuestaLLM = await CallOllamaApi(TextoACorregir);
Log.Log("Respuesta: " + RespuestaLLM);
TextoCorregido = ExtractCorrectedText(RespuestaLLM);
// Elimina comillas al principio y al final si existen
TextoCorregido = TextoCorregido.Trim('\"');
Log.Log("Texto a corregido: " + TextoCorregido);
}
}
@ -141,6 +150,7 @@ namespace GTPCorrgir
var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json");
try
{
Log.Log("Ask Ollama: " + content.ToString());
var response = await httpClient.PostAsync("http://127.0.0.1:11434/api/chat", content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
@ -188,6 +198,7 @@ namespace GTPCorrgir
var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json");
try
{
Log.Log("Ask OpenAI: " + content.ToString());
var response = await httpClient.PostAsync("https://api.openai.com/v1/chat/completions", content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();