Skip to content

Commit cc3fbb9

Browse files
committed
Bump version
1 parent f10ab7c commit cc3fbb9

14 files changed

+470
-171
lines changed

CommentHandler.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ public static async void Document_CommentsEventHandler(Word.Selection selection)
4242
Comment reply = c.Replies[i];
4343
chatHistory.Add((i % 2 == 1) ? new UserChatMessage(reply.Range.Text) : new AssistantChatMessage(reply.Range.Text));
4444
}
45-
await AddComment(
46-
c.Replies,
47-
c.Range,
48-
RAGControl.AskQuestion(Forge.CommentSystemPrompt, chatHistory, CommonUtils.GetActiveDocument().Range())
49-
);
45+
try
46+
{
47+
await AddComment(
48+
c.Replies,
49+
c.Range,
50+
RAGControl.AskQuestion(Forge.CommentSystemPrompt, chatHistory, CommonUtils.GetActiveDocument().Range())
51+
);
52+
} catch (OperationCanceledException ex)
53+
{
54+
CommonUtils.DisplayWarning(ex);
55+
}
5056
numComments++;
5157

5258
_isDraftingComment = false;

CommonUtils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ public static void DisplayError(Exception ex)
1313
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
1414
}
1515

16+
public static void DisplayWarning(Exception ex)
17+
{
18+
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
19+
}
20+
21+
public static void DisplayInformation(Exception ex)
22+
{
23+
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
24+
}
25+
26+
public static bool GetInternetAccessPermission(string url)
27+
{
28+
var result = MessageBox.Show($"Do you want to allow TextCraft to access the following internet resource?{Environment.NewLine}{url}", "Internet Access", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
29+
return result == DialogResult.Yes;
30+
}
31+
1632
public static Word.Application GetApplication()
1733
{
1834
return Globals.ThisAddIn.Application;

Forge.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.ClientModel;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Reflection;
65
using System.Text;
76
using System.Threading;
87
using System.Threading.Tasks;
@@ -197,7 +196,13 @@ private static async Task ReviewButton_Click()
197196
if (Globals.ThisAddIn.Application.Selection.End - Globals.ThisAddIn.Application.Selection.Start > 0)
198197
{
199198
var selectionRange = CommonUtils.GetSelectionRange();
200-
await CommentHandler.AddComment(CommonUtils.GetComments(), selectionRange, Review(paragraphs, selectionRange, prompt));
199+
try
200+
{
201+
await CommentHandler.AddComment(CommonUtils.GetComments(), selectionRange, Review(paragraphs, selectionRange, prompt));
202+
} catch (OperationCanceledException ex)
203+
{
204+
CommonUtils.DisplayWarning(ex);
205+
}
201206
hasCommented = true;
202207
}
203208
else
@@ -235,14 +240,21 @@ private static async Task AnalyzeText(string systemPrompt, string userPrompt)
235240
var selectionRange = Globals.ThisAddIn.Application.Selection.Range;
236241
var range = (selectionRange.End - selectionRange.Start > 0) ? selectionRange : throw new InvalidRangeException("No text is selected for analysis!");
237242

238-
ChatClient client = new ChatClient(ThisAddIn.Model, ThisAddIn.ApiKey, ThisAddIn.ClientOptions);
243+
ChatClient client = new ChatClient(ThisAddIn.Model, new ApiKeyCredential(ThisAddIn.ApiKey), ThisAddIn.ClientOptions);
239244
var streamingAnswer = client.CompleteChatStreamingAsync(
240245
new List<ChatMessage>() { new SystemChatMessage(systemPrompt), new UserChatMessage(@$"{userPrompt}: {range.Text}") },
241-
new ChatCompletionOptions() { MaxTokens = ThisAddIn.ContextLength },
246+
new ChatCompletionOptions() { MaxOutputTokenCount = ThisAddIn.ContextLength },
242247
ThisAddIn.CancellationTokenSource.Token
243248
);
249+
244250
range.Delete();
245-
await AddStreamingContentToRange(streamingAnswer, range);
251+
try
252+
{
253+
await AddStreamingContentToRange(streamingAnswer, range);
254+
} catch (OperationCanceledException ex)
255+
{
256+
CommonUtils.DisplayWarning(ex);
257+
}
246258
Globals.ThisAddIn.Application.Selection.SetRange(range.Start, range.End);
247259
}
248260

GenerateUserControl.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Windows.Forms;
54
using OpenAI.Chat;
6-
using static System.Net.Mime.MediaTypeNames;
75

86
namespace TextForge
97
{
@@ -27,7 +25,7 @@ private async void GenerateButton_Click(object sender, EventArgs e)
2725
{
2826
string textBoxContent = this.PromptTextBox.Text;
2927
if (textBoxContent.Length == 0)
30-
throw new ArgumentException("The textbox is empty!");
28+
throw new TextBoxEmptyException("The textbox is empty!");
3129

3230
/*
3331
* So, If the user changes the selection carot in Word after clicking "generate" (bc it takes so long to generate text).
@@ -48,13 +46,13 @@ private async void GenerateButton_Click(object sender, EventArgs e)
4846
await Forge.AddStreamingContentToRange(streamingAnswer, rangeBeforeChat);
4947
Globals.ThisAddIn.Application.Selection.SetRange(rangeBeforeChat.Start, rangeBeforeChat.End);
5048
}
51-
catch (ArgumentException ex)
49+
catch (TextBoxEmptyException ex)
5250
{
53-
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
51+
CommonUtils.DisplayInformation(ex);
5452
}
5553
catch (OperationCanceledException ex)
5654
{
57-
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
55+
CommonUtils.DisplayWarning(ex);
5856
}
5957
catch (Exception ex)
6058
{
@@ -112,6 +110,10 @@ private void PromptTextBox_KeyDown(object sender, KeyEventArgs e)
112110
CommonUtils.DisplayError(ex);
113111
}
114112
}
113+
}
115114

115+
public class TextBoxEmptyException : ArgumentException
116+
{
117+
public TextBoxEmptyException(string message) : base(message) { }
116118
}
117119
}

ModelProperties.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
64
using OpenAI.Models;
75

86
namespace TextForge
97
{
108
internal class ModelProperties
119
{
1210
// Public
11+
public const int BaselineContextWindowLength = 4096; // Change this if necessary
1312
public static List<string> UniqueEmbedModels { get { return _embedModels; } }
1413

1514
// Private
@@ -140,6 +139,12 @@ internal class ModelProperties
140139
// new models
141140
{ "minicpm-v", 32768 },
142141
{ "reader-lm", 256000 },
142+
{ "mistral-small", 131072 },
143+
{ "bespoke-minicheck", 32768 },
144+
{ "qwen2.5", 32768 },
145+
{ "nemotron-mini", 4096 },
146+
{ "solar-pro", 4096 },
147+
{ "qwen2.5-coder", 32768 }
143148
};
144149

145150
public static int GetContextLength(string modelName)
@@ -151,7 +156,7 @@ public static int GetContextLength(string modelName)
151156
else if (modelName.Contains(':'))
152157
{
153158
string key = modelName.Split(':')[0];
154-
return ollamaModelsContextLength.ContainsKey(key) ? ollamaModelsContextLength[key] : ThisAddIn.BaselineContextWindowLength;
159+
return ollamaModelsContextLength.ContainsKey(key) ? ollamaModelsContextLength[key] : BaselineContextWindowLength;
155160
}
156161
else if (modelName.StartsWith("o1"))
157162
{
@@ -175,7 +180,7 @@ public static int GetContextLength(string modelName)
175180
}
176181
else
177182
{
178-
return ThisAddIn.BaselineContextWindowLength;
183+
return BaselineContextWindowLength;
179184
}
180185
}
181186

0 commit comments

Comments
 (0)