Skip to content

Commit

Permalink
fix: together.ai and Open Router code generator (#173)
Browse files Browse the repository at this point in the history
* fix: together.ai and deepinfra code generator

* fix: Open Router Example

* feat: Added Embedding Models for DeepInfra and Together.ai

* removed Predefined Models generation.

---------

Co-authored-by: Gunpal Jain <gunpal5@hotmail.com>
  • Loading branch information
gunpal5 and Gunpal Jain authored Mar 3, 2025
1 parent 9c36c6c commit c526175
Show file tree
Hide file tree
Showing 25 changed files with 6,931 additions and 3,610 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
OpenRouterConfiguration configuration = new();
string apiKey = Environment.GetEnvironmentVariable("OPENROUTER_APIKEY") ?? throw new ArgumentNullException("no ApiKey is present");
OpenRouterProvider provider = new(apiKey);
OpenRouterModel model = new OpenRouterModel(provider, OpenRouterModelIds.Phi3MediumInstructFree);
OpenRouterModel model = new OpenRouterModel(provider, OpenRouterModelIds.MoonshotAiMoonlight16BA3bInstructFree);


Console.WriteLine("Phi:");
Expand Down
13 changes: 13 additions & 0 deletions src/DeepInfra/src/DeepInfraEmbeddingModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using LangChain.Providers.OpenAI;

namespace LangChain.Providers.DeepInfra;

public class DeepInfraEmbeddingModel(DeepInfraProvider provider, string id)
: OpenAiEmbeddingModel(provider, id)
{
public DeepInfraEmbeddingModel(DeepInfraProvider provider,
DeepInfraModelIds id) : this(provider,
DeepInfraModelProvider.GetModelById(id).Id ?? throw new InvalidOperationException("Model not found"))
{
}
}
1,782 changes: 990 additions & 792 deletions src/DeepInfra/src/DeepInfraModelIds.cs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/DeepInfra/src/DeepInfraModelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class DeepInfraModelProvider
{
private static Dictionary<DeepInfraModelIds, ChatModelMetadata> Models { get; set; } = new()
{
{ DeepInfraModelIds.DeepseekR1Turbo, ToMetadata("deepseek-ai/DeepSeek-R1-Turbo",32768,2E-06,6E-06)},
{ DeepInfraModelIds.DeepseekR1, ToMetadata("deepseek-ai/DeepSeek-R1",65536,7.5E-07,2.4E-06)},
{ DeepInfraModelIds.DeepseekR1DistillLlama70B, ToMetadata("deepseek-ai/DeepSeek-R1-Distill-Llama-70B",131072,2.3000000000000002E-07,6.9E-07)},
{ DeepInfraModelIds.DeepseekV3, ToMetadata("deepseek-ai/DeepSeek-V3",65536,4.9E-07,8.900000000000001E-07)},
Expand Down Expand Up @@ -81,6 +82,23 @@ public static class DeepInfraModelProvider
{ DeepInfraModelIds.MinicpmLlama3V25, ToMetadata("openbmb/MiniCPM-Llama3-V-2_5",8192,3.4000000000000003E-07,3.4000000000000003E-07)},
{ DeepInfraModelIds.OpenChat368B, ToMetadata("openchat/openchat-3.6-8b",8192,6E-08,6E-08)},
{ DeepInfraModelIds.OpenChat35, ToMetadata("openchat/openchat_3.5",8192,6E-08,6E-08)},
{ DeepInfraModelIds.BgeBaseEnV15, ToMetadata("BAAI/bge-base-en-v1.5",512,0,0)},
{ DeepInfraModelIds.BgeEnIcl, ToMetadata("BAAI/bge-en-icl",8192,1E-08,0)},
{ DeepInfraModelIds.BgeLargeEnV15, ToMetadata("BAAI/bge-large-en-v1.5",512,1E-08,0)},
{ DeepInfraModelIds.BgeM3, ToMetadata("BAAI/bge-m3",8192,1E-08,0)},
{ DeepInfraModelIds.E5BaseV2, ToMetadata("intfloat/e5-base-v2",512,0,0)},
{ DeepInfraModelIds.E5LargeV2, ToMetadata("intfloat/e5-large-v2",512,1E-08,0)},
{ DeepInfraModelIds.MultilingualE5Large, ToMetadata("intfloat/multilingual-e5-large",512,1E-08,0)},
{ DeepInfraModelIds.AllMinilmL12V2, ToMetadata("sentence-transformers/all-MiniLM-L12-v2",512,0,0)},
{ DeepInfraModelIds.AllMinilmL6V2, ToMetadata("sentence-transformers/all-MiniLM-L6-v2",512,0,0)},
{ DeepInfraModelIds.AllMpnetBaseV2, ToMetadata("sentence-transformers/all-mpnet-base-v2",512,0,0)},
{ DeepInfraModelIds.ClipVitB32, ToMetadata("sentence-transformers/clip-ViT-B-32",77,0,0)},
{ DeepInfraModelIds.ClipVitB32MultilingualV1, ToMetadata("sentence-transformers/clip-ViT-B-32-multilingual-v1",512,0,0)},
{ DeepInfraModelIds.MultiQaMpnetBaseDotV1, ToMetadata("sentence-transformers/multi-qa-mpnet-base-dot-v1",512,0,0)},
{ DeepInfraModelIds.ParaphraseMinilmL6V2, ToMetadata("sentence-transformers/paraphrase-MiniLM-L6-v2",512,0,0)},
{ DeepInfraModelIds.Text2vecBaseChinese, ToMetadata("shibing624/text2vec-base-chinese",512,0,0)},
{ DeepInfraModelIds.GteBase, ToMetadata("thenlper/gte-base",512,0,0)},
{ DeepInfraModelIds.GteLarge, ToMetadata("thenlper/gte-large",512,1E-08,0)},

};

Expand Down
362 changes: 0 additions & 362 deletions src/DeepInfra/src/Predefined/AllModels.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ internal sealed class ModelInfo
public string? PredefinedClassCode { get; set; }
public string? EnumMemberName { get; set; }
public string? EnumMemberCode { get; set; }
public ModelType ModelType { get; set; } = ModelType.Text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace LangChain.Providers.DeepInfra.CodeGenerator.Classes;

public enum ModelType
{
Text,
Embedding,
Image
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,98 @@ public static async Task GenerateCodesAsync(GenerationOptions options)
Console.WriteLine("Loading Models...");
var models = await GetModelsAsync(options).ConfigureAwait(false);

Console.WriteLine("Parsing Embedding Models...");

var embeddingModels = await GetEmbeddingModelsAsync(options).ConfigureAwait(false);

Console.WriteLine($"{models.Count} Models Found...");

Console.WriteLine($"{models.Count+embeddingModels.Count} Models Found...");

//Sort Models by index
var sorted = models.OrderBy(s => s.Index).ToList();

//Create AllModels.cs
Console.WriteLine("Creating AllModels.cs...");
await CreateAllModelsFile(sorted, options.OutputFolder).ConfigureAwait(false);
// Console.WriteLine("Creating AllModels.cs...");
// await CreateAllModelsFile(sorted, options.OutputFolder).ConfigureAwait(false);

//Create AllEmbeddingModels.cs
//Console.WriteLine("Creating AllEmbeddingModels.cs...");
//await CreateAllEmbeddingModelsFile(embeddingModels, options.OutputFolder).ConfigureAwait(false);

sorted.AddRange(embeddingModels);

//Create DeepInfraModelIds.cs
Console.WriteLine("Creating DeepInfraModelIds.cs...");
await CreateDeepInfraModelIdsFile(sorted, options.OutputFolder).ConfigureAwait(false);


//Create DeepInfraModelIds.cs
Console.WriteLine("Creating DeepInfraModelProvider.cs...");
await CreateDeepInfraModelProviderFile(sorted, options.OutputFolder).ConfigureAwait(false);

Console.WriteLine("Task Completed!");
}

private static async Task CreateAllEmbeddingModelsFile(List<ModelInfo> sorted, string outputFolder)
{
var sb3 = new StringBuilder();
foreach (var item in sorted)
{
sb3.AppendLine(item.PredefinedClassCode);
sb3.AppendLine();
}

var classesFileContent =
H.Resources.AllModels_cs.AsString().Replace("{{DeepInfraClasses}}", sb3.ToString(), StringComparison.InvariantCulture);
var path1 = Path.Join(outputFolder, "Predefined");
Directory.CreateDirectory(path1);
var fileName = Path.Combine(path1, "AllEmbeddingModels.cs");
await File.WriteAllTextAsync(fileName, classesFileContent).ConfigureAwait(false);
Console.WriteLine($"Saved to {fileName}");
}

private static async Task<List<ModelInfo>> GetEmbeddingModelsAsync(GenerationOptions options)
{
var str = await GetStringAsync(new Uri("https://deepinfra.com/models/embeddings")).ConfigureAwait(false);
var lbb = new DocumentHelper();
var list = new List<ModelInfo>();
int index = 0;
//Parse Html
var hashSet = new HashSet<string?>();
do
{
lbb.DocumentText = str ?? string.Empty;
var links = lbb.FindNode("script", "type", "json", true);

if (links == null)
throw new InvalidOperationException("Model Info script node not found in the HTML document.");

var json = JObject.Parse(links.InnerText);

var models = (JArray)json.SelectToken("props.pageProps.models")!;

foreach (var model in models)
{
var modelInfo = ParseModelInfo(index, model, options, ModelType.Embedding);
if (modelInfo != null && hashSet.Add(modelInfo.EnumMemberCode))
{
list.Add(modelInfo);
}
}

var nextPage = lbb.FindNode("a", "aria-label", "next page", true);
if (nextPage != null)
{
str = await GetStringAsync(new Uri($"https://deepinfra.com{nextPage.GetAttributeValue("href", "")}"))
.ConfigureAwait(false);
if (string.IsNullOrEmpty(str))
break;
}
else break;

} while (true);
return list;
}

private static async Task<List<ModelInfo>> GetModelsAsync(GenerationOptions options)
{
var str = await GetStringAsync(new Uri("https://deepinfra.com/models/text-generation")).ConfigureAwait(false);
Expand All @@ -74,7 +144,7 @@ private static async Task<List<ModelInfo>> GetModelsAsync(GenerationOptions opti

foreach (var model in models)
{
var modelInfo = ParseModelInfo(index, model, options);
var modelInfo = ParseModelInfo(index, model, options, ModelType.Text);
if (modelInfo != null && hashSet.Add(modelInfo.EnumMemberCode))
{
list.Add(modelInfo);
Expand Down Expand Up @@ -190,7 +260,7 @@ private static async Task CreateAllModelsFile(List<ModelInfo> sorted, string out
/// <param name="modelToken"></param>
/// <param name="options"></param>
/// <returns></returns>
private static ModelInfo? ParseModelInfo(int i, JToken? modelToken, GenerationOptions options)
private static ModelInfo? ParseModelInfo(int i, JToken? modelToken, GenerationOptions options, ModelType modelType)
{
if (modelToken == null)
return null;
Expand Down Expand Up @@ -226,7 +296,7 @@ private static async Task CreateAllModelsFile(List<ModelInfo> sorted, string out
completionCost / (1000 * 1000));

//Code for Predefined Model Class
var predefinedClassCode = GetPreDefinedClassCode(enumMemberName);
var predefinedClassCode = GetPreDefinedClassCode(enumMemberName, modelType);

return new ModelInfo
{
Expand All @@ -237,7 +307,8 @@ private static async Task CreateAllModelsFile(List<ModelInfo> sorted, string out
ModelName = modelName,
PredefinedClassCode = predefinedClassCode,
EnumMemberCode = enumMemberCode,
Description = description
Description = description,
ModelType = modelType
};
}

Expand All @@ -251,13 +322,20 @@ private static string GetEnumMemberCode(string enumMemberName, string descriptio
return sb2.ToString();
}

private static string GetPreDefinedClassCode(string enumMemberName)
private static string GetPreDefinedClassCode(string enumMemberName, ModelType modelType)
{
var sb = new StringBuilder();
sb.AppendLine(
$"/// <inheritdoc cref=\"DeepInfraModelIds.{enumMemberName}\"/>\r\n/// <param name=\"provider\">Deep Infra Provider Instance</param>");
sb.AppendLine(
$"public class {enumMemberName.Replace("_", "", StringComparison.OrdinalIgnoreCase)}Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.{enumMemberName});");

if(modelType == ModelType.Text)
sb.AppendLine(
$"public class {enumMemberName.Replace("_", "", StringComparison.OrdinalIgnoreCase)}Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.{enumMemberName});");
else if (modelType == ModelType.Embedding)
{
sb.AppendLine(
$"public class {enumMemberName.Replace("_", "", StringComparison.OrdinalIgnoreCase)}EmbeddingModel(DeepInfraProvider provider) : DeepInfraEmbeddingModel(provider, DeepInfraModelIds.{enumMemberName});");
}
return sb.ToString();
}

Expand Down
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.2" />
<PackageVersion Include="Together" Version="0.9.0" />
<PackageVersion Include="Verify.MSTest" Version="27.0.1" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
112 changes: 1 addition & 111 deletions src/IntegrationTests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,115 +262,5 @@ public async Task Tools_Books(ProviderType providerType)
Console.WriteLine(response.Messages.AsHistory());
}

[Explicit]
public async Task GoogleEmbeddingTest()
{
var (llm, embeddingModel, _) = Helpers.GetModels(ProviderType.Google);

var embeddings = await embeddingModel.CreateEmbeddingsAsync(new EmbeddingRequest()
{
Strings = new List<string>
{
"The quick brown fox jumps over the lazy dog.",
"She sells seashells by the seashore.",
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
"A journey of a thousand miles begins with a single step.",
"To be or not to be, that is the question.",
"All that glitters is not gold.",
"A picture is worth a thousand words.",
"Actions speak louder than words.",
"Better late than never.",
"The early bird catches the worm.",
"Fortune favors the bold.",
"Ignorance is bliss.",
"Look before you leap.",
"Barking up the wrong tree.",
"Birds of a feather flock together.",
"Don't bite the hand that feeds you.",
"Every cloud has a silver lining.",
"Haste makes waste.",
"Strike while the iron is hot.",
"Time and tide wait for no man.",
"When in Rome, do as the Romans do.",
"Where there's smoke, there's fire.",
"You can't judge a book by its cover.",
"Practice makes perfect.",
"Rome wasn't built in a day.",
"Two heads are better than one.",
"A rolling stone gathers no moss.",
"Absence makes the heart grow fonder.",
"Beauty is in the eye of the beholder.",
"Cleanliness is next to godliness.",
"Curiosity killed the cat.",
"Don't count your chickens before they hatch.",
"Honesty is the best policy.",
"Necessity is the mother of invention.",
"No man is an island.",
"The pen is mightier than the sword.",
"The squeaky wheel gets the grease.",
"There's no place like home.",
"Too many cooks spoil the broth.",
"You can't have your cake and eat it too.",
"A fool and his money are soon parted.",
"Actions have consequences.",
"An apple a day keeps the doctor away.",
"As you sow, so shall you reap.",
"Don't bite off more than you can chew.",
"Don't put all your eggs in one basket.",
"Early to bed and early to rise makes a man healthy, wealthy, and wise.",
"Give credit where credit is due.",
"Good things come to those who wait.",
"If it ain't broke, don't fix it.",
"If you can't beat 'em, join 'em.",
"It's always darkest before the dawn.",
"Keep your friends close and your enemies closer.",
"Knowledge is power.",
"Laughter is the best medicine.",
"Let sleeping dogs lie.",
"Out of sight, out of mind.",
"The grass is always greener on the other side of the fence.",
"The road to hell is paved with good intentions.",
"Variety is the spice of life.",
"What goes around comes around.",
"You reap what you sow.",
"Every rose has its thorn.",
"Don't judge a person until you've walked a mile in their shoes.",
"A watched pot never boils.",
"Better safe than sorry.",
"Beggars can't be choosers.",
"Cut your coat according to your cloth.",
"Don't make a mountain out of a molehill.",
"Every dog has its day.",
"Great minds think alike.",
"If it sounds too good to be true, it probably is.",
"Lend your money and lose your friend.",
"Keep your chin up.",
"Actions create reactions.",
"Don't throw the baby out with the bathwater.",
"A stitch in time saves nine.",
"Out of the frying pan and into the fire.",
"The customer is always right.",
"The proof of the pudding is in the eating.",
"There's no such thing as a free lunch.",
"This too shall pass.",
"To err is human; to forgive, divine.",
"You can't please everyone.",
"What doesn't kill you makes you stronger.",
"Don't cry over spilled milk.",
"Good fences make good neighbors.",
"Hope for the best but prepare for the worst.",
"It's no use crying over spilt milk.",
"Life is what you make it.",
"Live and let live.",
"Never look a gift horse in the mouth.",
"When the going gets tough, the tough get going.",
"You can't have it both ways.",
"You can't make an omelette without breaking eggs.",
"Necessity knows no law.",
"Failing to prepare is preparing to fail."
}
});
embeddings.Values.Should().HaveCountGreaterThan(0);
embeddings.Values.First().Should().HaveCountGreaterThan(0);
}

}
Loading

0 comments on commit c526175

Please sign in to comment.