Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
suncloudsmoon committed Oct 5, 2024
1 parent cc3fbb9 commit dd4c992
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 497 deletions.
3 changes: 3 additions & 0 deletions CommonUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
Expand All @@ -8,6 +9,8 @@ namespace TextForge
{
internal class CommonUtils
{
public static readonly HttpClient client = new HttpClient();

public static void DisplayError(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
Expand Down
55 changes: 28 additions & 27 deletions Forge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public partial class Forge

private CustomTaskPane _generateTaskPane;
private CustomTaskPane _ragControlTaskPane;

private static readonly object door = new object();

private void Forge_Load(object sender, RibbonUIEventArgs e)
{
Expand All @@ -33,11 +35,11 @@ private void Forge_Load(object sender, RibbonUIEventArgs e)
if (!ThisAddIn.IsAddinInitialized)
ThisAddIn.InitializeAddin();

List<string> models = new List<string>(ThisAddIn.ModelList);
List<string> modelList = new List<string>(ModelProperties.GetModelList(ThisAddIn.ModelList));

// Remove embedding models from the list
RemoveEmbeddingModels(ref models);
AddEmbeddingModelsToDropDownList(models);
modelList = RemoveEmbeddingModels(modelList).ToList();
AddEmbeddingModelsToDropDownList(modelList);

_box = new AboutBox();
_optionsBox = this.OptionsGroup;
Expand All @@ -50,33 +52,29 @@ private void Forge_Load(object sender, RibbonUIEventArgs e)
}
}

private void RemoveEmbeddingModels(ref List<string> modelList)
private IEnumerable<string> RemoveEmbeddingModels(IEnumerable<string> modelList)
{
var copyList = new List<string>(modelList);
foreach (var model in copyList)
{
foreach (var item in ModelProperties.UniqueEmbedModels)
if (model.Contains(item))
modelList.Remove(model);
if (model.Contains("embed"))
modelList.Remove(model);
}
return modelList
.Where(model => !ModelProperties.UniqueEmbedModels.Any(item => model.Contains(item)) && !model.Contains("embed"))
.ToList();
}

private void AddEmbeddingModelsToDropDownList(List<string> models)
private void AddEmbeddingModelsToDropDownList(IEnumerable<string> models)
{
var ribbonFactory = Globals.Factory.GetRibbonFactory();
foreach (string model in models)
var sortedModels = models.OrderBy(m => m).ToList();
foreach (string model in sortedModels)
{
var newItem = ribbonFactory.CreateRibbonDropDownItem();
newItem.Label = model;

ModelListDropDown.Items.Add(newItem);

if (model == ThisAddIn.Model)
{
ModelListDropDown.SelectedItem = newItem;
UpdateCheckbox();
var newItem = ribbonFactory.CreateRibbonDropDownItem();
newItem.Label = model;
ModelListDropDown.Items.Add(newItem);

if (model == ThisAddIn.Model)
{
ModelListDropDown.SelectedItem = newItem;
UpdateCheckbox();
}
}
}
}
Expand All @@ -92,13 +90,16 @@ private void GenerateButton_Click(object sender, RibbonControlEventArgs e)
}
}

private void ModelListDropDown_SelectionChanged(object sender, RibbonControlEventArgs e)
private async void ModelListDropDown_SelectionChanged(object sender, RibbonControlEventArgs e)
{
try
{
ThisAddIn.Model = GetSelectedItemLabel();
ThisAddIn.ContextLength = ModelProperties.GetContextLength(ThisAddIn.Model);
UpdateCheckbox();
await Task.Run(() =>
{
ThisAddIn.Model = GetSelectedItemLabel();
ThisAddIn.ContextLength = ModelProperties.GetContextLength(ThisAddIn.Model, ThisAddIn.ModelList);
UpdateCheckbox();
});
}
catch (Exception ex)
{
Expand Down
192 changes: 68 additions & 124 deletions ModelProperties.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Text.Json;
using OpenAI.Models;
using System.Diagnostics;

namespace TextForge
{
Expand All @@ -27,136 +30,43 @@ internal class ModelProperties
{ "gpt-3.5-turbo-instruct", 4096 },
};

private static readonly Dictionary<string, int> ollamaModelsContextLength = new Dictionary<string, int>()
{
{ "llama3.1", 131072 },
{ "gemma2", 8192 },
{ "mistral-nemo", 1024000 },
{ "mistral-large", 32768 },
{ "qwen2", 32768 },
{ "deepseek-coder-v2", 163840 },
{ "phi3", 4096 }, // the lowest config of phi3 (https://ollama.com/library/phi3:medium-4k/blobs/0d98d611d31b)
{ "mistral", 32768 },
{ "mixtral", 32768 },
{ "codegemma", 8192 },
{ "command-r", 131072 },
{ "command-r-plus", 131072 },
{ "llava", 4096 }, // https://ollama.com/library/llava:13b-v1.6-vicuna-q4_0/blobs/87d5b13e5157
{ "llama3", 8192 },
{ "gemma", 8192 },
{ "qwen", 32768 },
{ "llama2", 4096 },
{ "codellama", 16384 },
{ "dolphin-mixtral", 32768 },
{ "phi", 2048 },
{ "llama2-uncensored", 2048 },
{ "deepseek-coder", 16384 },
{ "dolphin-mistral", 32768 },
{ "zephyr", 32768 },
{ "starcoder2", 16384 },
{ "dolphin-llama3", 8192 },
{ "orca-mini", 2048 },
{ "yi", 4096 },
{ "mistral-openorca", 32768 },
{ "llava-llama3", 8192 },
{ "starcoder", 8192 },
{ "llama2-chinese", 4096 },
{ "vicuna", 2048 }, // https://ollama.com/library/vicuna:7b-q3_K_S/blobs/a887a7755a1e
{ "tinyllama", 2048 },
{ "codestral", 32768 },
{ "wizard-vicuna-uncensored", 2048 },
{ "nous-hermes2", 4096 },
{ "openchat", 8192 },
{ "aya", 8192 },
{ "granite-code", 2048 },
{ "wizardlm2", 32768 },
{ "tinydolphin", 4096 },
{ "wizardcoder", 16384 },
{ "stable-code", 16384 },
{ "openhermes", 32768 },
{ "codeqwen", 65536 },
{ "codegeex4", 131072 },
{ "stablelm2", 4096 },
{ "wizard-math", 2048 }, // https://ollama.com/library/wizard-math:7b-q5_1/blobs/b39818bfe610
{ "qwen2-math", 4096 },
{ "neural-chat", 32768 },
{ "llama3-gradient", 1048576 },
{ "phind-codellama", 16384 },
{ "nous-hermes", 2048 }, // https://ollama.com/library/nous-hermes:13b-q4_1/blobs/433b4abded9b
{ "sqlcoder", 8192 }, // https://ollama.com/library/sqlcoder:15b-q8_0/blobs/2319eec9425f
{ "dolphincoder", 16384 },
{ "xwinlm", 4096 },
{ "deepseek-llm", 4096 },
{ "yarn-llama2", 65536 },
{ "llama3-chatqa", 8192 },
{ "wizardlm", 2048 },
{ "starling-lm", 8192 },
{ "falcon", 2048 },
{ "orca2", 4096 },
{ "moondream", 2048 },
{ "samantha-mistral", 32768 },
{ "solar", 4096 },
{ "smollm", 2048 },
{ "stable-beluga", 4096 },
{ "dolphin-phi", 2048 },
{ "deepseek-v2", 163840 },
{ "glm4", 8192 }, // https://ollama.com/library/glm4:9b-text-q6_K/blobs/2f657a57a8df
{ "phi3.5", 131072 },
{ "bakllava", 32768 },
{ "wizardlm-uncensored", 4096 },
{ "yarn-mistral", 32768 }, // https://ollama.com/library/yarn-mistral/blobs/0e8703041ff2
{ "medllama2", 4096 },
{ "llama-pro", 4096 },
{ "llava-phi3", 4096 },
{ "meditron", 2048 },
{ "nous-hermes2-mixtral", 32768 },
{ "nexusraven", 16384 },
{ "hermes3", 131072 },
{ "codeup", 4096 },
{ "everythinglm", 16384 },
{ "internlm2", 32768 },
{ "magicoder", 16384 },
{ "stablelm-zephyr", 4096 },
{ "codebooga", 16384 },
{ "yi-coder", 131072 },
{ "mistrallite", 32768 },
{ "llama3-groq-tool-use", 8192 },
{ "falcon2", 2048 },
{ "wizard-vicuna", 2048 },
{ "duckdb-nsql", 16384 },
{ "megadolphin", 4096 },
{ "reflection", 8192 },
{ "notux", 32768 },
{ "goliath", 4096 },
{ "open-orca-platypus2", 4096 },
{ "notus", 32768 },
{ "dbrx", 32768 },
{ "mathstral", 32768 },
{ "alfred", 2048 },
{ "nuextract", 4096 },
{ "firefunction-v2", 8192 },
{ "deepseek-v2.5", 163840 },
// 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 }
};
private static bool IsOllamaEndpoint = false;
private static bool IsOllamaFetched = false;
private static Dictionary<string, int> ollamaContextWindowCache = new Dictionary<string, int>();

public static int GetContextLength(string modelName)
public static int GetContextLength(string modelName, OpenAIModelCollection availableModels)
{
if (openAIModelsContextLength.ContainsKey(modelName))
{
return openAIModelsContextLength[modelName];
}
else if (modelName.Contains(':'))
{
string key = modelName.Split(':')[0];
return ollamaModelsContextLength.ContainsKey(key) ? ollamaModelsContextLength[key] : BaselineContextWindowLength;
try
{
if (!IsOllamaFetched)
{
IsOllamaEndpoint = IsOllama(availableModels);
IsOllamaFetched = true;
}
if (IsOllamaEndpoint)
{
int contextWindow;
if (!ollamaContextWindowCache.TryGetValue(modelName, out contextWindow))
{
contextWindow = GetOllamaModelContextWindow(modelName);
ollamaContextWindowCache[modelName] = contextWindow;
}
return contextWindow;
} else
{
return BaselineContextWindowLength;
}
} catch (OllamaMissingContextWindowException ex)
{
CommonUtils.DisplayWarning(ex);
return BaselineContextWindowLength;
}
}
else if (modelName.StartsWith("o1"))
{
Expand Down Expand Up @@ -184,10 +94,44 @@ public static int GetContextLength(string modelName)
}
}

public static IEnumerable<string> GetModelList(ModelClient client)
public static IEnumerable<string> GetModelList(OpenAIModelCollection availableModels)
{
OpenAIModelInfoCollection availableModels = client.GetModels().Value;
return availableModels.Select(info => info.Id).ToList();
}

private static bool IsOllama(OpenAIModelCollection availableModels)
{
return (availableModels.Count == 0) ? false : availableModels.First().OwnedBy == "library";
}

private static int GetOllamaModelContextWindow(string model)
{
var ollamaEndpoint = ThisAddIn.OpenAIEndpoint.Replace("/v1", "");

Ollama ollamaInstance = new Ollama(new Uri(ollamaEndpoint));
var dict = ollamaInstance.Show(model, true).Result; // or await, if Show() is async

// Navigate to "model_info"
if (dict.TryGetValue("model_info", out var modelInfoObj) && modelInfoObj is JsonElement modelInfoElement)
{
// Use JsonNode or JsonElement to search for "context_length" key
var modelInfoNode = JsonNode.Parse(modelInfoElement.GetRawText());

foreach (var keyValuePair in modelInfoNode.AsObject())
{
// Search for a nested object containing "context_length"
if (keyValuePair.Key.EndsWith(".context_length"))
{
return int.Parse(keyValuePair.Value.ToString());
}
}
}
throw new OllamaMissingContextWindowException($"Unable to fetch the context length for {model}!");
}
}

public class OllamaMissingContextWindowException : ApplicationException
{
public OllamaMissingContextWindowException(string message) : base(message) { }
}
}
Loading

0 comments on commit dd4c992

Please sign in to comment.