-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRibbon1.cs
170 lines (152 loc) · 6.39 KB
/
Ribbon1.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Net;
using EmailReplyParser;
using Microsoft.Office.Core;
using System.Configuration;
using System.IO;
namespace OutlookGPT
{
public partial class Ribbon1
{
public string apikey = "";
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
// StreamWriter sw = new StreamWriter(Application.UserAppDataPath + "openaikey.dat"
if (!File.Exists(System.Windows.Forms.Application.UserAppDataPath + "openaikey.dat"))
{
Form frm2 = new Form2();
frm2.ShowDialog();
return;
}
else
{
using (StreamReader sr = new StreamReader(System.Windows.Forms.Application.UserAppDataPath + "openaikey.dat"))
{
while (!sr.EndOfStream)
{
apikey = sr.ReadLine();
break;
}
}
}
}
private void dropDown1_SelectionChanged(object sender, RibbonControlEventArgs e)
{
}
private void dropDown1_ButtonClick(object sender, RibbonControlEventArgs e)
{
}
private async void button1_Click(object sender, RibbonControlEventArgs e)
{
// don't want them moving on without setting an API key
if (apikey.Length < 5)
{
Form frm2 = new Form2();
frm2.ShowDialog();
return;
}
// find the compose window
Outlook.Application application = Globals.ThisAddIn.Application;
Outlook.Inspector inspector = application.ActiveInspector();
Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem;
// verify there is one
if (mailItem == null)
{
MessageBox.Show("I couldn't find an email. Sorry.");
return;
}
string prompt = "";
string systemrole = "";
// go through options
switch (dropDown1.SelectedItem.Label)
{
case "Positive":
systemrole = "Please rephrase the following statement as a positive message: ";
break;
case "Conscionable":
systemrole = "Please rephrase the following statement more conscionable: ";
break;
case "Politically Correct":
systemrole = "Please rephrase the following statement as politically correct as possible: ";
break;
case "Stern - Gently":
systemrole = "Please rephrase the following statement very stern, but also gently: ";
break;
case "Stern - Direct":
systemrole = "Please rephrase the following statement very stern and direct: ";
break;
case "Shorten":
systemrole = "Please rephrase the following statement so it's more succint and professional: ";
break;
}
// add stripped mail body
string oldmail = mailItem.HTMLBody;
var parsed_body = EmailReplyParser.EmailParser.Parse(mailItem.Body);
prompt += parsed_body.Fragments[0].Content;
//prepare all the parameters
//string apiKey = "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";
string model = "gpt-3.5-turbo";
int maxTokens = 1024;
float temperature = 0.7f;
// Build the API request
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string requestUrl = $"https://api.openai.com/v1/chat/completions";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apikey}");
var requestJson = new
{
messages = new[]
{
new
{
role = "system",
content = systemrole
},
new
{
role = "user",
content = prompt
}
},
max_tokens = maxTokens,
temperature = temperature,
model = model
};
StringContent content = new StringContent(JsonConvert.SerializeObject(requestJson), Encoding.UTF8, "application/json");
// Send the request and receive the response
HttpResponseMessage response = client.PostAsync(requestUrl, content).Result;
string responseJson = response.Content.ReadAsStringAsync().Result;
// Extract the completed text from the response
dynamic responseObject = JsonConvert.DeserializeObject(responseJson);
string completedText = responseObject.choices[0].message.content;
completedText = completedText.Replace("\n", "<br>");
mailItem.HTMLBody = completedText + "<hr>" + oldmail;
}
catch (System.Exception ex)
{
MessageBox.Show("An error occurred: " + ex.ToString() + Environment.NewLine + Environment.NewLine + "I am opening up the Open API form just in case.", "Outlook GPT Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Form frm = new Form2();
frm.ShowDialog();
}
}
private void btnKey_Click(object sender, RibbonControlEventArgs e)
{
Form frm2 = new Form2();
frm2.ShowDialog();
}
}
}