Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
suncloudsmoon committed Sep 22, 2024
1 parent f10ab7c commit cc3fbb9
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 171 deletions.
16 changes: 11 additions & 5 deletions CommentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ public static async void Document_CommentsEventHandler(Word.Selection selection)
Comment reply = c.Replies[i];
chatHistory.Add((i % 2 == 1) ? new UserChatMessage(reply.Range.Text) : new AssistantChatMessage(reply.Range.Text));
}
await AddComment(
c.Replies,
c.Range,
RAGControl.AskQuestion(Forge.CommentSystemPrompt, chatHistory, CommonUtils.GetActiveDocument().Range())
);
try
{
await AddComment(
c.Replies,
c.Range,
RAGControl.AskQuestion(Forge.CommentSystemPrompt, chatHistory, CommonUtils.GetActiveDocument().Range())
);
} catch (OperationCanceledException ex)
{
CommonUtils.DisplayWarning(ex);
}
numComments++;

_isDraftingComment = false;
Expand Down
16 changes: 16 additions & 0 deletions CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ public static void DisplayError(Exception ex)
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

public static void DisplayWarning(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

public static void DisplayInformation(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

public static bool GetInternetAccessPermission(string url)
{
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);
return result == DialogResult.Yes;
}

public static Word.Application GetApplication()
{
return Globals.ThisAddIn.Application;
Expand Down
22 changes: 17 additions & 5 deletions Forge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.ClientModel;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -197,7 +196,13 @@ private static async Task ReviewButton_Click()
if (Globals.ThisAddIn.Application.Selection.End - Globals.ThisAddIn.Application.Selection.Start > 0)
{
var selectionRange = CommonUtils.GetSelectionRange();
await CommentHandler.AddComment(CommonUtils.GetComments(), selectionRange, Review(paragraphs, selectionRange, prompt));
try
{
await CommentHandler.AddComment(CommonUtils.GetComments(), selectionRange, Review(paragraphs, selectionRange, prompt));
} catch (OperationCanceledException ex)
{
CommonUtils.DisplayWarning(ex);
}
hasCommented = true;
}
else
Expand Down Expand Up @@ -235,14 +240,21 @@ private static async Task AnalyzeText(string systemPrompt, string userPrompt)
var selectionRange = Globals.ThisAddIn.Application.Selection.Range;
var range = (selectionRange.End - selectionRange.Start > 0) ? selectionRange : throw new InvalidRangeException("No text is selected for analysis!");

ChatClient client = new ChatClient(ThisAddIn.Model, ThisAddIn.ApiKey, ThisAddIn.ClientOptions);
ChatClient client = new ChatClient(ThisAddIn.Model, new ApiKeyCredential(ThisAddIn.ApiKey), ThisAddIn.ClientOptions);
var streamingAnswer = client.CompleteChatStreamingAsync(
new List<ChatMessage>() { new SystemChatMessage(systemPrompt), new UserChatMessage(@$"{userPrompt}: {range.Text}") },
new ChatCompletionOptions() { MaxTokens = ThisAddIn.ContextLength },
new ChatCompletionOptions() { MaxOutputTokenCount = ThisAddIn.ContextLength },
ThisAddIn.CancellationTokenSource.Token
);

range.Delete();
await AddStreamingContentToRange(streamingAnswer, range);
try
{
await AddStreamingContentToRange(streamingAnswer, range);
} catch (OperationCanceledException ex)
{
CommonUtils.DisplayWarning(ex);
}
Globals.ThisAddIn.Application.Selection.SetRange(range.Start, range.End);
}

Expand Down
14 changes: 8 additions & 6 deletions GenerateUserControl.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using OpenAI.Chat;
using static System.Net.Mime.MediaTypeNames;

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

/*
* So, If the user changes the selection carot in Word after clicking "generate" (bc it takes so long to generate text).
Expand All @@ -48,13 +46,13 @@ private async void GenerateButton_Click(object sender, EventArgs e)
await Forge.AddStreamingContentToRange(streamingAnswer, rangeBeforeChat);
Globals.ThisAddIn.Application.Selection.SetRange(rangeBeforeChat.Start, rangeBeforeChat.End);
}
catch (ArgumentException ex)
catch (TextBoxEmptyException ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
CommonUtils.DisplayInformation(ex);
}
catch (OperationCanceledException ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
CommonUtils.DisplayWarning(ex);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -112,6 +110,10 @@ private void PromptTextBox_KeyDown(object sender, KeyEventArgs e)
CommonUtils.DisplayError(ex);
}
}
}

public class TextBoxEmptyException : ArgumentException
{
public TextBoxEmptyException(string message) : base(message) { }
}
}
13 changes: 9 additions & 4 deletions ModelProperties.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAI.Models;

namespace TextForge
{
internal class ModelProperties
{
// Public
public const int BaselineContextWindowLength = 4096; // Change this if necessary
public static List<string> UniqueEmbedModels { get { return _embedModels; } }

// Private
Expand Down Expand Up @@ -140,6 +139,12 @@ internal class ModelProperties
// new models
{ "minicpm-v", 32768 },
{ "reader-lm", 256000 },
{ "mistral-small", 131072 },
{ "bespoke-minicheck", 32768 },
{ "qwen2.5", 32768 },
{ "nemotron-mini", 4096 },
{ "solar-pro", 4096 },
{ "qwen2.5-coder", 32768 }
};

public static int GetContextLength(string modelName)
Expand All @@ -151,7 +156,7 @@ public static int GetContextLength(string modelName)
else if (modelName.Contains(':'))
{
string key = modelName.Split(':')[0];
return ollamaModelsContextLength.ContainsKey(key) ? ollamaModelsContextLength[key] : ThisAddIn.BaselineContextWindowLength;
return ollamaModelsContextLength.ContainsKey(key) ? ollamaModelsContextLength[key] : BaselineContextWindowLength;
}
else if (modelName.StartsWith("o1"))
{
Expand All @@ -175,7 +180,7 @@ public static int GetContextLength(string modelName)
}
else
{
return ThisAddIn.BaselineContextWindowLength;
return BaselineContextWindowLength;
}
}

Expand Down
Loading

0 comments on commit cc3fbb9

Please sign in to comment.