-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAI.cs
127 lines (104 loc) · 3.98 KB
/
OpenAI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Diagnostics;
using System;
using System.IO;
namespace Agora {
public class OpenAI
{
/// <summary>
/// The path to the "Main.py" file, for test purposes its in the "bin\Debug" folder
/// </summary>
public static string PythonScriptPath = @"main.py";
public OpenAI()
{
}
public string GPT(string prompt)
{
// On va créer un script Python temporaire
// Puis placer du code à l'intérieur, enregistrer le fichier
// Et le lancer
string ScriptTemporaire = $@"main{prompt.GetHashCode()}.py";
string pythonSrc = @"
import g4f
# Automatic selection of provider
prompt = """"""
%here%
""""""
# Set with provider
response = g4f.ChatCompletion.create(
model=""gpt-3.5-turbo"",
messages=[{""role"": ""user"", ""content"": prompt}]
)
for message in response:
print(message)
";
// On place le prompt à envoyer à GPT à sa place dans le code Python
string pythonSrcNew = pythonSrc.Replace("%here%", prompt);
// On enregistrer le fichier
File.WriteAllText(ScriptTemporaire, pythonSrcNew);
string output;
ENCORE:
// On génère une instance du procéssus de Python3
Process process = new Process();
process.StartInfo.FileName = "python"; // Sous-entend que Python est dans le PATH de l'OS
process.StartInfo.Arguments = $"{ScriptTemporaire}";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
// Peut prendre jusqu'à 1 minute ! ...
var outputTask = process.StandardOutput.ReadToEnd();
var errorTask = process.StandardError.ReadToEnd();
// On récupère la réponse de GPT et on la néttoie un peu
output = outputTask.Trim().Replace("\r\n", "");
if (output != "")
{
// TODO : Gérer les problèmes
string errorOutput = errorTask;
if (output.Contains("bool, temperature: float)"))
{
output = output.Split(new string[] { "bool, temperature: float)" }, StringSplitOptions.None)[1];
}
if (output.Contains("pip install -U g4f"))
{
output = output.Split(new string[] { "pip install -U g4f" }, StringSplitOptions.None)[1];
}
output = NéttoyerUTF8(output);
}
else
{
goto ENCORE;
}
File.Delete(ScriptTemporaire);
return output;
}
/// <summary>
/// Une fonction pour néttoyer manuellement
/// le message que renvoit les APIs de
/// g4free v2
/// </summary>
/// <param name="output"></param>
/// <returns></returns>
private static string NéttoyerUTF8(string output)
{
output = output.Replace("Ú", "é");
output = output.Replace("Þ", "è");
output = output.Replace("Ó", "à");
output = output.Replace("╔", "É");
output = output.Replace("╩", "Ê");
output = output.Replace("þ", "ç");
output = output.Replace("¨", "ù");
output = output.Replace("Ô", "â");
output = output.Replace("└", "À");
output = output.Replace("¯", "î");
output = output.Replace("Û", "ê");
output = output.Replace("¶", "ô");
output = output.Replace("░", "");
output = output.Replace("ñ", "¤");
output = output.Replace("Ã", "Ç");
output = output.Replace("¹", "û");
output = output.Replace("£", "œ");
return output;
}
}
}