From 687c0e215045084fa602c8813047bf1a151c1dac Mon Sep 17 00:00:00 2001 From: Gunpal Jain Date: Sun, 28 Apr 2024 02:15:48 +0530 Subject: [PATCH] feat: Added Deep Infra Provider --- LangChain.sln | 28 +- .../DeepInfra/src/DeepInfraConfiguration.cs | 14 + src/Providers/DeepInfra/src/DeepInfraModel.cs | 24 ++ .../DeepInfra/src/DeepInfraModelIds.cs | 306 ++++++++++++++ .../DeepInfra/src/DeepInfraModelProvider.cs | 51 +++ .../DeepInfra/src/DeepInfraProvider.cs | 16 + .../src/LangChain.Providers.DeepInfra.csproj | 22 + .../DeepInfra/src/Predefined/AllModels.cs | 137 ++++++ .../DeepInfra/tests/DeepInfraTests.cs | 37 ++ ...LangChain.Providers.DeepInfra.Tests.csproj | 11 + src/Providers/DeepInfra/tests/Tests.cs | 32 ++ .../DeepInfra/tests/WeatherFunctions.cs | 58 +++ .../Classes/ModelInfo.cs | 13 + .../Helpers/DocumentHelper.cs | 66 +++ ...n.Providers.DeepInfra.CodeGenerator.csproj | 27 ++ .../Main/DeepInfraCodeGenerator.cs | 396 ++++++++++++++++++ .../Main/GenerationOptions.cs | 7 + .../Program.cs | 62 +++ .../Properties/launchSettings.json | 8 + .../Resources/AllModels.cs | 3 + .../Resources/DeepInfraModelIds.cs | 9 + .../Resources/DeepInfraModelProvider.cs | 24 ++ .../src/LangChain.Providers.OpenAI.csproj | 8 +- src/Providers/OpenAI/src/OpenAiProvider.cs | 4 +- 24 files changed, 1355 insertions(+), 8 deletions(-) create mode 100644 src/Providers/DeepInfra/src/DeepInfraConfiguration.cs create mode 100644 src/Providers/DeepInfra/src/DeepInfraModel.cs create mode 100644 src/Providers/DeepInfra/src/DeepInfraModelIds.cs create mode 100644 src/Providers/DeepInfra/src/DeepInfraModelProvider.cs create mode 100644 src/Providers/DeepInfra/src/DeepInfraProvider.cs create mode 100644 src/Providers/DeepInfra/src/LangChain.Providers.DeepInfra.csproj create mode 100644 src/Providers/DeepInfra/src/Predefined/AllModels.cs create mode 100644 src/Providers/DeepInfra/tests/DeepInfraTests.cs create mode 100644 src/Providers/DeepInfra/tests/LangChain.Providers.DeepInfra.Tests.csproj create mode 100644 src/Providers/DeepInfra/tests/Tests.cs create mode 100644 src/Providers/DeepInfra/tests/WeatherFunctions.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Classes/ModelInfo.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Helpers/DocumentHelper.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/LangChain.Providers.DeepInfra.CodeGenerator.csproj create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/DeepInfraCodeGenerator.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/GenerationOptions.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Program.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Properties/launchSettings.json create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/AllModels.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelIds.cs create mode 100644 src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelProvider.cs diff --git a/LangChain.sln b/LangChain.sln index 45d0638f..1e4d4b6e 100644 --- a/LangChain.sln +++ b/LangChain.sln @@ -346,9 +346,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Togethe EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mongo", "Mongo", "{07C5311E-E2E7-4F53-B837-A72196C54D8B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Databases.Mongo", "src\Databases\Mongo\src\LangChain.Databases.Mongo.csproj", "{F4488CA6-9648-4467-862E-3C4A3CAAF5B4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Databases.Mongo", "src\Databases\Mongo\src\LangChain.Databases.Mongo.csproj", "{F4488CA6-9648-4467-862E-3C4A3CAAF5B4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Databases.Mongo.IntegrationTests", "src\Databases\Mongo\test\LangChain.Databases.Mongo.IntegrationTests.csproj", "{E975CF2E-0D83-417B-A859-07BC028047D0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Databases.Mongo.IntegrationTests", "src\Databases\Mongo\test\LangChain.Databases.Mongo.IntegrationTests.csproj", "{E975CF2E-0D83-417B-A859-07BC028047D0}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DeepInfra", "DeepInfra", "{97A6B334-EB72-4B7E-A4EA-2073604DD94B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.DeepInfra", "src\Providers\DeepInfra\src\LangChain.Providers.DeepInfra.csproj", "{EF25353F-C816-4197-8E33-623600AC5A4F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.DeepInfra.Tests", "src\Providers\DeepInfra\tests\LangChain.Providers.DeepInfra.Tests.csproj", "{B94544AB-1A3E-4604-A094-0D63C91AA6B2}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.DeepInfra.CodeGenerator", "src\Providers\DeepInfra\tools\LangChain.Providers.DeepInfra.CodeGenerator\LangChain.Providers.DeepInfra.CodeGenerator.csproj", "{44851579-7483-444F-BA01-E6D5427FFBFC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -720,6 +728,18 @@ Global {E975CF2E-0D83-417B-A859-07BC028047D0}.Debug|Any CPU.Build.0 = Debug|Any CPU {E975CF2E-0D83-417B-A859-07BC028047D0}.Release|Any CPU.ActiveCfg = Release|Any CPU {E975CF2E-0D83-417B-A859-07BC028047D0}.Release|Any CPU.Build.0 = Release|Any CPU + {EF25353F-C816-4197-8E33-623600AC5A4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF25353F-C816-4197-8E33-623600AC5A4F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF25353F-C816-4197-8E33-623600AC5A4F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF25353F-C816-4197-8E33-623600AC5A4F}.Release|Any CPU.Build.0 = Release|Any CPU + {B94544AB-1A3E-4604-A094-0D63C91AA6B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B94544AB-1A3E-4604-A094-0D63C91AA6B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B94544AB-1A3E-4604-A094-0D63C91AA6B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B94544AB-1A3E-4604-A094-0D63C91AA6B2}.Release|Any CPU.Build.0 = Release|Any CPU + {44851579-7483-444F-BA01-E6D5427FFBFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44851579-7483-444F-BA01-E6D5427FFBFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44851579-7483-444F-BA01-E6D5427FFBFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44851579-7483-444F-BA01-E6D5427FFBFC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -860,6 +880,10 @@ Global {07C5311E-E2E7-4F53-B837-A72196C54D8B} = {A098FF69-D8B5-4B2B-83D5-F777D3817F15} {F4488CA6-9648-4467-862E-3C4A3CAAF5B4} = {07C5311E-E2E7-4F53-B837-A72196C54D8B} {E975CF2E-0D83-417B-A859-07BC028047D0} = {07C5311E-E2E7-4F53-B837-A72196C54D8B} + {97A6B334-EB72-4B7E-A4EA-2073604DD94B} = {E2B9833C-0397-4FAF-A3A8-116E58749750} + {EF25353F-C816-4197-8E33-623600AC5A4F} = {97A6B334-EB72-4B7E-A4EA-2073604DD94B} + {B94544AB-1A3E-4604-A094-0D63C91AA6B2} = {97A6B334-EB72-4B7E-A4EA-2073604DD94B} + {44851579-7483-444F-BA01-E6D5427FFBFC} = {97A6B334-EB72-4B7E-A4EA-2073604DD94B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5C00D0F1-6138-4ED9-846B-97E43D6DFF1C} diff --git a/src/Providers/DeepInfra/src/DeepInfraConfiguration.cs b/src/Providers/DeepInfra/src/DeepInfraConfiguration.cs new file mode 100644 index 00000000..37f427d8 --- /dev/null +++ b/src/Providers/DeepInfra/src/DeepInfraConfiguration.cs @@ -0,0 +1,14 @@ +using LangChain.Providers.OpenAI; + +namespace LangChain.Providers.DeepInfra; + +/// +/// +/// +public class DeepInfraConfiguration : OpenAiConfiguration +{ + /// + /// + /// + public new const string SectionName = "DeepInfra"; +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/src/DeepInfraModel.cs b/src/Providers/DeepInfra/src/DeepInfraModel.cs new file mode 100644 index 00000000..829837e3 --- /dev/null +++ b/src/Providers/DeepInfra/src/DeepInfraModel.cs @@ -0,0 +1,24 @@ +using LangChain.Providers.OpenAI; +using OpenAI.Constants; + +namespace LangChain.Providers.DeepInfra; + +/// +/// +public class DeepInfraModel(DeepInfraProvider provider, ChatModels model) : OpenAiChatModel(provider, model) +{ + public DeepInfraModel(DeepInfraProvider provider, + DeepInfraModelIds id) : this(provider, DeepInfraModelProvider.GetModelById(id)) + { + } + + public DeepInfraModel(DeepInfraProvider provider, string id) : this(provider, new ChatModels( + id, + 0, + PricePerOutputTokenInUsd: 0.0, + PricePerInputTokenInUsd: 0.0)) + { + } + + +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/src/DeepInfraModelIds.cs b/src/Providers/DeepInfra/src/DeepInfraModelIds.cs new file mode 100644 index 00000000..00adf357 --- /dev/null +++ b/src/Providers/DeepInfra/src/DeepInfraModelIds.cs @@ -0,0 +1,306 @@ +namespace LangChain.Providers.DeepInfra; + +/// +/// List of all the Predefined DeepInfra Models +/// +public enum DeepInfraModelIds +{ + + /// + /// Name: Meta-Llama-3-70B-Instruct
+ /// Organization: meta-llama
+ /// Context Length: 8192
+ /// Prompt Cost: $0.59/MTok
+ /// Completion Cost: $0.59/MTok
+ /// Description: Model Details Meta developed and released the Meta Llama 3 family of large language models (LLMs), a collection of pretrained and instruction tuned generative text models in 8 and 70B sizes.
+ /// HuggingFace Url: https://huggingface.co/meta-llama/Meta-Llama-3-70B-Instruct + ///
+ MetaLlama370BInstruct, + + /// + /// Name: Meta-Llama-3-8B-Instruct
+ /// Organization: meta-llama
+ /// Context Length: 8192
+ /// Prompt Cost: $0.08/MTok
+ /// Completion Cost: $0.08/MTok
+ /// Description:
+ /// HuggingFace Url: https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct + ///
+ MetaLlama38BInstruct, + + /// + /// Name: Mixtral-8x22B-Instruct-v0.1
+ /// Organization: mistralai
+ /// Context Length: 65536
+ /// Prompt Cost: $0.65/MTok
+ /// Completion Cost: $0.65/MTok
+ /// Description: This is the instruction fine-tuned version of Mixtral-8x22B - the latest and largest mixture of experts large language model (LLM) from Mistral AI. This state of the art machine learning model uses a mixture 8 of experts (MoE) 22b models. During inference 2 experts are selected. This architecture allows large models to be fast and cheap at inference.
+ /// HuggingFace Url: https://huggingface.co/mistralai/Mixtral-8x22B-Instruct-v0.1 + ///
+ Mixtral8X22bInstructV01, + + /// + /// Name: WizardLM-2-8x22B
+ /// Organization: microsoft
+ /// Context Length: 65536
+ /// Prompt Cost: $0.65/MTok
+ /// Completion Cost: $0.65/MTok
+ /// Description: WizardLM-2 8x22B is Microsoft AI's most advanced Wizard model. It demonstrates highly competitive performance compared to those leading proprietary models.
+ /// HuggingFace Url: https://huggingface.co/microsoft/WizardLM-2-8x22B + ///
+ Wizardlm28X22b, + + /// + /// Name: WizardLM-2-7B
+ /// Organization: microsoft
+ /// Context Length: 32768
+ /// Prompt Cost: $0.07/MTok
+ /// Completion Cost: $0.07/MTok
+ /// Description: WizardLM-2 7B is the smaller variant of Microsoft AI's latest Wizard model. It is the fastest and achieves comparable performance with existing 10x larger open-source leading models
+ /// HuggingFace Url: https://huggingface.co/microsoft/WizardLM-2-7B + ///
+ Wizardlm27B, + + /// + /// Name: zephyr-orpo-141b-A35b-v0.1
+ /// Organization: HuggingFaceH4
+ /// Context Length: 65536
+ /// Prompt Cost: $0.65/MTok
+ /// Completion Cost: $0.65/MTok
+ /// Description: Zephyr 141B-A35B is an instruction-tuned (assistant) version of Mixtral-8x22B. It was fine-tuned on a mix of publicly available, synthetic datasets. It achieves strong performance on chat benchmarks.
+ /// HuggingFace Url: https://huggingface.co/HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1 + ///
+ ZephyrOrpo141BA35bV01, + + /// + /// Name: gemma-1.1-7b-it
+ /// Organization: google
+ /// Context Length: 8192
+ /// Prompt Cost: $0.07/MTok
+ /// Completion Cost: $0.07/MTok
+ /// Description: Gemma is an open-source model designed by Google. This is Gemma 1.1 7B (IT), an update over the original instruction-tuned Gemma release. Gemma 1.1 was trained using a novel RLHF method, leading to substantial gains on quality, coding capabilities, factuality, instruction following and multi-turn conversation quality.
+ /// HuggingFace Url: https://huggingface.co/google/gemma-1.1-7b-it + ///
+ Gemma117BIt, + + /// + /// Name: dbrx-instruct
+ /// Organization: databricks
+ /// Context Length: 32768
+ /// Prompt Cost: $0.6/MTok
+ /// Completion Cost: $0.6/MTok
+ /// Description: DBRX is an open source LLM created by Databricks. It uses mixture-of-experts (MoE) architecture with 132B total parameters of which 36B parameters are active on any input. It outperforms existing open source LLMs like Llama 2 70B and Mixtral-8x7B on standard industry benchmarks for language understanding, programming, math, and logic.
+ /// HuggingFace Url: https://huggingface.co/databricks/dbrx-instruct + ///
+ DbrxInstruct, + + /// + /// Name: Mixtral-8x7B-Instruct-v0.1
+ /// Organization: mistralai
+ /// Context Length: 32768
+ /// Prompt Cost: $0.24/MTok
+ /// Completion Cost: $0.24/MTok
+ /// Description: Mixtral is mixture of expert large language model (LLM) from Mistral AI. This is state of the art machine learning model using a mixture 8 of experts (MoE) 7b models. During inference 2 expers are selected. This architecture allows large models to be fast and cheap at inference. The Mixtral-8x7B outperforms Llama 2 70B on most benchmarks.
+ /// HuggingFace Url: https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1 + ///
+ Mixtral8X7BInstructV01, + + /// + /// Name: Mistral-7B-Instruct-v0.2
+ /// Organization: mistralai
+ /// Context Length: 32768
+ /// Prompt Cost: $0.07/MTok
+ /// Completion Cost: $0.07/MTok
+ /// Description: The Mistral-7B-Instruct-v0.2 Large Language Model (LLM) is a instruct fine-tuned version of the Mistral-7B-v0.2 generative text model using a variety of publicly available conversation datasets.
+ /// HuggingFace Url: https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2 + ///
+ Mistral7BInstructV02, + + /// + /// Name: Llama-2-70b-chat-hf
+ /// Organization: meta-llama
+ /// Context Length: 4096
+ /// Prompt Cost: $0.64/MTok
+ /// Completion Cost: $0.64/MTok
+ /// Description: LLaMa 2 is a collections of LLMs trained by Meta. This is the 70B chat optimized version. This endpoint has per token pricing.
+ /// HuggingFace Url: https://huggingface.co/meta-llama/Llama-2-70b-chat-hf + ///
+ Llama270BChatHf, + + /// + /// Name: dolphin-2.6-mixtral-8x7b
+ /// Organization: cognitivecomputations
+ /// Context Length: 32768
+ /// Prompt Cost: $0.24/MTok
+ /// Completion Cost: $0.24/MTok
+ /// Description: The Dolphin 2.6 Mixtral 8x7b model is a finetuned version of the Mixtral-8x7b model, trained on a variety of data including coding data, for 3 days on 4 A100 GPUs. It is uncensored and requires trust_remote_code. The model is very obedient and good at coding, but not DPO tuned. The dataset has been filtered for alignment and bias. The model is compliant with user requests and can be used for various purposes such as generating code or engaging in general chat.
+ /// HuggingFace Url: https://huggingface.co/cognitivecomputations/dolphin-2.6-mixtral-8x7b + ///
+ Dolphin26Mixtral8X7B, + + /// + /// Name: lzlv_70b_fp16_hf
+ /// Organization: lizpreciatior
+ /// Context Length: 4096
+ /// Prompt Cost: $0.59/MTok
+ /// Completion Cost: $0.59/MTok
+ /// Description: A Mythomax/MLewd_13B-style merge of selected 70B models A multi-model merge of several LLaMA2 70B finetunes for roleplaying and creative work. The goal was to create a model that combines creativity with intelligence for an enhanced experience.
+ /// HuggingFace Url: https://huggingface.co/lizpreciatior/lzlv_70b_fp16_hf + ///
+ Lzlv70BFp16Hf, + + /// + /// Name: openchat_3.5
+ /// Organization: openchat
+ /// Context Length: 8192
+ /// Prompt Cost: $0.1/MTok
+ /// Completion Cost: $0.1/MTok
+ /// Description: OpenChat is a library of open-source language models that have been fine-tuned with C-RLFT, a strategy inspired by offline reinforcement learning. These models can learn from mixed-quality data without preference labels and have achieved exceptional performance comparable to ChatGPT. The developers of OpenChat are dedicated to creating a high-performance, commercially viable, open-source large language model and are continuously making progress towards this goal.
+ /// HuggingFace Url: https://huggingface.co/openchat/openchat_3.5 + ///
+ OpenChat35, + + /// + /// Name: llava-1.5-7b-hf
+ /// Organization: llava-hf
+ /// Context Length: 4096
+ /// Prompt Cost: $0.34/MTok
+ /// Completion Cost: $0.34/MTok
+ /// Description: LLaVa is a multimodal model that supports vision and language models combined.
+ /// HuggingFace Url: https://huggingface.co/llava-hf/llava-1.5-7b-hf + ///
+ Llava157BHf, + + /// + /// Name: airoboros-70b
+ /// Organization: deepinfra
+ /// Context Length: 4096
+ /// Prompt Cost: $0.7/MTok
+ /// Completion Cost: $0.7/MTok
+ /// Description: Latest version of the Airoboros model fine-tunned version of llama-2-70b using the Airoboros dataset. This model is currently running jondurbin/airoboros-l2-70b-2.2.1
+ /// HuggingFace Url: https://huggingface.co/deepinfra/airoboros-70b + ///
+ Airoboros70B, + + /// + /// Name: Llama-2-7b-chat-hf
+ /// Organization: meta-llama
+ /// Context Length: 4096
+ /// Prompt Cost: $0.07/MTok
+ /// Completion Cost: $0.07/MTok
+ /// Description: Llama 2 is a collection of pretrained and fine-tuned generative text models ranging in scale from 7 billion to 70 billion parameters. This is the repository for the 7B fine-tuned model, optimized for dialogue use cases and converted for the Hugging Face Transformers format.
+ /// HuggingFace Url: https://huggingface.co/meta-llama/Llama-2-7b-chat-hf + ///
+ Llama27BChatHf, + + /// + /// Name: Yi-34B-Chat
+ /// Organization: 01-ai
+ /// Context Length: 4096
+ /// Prompt Cost: $0.6/MTok
+ /// Completion Cost: $0.6/MTok
+ /// Description:
+ /// HuggingFace Url: https://huggingface.co/01-ai/Yi-34B-Chat + ///
+ Yi34BChat, + + /// + /// Name: chronos-hermes-13b-v2
+ /// Organization: Austism
+ /// Context Length: 4096
+ /// Prompt Cost: $0.13/MTok
+ /// Completion Cost: $0.13/MTok
+ /// Description: This offers the imaginative writing style of chronos while still retaining coherency and being capable. Outputs are long and utilize exceptional prose. Supports a maxium context length of 4096. The model follows the Alpaca prompt format.
+ /// HuggingFace Url: https://huggingface.co/Austism/chronos-hermes-13b-v2 + ///
+ ChronosHermes13BV2, + + /// + /// Name: MythoMax-L2-13b
+ /// Organization: Gryphe
+ /// Context Length: 4096
+ /// Prompt Cost: $0.13/MTok
+ /// Completion Cost: $0.13/MTok
+ /// Description:
+ /// HuggingFace Url: https://huggingface.co/Gryphe/MythoMax-L2-13b + ///
+ MythomaxL213B, + + /// + /// Name: Phind-CodeLlama-34B-v2
+ /// Organization: Phind
+ /// Context Length: 4096
+ /// Prompt Cost: $0.6/MTok
+ /// Completion Cost: $0.6/MTok
+ /// Description: Phind-CodeLlama-34B-v2 is an open-source language model that has been fine-tuned on 1.5B tokens of high-quality programming-related data and achieved a pass@1 rate of 73.8% on HumanEval. It is multi-lingual and proficient in Python, C/C++, TypeScript, Java, and more. It has been trained on a proprietary dataset of instruction-answer pairs instead of code completion examples. The model is instruction-tuned on the Alpaca/Vicuna format to be steerable and easy-to-use. It accepts the Alpaca/Vicuna instruction format and can generate one completion for each prompt.
+ /// HuggingFace Url: https://huggingface.co/Phind/Phind-CodeLlama-34B-v2 + ///
+ PhindCodellama34BV2, + + /// + /// Name: starcoder2-15b
+ /// Organization: bigcode
+ /// Context Length: 16384
+ /// Prompt Cost: $0.4/MTok
+ /// Completion Cost: $0.4/MTok
+ /// Description: StarCoder2-15B model is a 15B parameter model trained on 600+ programming languages. It specializes in code completion.
+ /// HuggingFace Url: https://huggingface.co/bigcode/starcoder2-15b + ///
+ Starcoder215B, + + /// + /// Name: CodeLlama-34b-Instruct-hf
+ /// Organization: codellama
+ /// Context Length: 4096
+ /// Prompt Cost: $0.6/MTok
+ /// Completion Cost: $0.6/MTok
+ /// Description: Code Llama is a state-of-the-art LLM capable of generating code, and natural language about code, from both code and natural language prompts. This particular instance is the 34b instruct variant
+ /// HuggingFace Url: https://huggingface.co/codellama/CodeLlama-34b-Instruct-hf + ///
+ Codellama34BInstructHf, + + /// + /// Name: CodeLlama-70b-Instruct-hf
+ /// Organization: codellama
+ /// Context Length: 4096
+ /// Prompt Cost: $0.7/MTok
+ /// Completion Cost: $0.7/MTok
+ /// Description: CodeLlama-70b is the largest and latest code generation from the Code Llama collection.
+ /// HuggingFace Url: https://huggingface.co/codellama/CodeLlama-70b-Instruct-hf + ///
+ Codellama70BInstructHf, + + /// + /// Name: Llama-2-13b-chat-hf
+ /// Organization: meta-llama
+ /// Context Length: 4096
+ /// Prompt Cost: $0.13/MTok
+ /// Completion Cost: $0.13/MTok
+ /// Description: Llama 2 is a collection of pretrained and fine-tuned generative text models ranging in scale from 7 billion to 70 billion parameters. This is the repository for the 7B fine-tuned model, optimized for dialogue use cases and converted for the Hugging Face Transformers format.
+ /// HuggingFace Url: https://huggingface.co/meta-llama/Llama-2-13b-chat-hf + ///
+ Llama213BChatHf, + + /// + /// Name: Mistral-7B-Instruct-v0.1
+ /// Organization: mistralai
+ /// Context Length: 32768
+ /// Prompt Cost: $0.07/MTok
+ /// Completion Cost: $0.07/MTok
+ /// Description: The Mistral-7B-Instruct-v0.1 Large Language Model (LLM) is a instruct fine-tuned version of the Mistral-7B-v0.1 generative text model using a variety of publicly available conversation datasets.
+ /// HuggingFace Url: https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1 + ///
+ Mistral7BInstructV01, + + /// + /// Name: Mixtral-8x22B-v0.1
+ /// Organization: mistralai
+ /// Context Length: 65536
+ /// Prompt Cost: $0.65/MTok
+ /// Completion Cost: $0.65/MTok
+ /// Description: Mixtral-8x22B is the latest and largest mixture of expert large language model (LLM) from Mistral AI. This is state of the art machine learning model using a mixture 8 of experts (MoE) 22b models. During inference 2 expers are selected. This architecture allows large models to be fast and cheap at inference. This model is not instruction tuned.
+ /// HuggingFace Url: https://huggingface.co/mistralai/Mixtral-8x22B-v0.1 + ///
+ Mixtral8X22bV01, + +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/src/DeepInfraModelProvider.cs b/src/Providers/DeepInfra/src/DeepInfraModelProvider.cs new file mode 100644 index 00000000..65639f67 --- /dev/null +++ b/src/Providers/DeepInfra/src/DeepInfraModelProvider.cs @@ -0,0 +1,51 @@ +using OpenAI.Constants; + +namespace LangChain.Providers.DeepInfra; + +/// +/// Contains all the DeepInfra models. +/// +public static class DeepInfraModelProvider +{ + private static Dictionary Models { get; set; } = new() + { + { DeepInfraModelIds.MetaLlama370BInstruct, new ChatModels("meta-llama/Meta-Llama-3-70B-Instruct",8192,5.9E-07,7.900000000000001E-07)}, + { DeepInfraModelIds.MetaLlama38BInstruct, new ChatModels("meta-llama/Meta-Llama-3-8B-Instruct",8192,8E-08,8E-08)}, + { DeepInfraModelIds.Mixtral8X22bInstructV01, new ChatModels("mistralai/Mixtral-8x22B-Instruct-v0.1",65536,6.5E-07,6.5E-07)}, + { DeepInfraModelIds.Wizardlm28X22b, new ChatModels("microsoft/WizardLM-2-8x22B",65536,6.5E-07,6.5E-07)}, + { DeepInfraModelIds.Wizardlm27B, new ChatModels("microsoft/WizardLM-2-7B",32768,7E-08,7E-08)}, + { DeepInfraModelIds.ZephyrOrpo141BA35bV01, new ChatModels("HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1",65536,6.5E-07,6.5E-07)}, + { DeepInfraModelIds.Gemma117BIt, new ChatModels("google/gemma-1.1-7b-it",8192,7E-08,7E-08)}, + { DeepInfraModelIds.DbrxInstruct, new ChatModels("databricks/dbrx-instruct",32768,6E-07,6E-07)}, + { DeepInfraModelIds.Mixtral8X7BInstructV01, new ChatModels("mistralai/Mixtral-8x7B-Instruct-v0.1",32768,2.4E-07,2.4E-07)}, + { DeepInfraModelIds.Mistral7BInstructV02, new ChatModels("mistralai/Mistral-7B-Instruct-v0.2",32768,7E-08,7E-08)}, + { DeepInfraModelIds.Llama270BChatHf, new ChatModels("meta-llama/Llama-2-70b-chat-hf",4096,6.4E-07,8.000000000000001E-07)}, + { DeepInfraModelIds.Dolphin26Mixtral8X7B, new ChatModels("cognitivecomputations/dolphin-2.6-mixtral-8x7b",32768,2.4E-07,2.4E-07)}, + { DeepInfraModelIds.Lzlv70BFp16Hf, new ChatModels("lizpreciatior/lzlv_70b_fp16_hf",4096,5.9E-07,7.900000000000001E-07)}, + { DeepInfraModelIds.OpenChat35, new ChatModels("openchat/openchat_3.5",8192,1.0000000000000001E-07,1.0000000000000001E-07)}, + { DeepInfraModelIds.Llava157BHf, new ChatModels("llava-hf/llava-1.5-7b-hf",4096,3.4000000000000003E-07,3.4000000000000003E-07)}, + { DeepInfraModelIds.Airoboros70B, new ChatModels("deepinfra/airoboros-70b",4096,7E-07,9.000000000000001E-07)}, + { DeepInfraModelIds.Llama27BChatHf, new ChatModels("meta-llama/Llama-2-7b-chat-hf",4096,7E-08,7E-08)}, + { DeepInfraModelIds.Yi34BChat, new ChatModels("01-ai/Yi-34B-Chat",4096,6E-07,6E-07)}, + { DeepInfraModelIds.ChronosHermes13BV2, new ChatModels("Austism/chronos-hermes-13b-v2",4096,1.3E-07,1.3E-07)}, + { DeepInfraModelIds.MythomaxL213B, new ChatModels("Gryphe/MythoMax-L2-13b",4096,1.3E-07,1.3E-07)}, + { DeepInfraModelIds.PhindCodellama34BV2, new ChatModels("Phind/Phind-CodeLlama-34B-v2",4096,6E-07,6E-07)}, + { DeepInfraModelIds.Starcoder215B, new ChatModels("bigcode/starcoder2-15b",16384,4.0000000000000003E-07,4.0000000000000003E-07)}, + { DeepInfraModelIds.Codellama34BInstructHf, new ChatModels("codellama/CodeLlama-34b-Instruct-hf",4096,6E-07,6E-07)}, + { DeepInfraModelIds.Codellama70BInstructHf, new ChatModels("codellama/CodeLlama-70b-Instruct-hf",4096,7E-07,9.000000000000001E-07)}, + { DeepInfraModelIds.Llama213BChatHf, new ChatModels("meta-llama/Llama-2-13b-chat-hf",4096,1.3E-07,1.3E-07)}, + { DeepInfraModelIds.Mistral7BInstructV01, new ChatModels("mistralai/Mistral-7B-Instruct-v0.1",32768,7E-08,7E-08)}, + { DeepInfraModelIds.Mixtral8X22bV01, new ChatModels("mistralai/Mixtral-8x22B-v0.1",65536,6.5E-07,6.5E-07)}, + + }; + + public static ChatModels GetModelById(DeepInfraModelIds modelId) + { + if (Models.TryGetValue(modelId, out var id)) + { + return id; + } + + throw new ArgumentException($"Invalid Deep Infra Model {modelId}"); + } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/src/DeepInfraProvider.cs b/src/Providers/DeepInfra/src/DeepInfraProvider.cs new file mode 100644 index 00000000..f9ec027d --- /dev/null +++ b/src/Providers/DeepInfra/src/DeepInfraProvider.cs @@ -0,0 +1,16 @@ +using LangChain.Providers.OpenAI; +using OpenAI; + +namespace LangChain.Providers.DeepInfra; + +public class DeepInfraProvider : OpenAiProvider +{ + public DeepInfraProvider(DeepInfraConfiguration configuration) : base(configuration) + { + } + + public DeepInfraProvider(string apiKey) : base(apiKey, "api.deepinfra.com",apiVersion: "v1/openai") + { + + } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/src/LangChain.Providers.DeepInfra.csproj b/src/Providers/DeepInfra/src/LangChain.Providers.DeepInfra.csproj new file mode 100644 index 00000000..f581fcd4 --- /dev/null +++ b/src/Providers/DeepInfra/src/LangChain.Providers.DeepInfra.csproj @@ -0,0 +1,22 @@ + + + + net4.6.2;netstandard2.0;net6.0;net7.0;net8.0 + + + + + + + + Deep Infra provider. + $(PackageTags);deepinfra;api + + + + + + + + + diff --git a/src/Providers/DeepInfra/src/Predefined/AllModels.cs b/src/Providers/DeepInfra/src/Predefined/AllModels.cs new file mode 100644 index 00000000..5ddc0bcf --- /dev/null +++ b/src/Providers/DeepInfra/src/Predefined/AllModels.cs @@ -0,0 +1,137 @@ +namespace LangChain.Providers.DeepInfra.Predefined; + +/// +/// Deep Infra Provider Instance +public class MetaLlama370BInstructModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.MetaLlama370BInstruct); + + +/// +/// Deep Infra Provider Instance +public class MetaLlama38BInstructModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.MetaLlama38BInstruct); + + +/// +/// Deep Infra Provider Instance +public class Mixtral8X22bInstructV01Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Mixtral8X22bInstructV01); + + +/// +/// Deep Infra Provider Instance +public class Wizardlm28X22bModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Wizardlm28X22b); + + +/// +/// Deep Infra Provider Instance +public class Wizardlm27BModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Wizardlm27B); + + +/// +/// Deep Infra Provider Instance +public class ZephyrOrpo141BA35bV01Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.ZephyrOrpo141BA35bV01); + + +/// +/// Deep Infra Provider Instance +public class Gemma117BItModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Gemma117BIt); + + +/// +/// Deep Infra Provider Instance +public class DbrxInstructModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.DbrxInstruct); + + +/// +/// Deep Infra Provider Instance +public class Mixtral8X7BInstructV01Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Mixtral8X7BInstructV01); + + +/// +/// Deep Infra Provider Instance +public class Mistral7BInstructV02Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Mistral7BInstructV02); + + +/// +/// Deep Infra Provider Instance +public class Llama270BChatHfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Llama270BChatHf); + + +/// +/// Deep Infra Provider Instance +public class Dolphin26Mixtral8X7BModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Dolphin26Mixtral8X7B); + + +/// +/// Deep Infra Provider Instance +public class Lzlv70BFp16HfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Lzlv70BFp16Hf); + + +/// +/// Deep Infra Provider Instance +public class OpenChat35Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.OpenChat35); + + +/// +/// Deep Infra Provider Instance +public class Llava157BHfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Llava157BHf); + + +/// +/// Deep Infra Provider Instance +public class Airoboros70BModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Airoboros70B); + + +/// +/// Deep Infra Provider Instance +public class Llama27BChatHfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Llama27BChatHf); + + +/// +/// Deep Infra Provider Instance +public class Yi34BChatModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Yi34BChat); + + +/// +/// Deep Infra Provider Instance +public class ChronosHermes13BV2Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.ChronosHermes13BV2); + + +/// +/// Deep Infra Provider Instance +public class MythomaxL213BModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.MythomaxL213B); + + +/// +/// Deep Infra Provider Instance +public class PhindCodellama34BV2Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.PhindCodellama34BV2); + + +/// +/// Deep Infra Provider Instance +public class Starcoder215BModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Starcoder215B); + + +/// +/// Deep Infra Provider Instance +public class Codellama34BInstructHfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Codellama34BInstructHf); + + +/// +/// Deep Infra Provider Instance +public class Codellama70BInstructHfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Codellama70BInstructHf); + + +/// +/// Deep Infra Provider Instance +public class Llama213BChatHfModel(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Llama213BChatHf); + + +/// +/// Deep Infra Provider Instance +public class Mistral7BInstructV01Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Mistral7BInstructV01); + + +/// +/// Deep Infra Provider Instance +public class Mixtral8X22bV01Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.Mixtral8X22bV01); + + diff --git a/src/Providers/DeepInfra/tests/DeepInfraTests.cs b/src/Providers/DeepInfra/tests/DeepInfraTests.cs new file mode 100644 index 00000000..44746ed4 --- /dev/null +++ b/src/Providers/DeepInfra/tests/DeepInfraTests.cs @@ -0,0 +1,37 @@ +using LangChain.Providers.DeepInfra.Predefined; + +namespace LangChain.Providers.DeepInfra.Tests; + +[TestFixture, Explicit] +public class DeepInfraTests +{ + [Test] + public async Task ShouldGenerateFine_WithPredefinedModel() + { + var apiKey = + Environment.GetEnvironmentVariable("Deep_Infra_Key") ?? + throw new InvalidOperationException("Deep_Infra_Key is not set"); + + var model = new Wizardlm27BModel(new DeepInfraProvider(apiKey)); + + var result = await model.GenerateAsync("Write a Poem".AsHumanMessage()); + + result.Messages.Count.Should().BeGreaterThan(0); + result.Messages.Last().Content.Should().NotBeNullOrEmpty(); + } + + [Test] + public async Task ShouldGenerateFine_With_Enum_Model() + { + var apiKey = + Environment.GetEnvironmentVariable("Deep_Infra_Key", EnvironmentVariableTarget.User) ?? + throw new InvalidOperationException("Deep_Infra_Key is not set"); + + var model = new DeepInfraModel(new DeepInfraProvider(apiKey),DeepInfraModelIds.Wizardlm27B); + + var result = await model.GenerateAsync("Write a Poem".AsHumanMessage()); + + result.Messages.Count.Should().BeGreaterThan(0); + result.Messages.Last().Content.Should().NotBeNullOrEmpty(); + } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tests/LangChain.Providers.DeepInfra.Tests.csproj b/src/Providers/DeepInfra/tests/LangChain.Providers.DeepInfra.Tests.csproj new file mode 100644 index 00000000..df0e35af --- /dev/null +++ b/src/Providers/DeepInfra/tests/LangChain.Providers.DeepInfra.Tests.csproj @@ -0,0 +1,11 @@ + + + + net8.0 + + + + + + + diff --git a/src/Providers/DeepInfra/tests/Tests.cs b/src/Providers/DeepInfra/tests/Tests.cs new file mode 100644 index 00000000..7779a530 --- /dev/null +++ b/src/Providers/DeepInfra/tests/Tests.cs @@ -0,0 +1,32 @@ + +using LangChain.Providers.DeepInfra.Predefined; + +namespace LangChain.Providers.DeepInfra.Tests; + +[TestFixture, Explicit] +public class GeneralTests +{ + [Test] + public async Task GetWeather() + { + var apiKey = + Environment.GetEnvironmentVariable("Deep_Infra_Key") ?? + throw new InvalidOperationException("Deep_Infra_Key environment variable is not found."); + + var model = new Mixtral8X7BInstructV01Model(new DeepInfraProvider(apiKey)); + + var service = new WeatherService(); + model.AddGlobalTools(service.AsTools(), service.AsCalls()); + + var response = await model.GenerateAsync( + new[] + { + "You are a helpful weather assistant.".AsSystemMessage(), + "What's the weather like today?".AsHumanMessage(), + "Sure! Could you please provide me with your location?".AsAiMessage(), + "Dubai, UAE".AsHumanMessage(), + }); + + Console.WriteLine(response.Messages.AsHistory()); + } +} diff --git a/src/Providers/DeepInfra/tests/WeatherFunctions.cs b/src/Providers/DeepInfra/tests/WeatherFunctions.cs new file mode 100644 index 00000000..44f98134 --- /dev/null +++ b/src/Providers/DeepInfra/tests/WeatherFunctions.cs @@ -0,0 +1,58 @@ +using tryAGI.OpenAI; +using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; + +namespace LangChain.Providers.DeepInfra.Tests; + +public enum Unit +{ + Celsius, + Fahrenheit, +} + +public class Weather +{ + public string Location { get; set; } = string.Empty; + public double Temperature { get; set; } + public Unit Unit { get; set; } + public string Description { get; set; } = string.Empty; +} + +[OpenAiFunctions] +public interface IWeatherFunctions +{ + [Description("Get the current weather in a given location")] + public Weather GetCurrentWeather( + [Description("The city and state, e.g. San Francisco, CA")] string location, + Unit unit = Unit.Celsius); + + [Description("Get the current weather in a given location")] + public Task GetCurrentWeatherAsync( + [Description("The city and state, e.g. San Francisco, CA")] string location, + Unit unit = Unit.Celsius, + CancellationToken cancellationToken = default); +} + +public class WeatherService : IWeatherFunctions +{ + public Weather GetCurrentWeather(string location, Unit unit = Unit.Celsius) + { + return new Weather + { + Location = location, + Temperature = 22.0, + Unit = unit, + Description = "Sunny", + }; + } + + public Task GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius, CancellationToken cancellationToken = default) + { + return Task.FromResult(new Weather + { + Location = location, + Temperature = 22.0, + Unit = unit, + Description = "Sunny", + }); + } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Classes/ModelInfo.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Classes/ModelInfo.cs new file mode 100644 index 00000000..54d15247 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Classes/ModelInfo.cs @@ -0,0 +1,13 @@ +namespace LangChain.Providers.DeepInfra.CodeGenerator.Classes; + +public class ModelInfo +{ + public int Index { get; set; } + public string? ModelId { get; set; } + public string? ModelName { get; set; } + public string? Description { get; set; } + public string? DicAddCode { get; set; } + public string? PredefinedClassCode { get; set; } + public string? EnumMemberName { get; set; } + public string? EnumMemberCode { get; set; } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Helpers/DocumentHelper.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Helpers/DocumentHelper.cs new file mode 100644 index 00000000..4f4fe75e --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Helpers/DocumentHelper.cs @@ -0,0 +1,66 @@ +using System.Globalization; +using System.Net; +using HtmlAgilityPack; + +namespace LangChain.Providers.DeepInfra.CodeGenerator.Helpers; + +public class DocumentHelper +{ + private string _docHtml = string.Empty; + + public DocumentHelper() + { + Document = new HtmlDocument(); + DocumentText = ""; + ServicePointManager.Expect100Continue = false; + + HtmlNode.ElementsFlags.Remove("form"); + HtmlNode.ElementsFlags.Remove("option"); + } + + public HtmlDocument Document { get; set; } + + public string DocumentText + { + get => _docHtml; + + set + { + if (!string.IsNullOrEmpty(value)) + { + Document = new HtmlDocument(); + Document.LoadHtml(value); + _docHtml = value; + } + else + { + _docHtml = ""; + Document = new HtmlDocument(); + } + } + } + + public HtmlNode? FindNode(string tagName, string attributeName, string attributeValue, bool approx) + { + tagName = tagName ?? throw new ArgumentNullException(nameof(tagName)); + + if (!approx) + { + var htmlNode = + Document.DocumentNode.SelectSingleNode(string.Format("//{0}[@{1}='{2}']", tagName.ToLower(CultureInfo.InvariantCulture), + attributeName, attributeValue)); + + if (htmlNode != null) return htmlNode; + } + else + { + foreach (var nd in Document.DocumentNode.Descendants(tagName.ToLower(CultureInfo.InvariantCulture))) + if (nd.Attributes[attributeName] != null) + if (!string.IsNullOrEmpty(nd.Attributes[attributeName].Value)) + if (nd.Attributes[attributeName].Value.Contains(attributeValue, StringComparison.InvariantCultureIgnoreCase)) + return nd; + } + + return null; + } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/LangChain.Providers.DeepInfra.CodeGenerator.csproj b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/LangChain.Providers.DeepInfra.CodeGenerator.csproj new file mode 100644 index 00000000..ecb210be --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/LangChain.Providers.DeepInfra.CodeGenerator.csproj @@ -0,0 +1,27 @@ + + + + Exe + net8.0 + enable + enable + $(NoWarn);CA1303;CA1305;CA1304;CA1308 + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/DeepInfraCodeGenerator.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/DeepInfraCodeGenerator.cs new file mode 100644 index 00000000..d7def3b5 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/DeepInfraCodeGenerator.cs @@ -0,0 +1,396 @@ +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using System.Web; +using HtmlAgilityPack; +using LangChain.Providers.DeepInfra.CodeGenerator.Classes; +using LangChain.Providers.DeepInfra.CodeGenerator.Helpers; +using Newtonsoft.Json.Linq; +using static System.Globalization.CultureInfo; +using static System.Text.RegularExpressions.Regex; + +// ReSharper disable LocalizableElement + +namespace LangChain.Providers.DeepInfra.CodeGenerator.Main; + +public static class DeepInfraCodeGenerator +{ + #region Public Methods + + /// + /// Generate Models and Enum files + /// + /// + /// + public static async Task GenerateCodesAsync(GenerationOptions options) + { + options = options ?? throw new ArgumentNullException(nameof(options)); + + + //Load Deep Infra Docs Page... + Console.WriteLine("Loading Models..."); + var models = await GetModelsAsync(options).ConfigureAwait(false); + + + Console.WriteLine($"{models.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); + + //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> GetModelsAsync(GenerationOptions options) + { + var str = await GetStringAsync(new Uri("https://deepinfra.com/models/text-generation")).ConfigureAwait(false); + var lbb = new DocumentHelper(); + var list = new List(); + int index = 0; + //Parse Html + var hashSet = new HashSet(); + do + { + lbb.DocumentText = str; + var links = lbb.FindNode("script", "type", "json", true); + + if (links == null) + throw new Exception("Model Info script node not found."); + + 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); + 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; + } + + #endregion + + #region Private Methods + + /// + /// Creates Codes\DeepInfraModelProvider.cs + /// + /// + /// + /// + private static async Task CreateDeepInfraModelProviderFile(List sorted, string outputFolder) + { + var sb3 = new StringBuilder(); + var first = true; + HashSet keys = new HashSet(); + foreach (var item in sorted) + { + if(!keys.Add(item.EnumMemberName)) + continue; + + if (first) + { + sb3.AppendLine(item.DicAddCode); + first = false; + continue; + } + + sb3.AppendLine($" {item.DicAddCode}"); + } + + var dicsAdd = + H.Resources.DeepInfraModelProvider_cs.AsString().Replace("{{DicAdd}}", sb3.ToString(), StringComparison.InvariantCulture); + Directory.CreateDirectory(outputFolder); + var fileName = Path.Combine(outputFolder, "DeepInfraModelProvider.cs"); + await File.WriteAllTextAsync(fileName, dicsAdd).ConfigureAwait(false); + Console.WriteLine($"Saved to {fileName}"); + } + + /// + /// Creates Codes\DeepInfraModelIds.cs + /// + /// + /// + /// + private static async Task CreateDeepInfraModelIdsFile(List sorted, string outputFolder) + { + var sb3 = new StringBuilder(); + foreach (var item in sorted) + { + var tem = item.EnumMemberCode? + .Replace("\r\n", "\n", StringComparison.InvariantCulture) + .Replace("\n", "\n ", StringComparison.InvariantCulture) + ; + sb3.Append(tem); + } + + var modelIdsContent = + H.Resources.DeepInfraModelIds_cs.AsString().Replace("{{ModelIds}}", sb3.ToString(), StringComparison.InvariantCulture); + Directory.CreateDirectory(outputFolder); + var fileName = Path.Combine(outputFolder, "DeepInfraModelIds.cs"); + await File.WriteAllTextAsync(fileName, modelIdsContent).ConfigureAwait(false); + Console.WriteLine($"Saved to {fileName}"); + } + + /// + /// Creates Codes\Predefined\AllModels.cs file + /// + /// + /// + /// + private static async Task CreateAllModelsFile(List 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, "AllModels.cs"); + await File.WriteAllTextAsync(fileName, classesFileContent).ConfigureAwait(false); + Console.WriteLine($"Saved to {fileName}"); + } + + /// + /// Parses Model info from Deep Infra docs + /// + /// + /// + /// + /// + private static ModelInfo? ParseModelInfo(int i, JToken modelToken, GenerationOptions options) + { + if (modelToken == null) + return null; + + + //Modal Name + var modelName = (string)modelToken["name"]!; + + if (string.IsNullOrEmpty(modelName)) + return null; + + //Model Id + var modelId = (string)modelToken["full_name"]!; + + var organization = (string)modelToken["owner"]!; + var enumMemberName = GetModelIdsEnumMemberFromName(modelId, modelName, options); + + + + var contextLength = (int)(modelToken["max_tokens"] ?? 0); + var tokenLength = contextLength.ToString(); + var promptCost = Math.Round((double)(modelToken.SelectToken("pricing.cents_per_input_token") ?? 0) * 10000,2); + var completionCost = Math.Round((double)(modelToken.SelectToken("pricing.cents_per_output_token") ?? 0) * 10000,2); + + var description = + $"Name: {(string)modelName}
\r\n/// Organization: {organization}
\r\n/// Context Length: {contextLength}
\r\n/// Prompt Cost: ${promptCost}/MTok
\r\n/// Completion Cost: ${promptCost}/MTok
\r\n/// Description: {(string)modelToken["description"]}
\r\n/// HuggingFace Url: https://huggingface.co/{modelId}"; + + //Enum Member code with doc + var enumMemberCode = GetEnumMemberCode(enumMemberName, description); + + //Code for adding ChatModel into Dictionary() + var dicAddCode = GetDicAddCode(enumMemberName, modelId, tokenLength, promptCost / (1000 * 1000), + completionCost / (1000 * 1000)); + + //Code for Predefined Model Class + var predefinedClassCode = GetPreDefinedClassCode(enumMemberName); + + return new ModelInfo + { + DicAddCode = dicAddCode, + EnumMemberName = enumMemberName, + Index = i, + ModelId = modelId, + ModelName = modelName, + PredefinedClassCode = predefinedClassCode, + EnumMemberCode = enumMemberCode, + Description = description + }; + } + + private static string GetEnumMemberCode(string enumMemberName, string description) + { + var sb2 = new StringBuilder(); + + sb2.AppendLine($"\r\n/// \r\n/// {description} \r\n/// "); + + sb2.AppendLine($"{enumMemberName},"); + return sb2.ToString(); + } + + private static string GetPreDefinedClassCode(string enumMemberName) + { + var sb = new StringBuilder(); + sb.AppendLine( + $"/// \r\n/// Deep Infra Provider Instance"); + sb.AppendLine( + $"public class {enumMemberName.Replace("_", "", StringComparison.OrdinalIgnoreCase)}Model(DeepInfraProvider provider) : DeepInfraModel(provider, DeepInfraModelIds.{enumMemberName});"); + return sb.ToString(); + } + + private static string GetDicAddCode(string enumMemberName, string modelId, string tokenLength, double promptCost, + double completionCost) + { + return "{ " + + $"DeepInfraModelIds.{enumMemberName}, new ChatModels(\"{modelId}\",{tokenLength},{promptCost},{completionCost})" + + "},"; + } + + /// + /// Creates Enum Member name from Model Name with C# convention + /// + /// + /// + /// + /// + private static string GetModelIdsEnumMemberFromName(string modelId, string modelName, GenerationOptions options) + { + var enumType = Replace(modelName, "[_.: -]", "_"); + enumType = Replace(enumType, "[()]", ""); + + enumType = enumType + .Replace("__", "_", StringComparison.InvariantCulture) + .Replace("_🐬", "", StringComparison.InvariantCulture); + + enumType = CurrentCulture.TextInfo.ToTitleCase(enumType + .Replace("_", " ", StringComparison.InvariantCulture).ToLower(CurrentCulture)); + + enumType = options.ReplaceEnumNameFunc?.Invoke(enumType, modelId, modelName) ?? enumType; + + return enumType; + } + + /// + /// Fetches Model Page and parses the model description + /// + /// + /// + /// + private static async Task GetDescription(int i, IEnumerable tr) + { + var lbb = new DocumentHelper(); + var anchor = tr.ElementAt(0).Descendants("a").First(); + + var href = anchor.GetAttributeValue("href", ""); + var url = $"https://DeepInfra.ai{href}"; + + Console.WriteLine($"{i - 1} Fetching doc from {url}..."); + + var path = Path.Combine("cache", href.Replace('/', '_') + ".html"); + if (File.Exists(path)) + { + return await File.ReadAllTextAsync(path).ConfigureAwait(false); + } + + var str = await GetStringAsync(new Uri(url)).ConfigureAwait(false); + + lbb.DocumentText = str; + + var msg = + lbb.FindNode("div", "class", "prose-slate", true) ?? + throw new InvalidOperationException("Description not found"); + + var sb = new StringBuilder(); + var first = true; + foreach (var child in msg.ChildNodes) + { + if (string.IsNullOrEmpty(child.InnerText.Trim())) continue; + var text = child.InnerText.Trim().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + foreach (var text2 in text) + { + var text3 = HttpUtility.HtmlDecode(text2); + if (first) + { + sb.AppendLine(text3 + "
"); + first = false; + } + + if (sb.ToString().Contains(text3, StringComparison.OrdinalIgnoreCase)) + continue; + sb.AppendLine($"/// {text3}
"); + } + } + + var html = sb.ToString().Trim(); + + Directory.CreateDirectory("cache"); + + await File.WriteAllTextAsync(path, html).ConfigureAwait(false); + + return html; + } + + /// + /// Creates HttpClient + /// + /// + private static async Task GetStringAsync(Uri uri, CancellationToken cancellationToken = default) + { + using var handler = new HttpClientHandler(); + //handler.CheckCertificateRevocationList = true; + handler.AutomaticDecompression = + DecompressionMethods.Deflate | DecompressionMethods.Brotli | DecompressionMethods.GZip; + using var client = new HttpClient(handler); + client.DefaultRequestHeaders.Add("Accept", "*/*"); + client.DefaultRequestHeaders.Add("UserAgent", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"); + client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br"); + + using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5)); + + while (!cancellationToken.IsCancellationRequested) + { + try + { + return await client.GetStringAsync(uri, cancellationToken).ConfigureAwait(false); + } + catch (HttpRequestException ex) + { + if (ex.StatusCode == HttpStatusCode.NotFound) + { + return null; + } + await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false); + } + } + + throw new TaskCanceledException(); + } + + #endregion +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/GenerationOptions.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/GenerationOptions.cs new file mode 100644 index 00000000..6cb0e430 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Main/GenerationOptions.cs @@ -0,0 +1,7 @@ +namespace LangChain.Providers.DeepInfra.CodeGenerator.Main; + +public class GenerationOptions +{ + public string OutputFolder { get; set; } = "Generated"; + public Func? ReplaceEnumNameFunc { get; set; } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Program.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Program.cs new file mode 100644 index 00000000..bc9d4461 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Program.cs @@ -0,0 +1,62 @@ +using System.CommandLine; +using System.CommandLine.NamingConventionBinder; +using LangChain.Providers.DeepInfra.CodeGenerator.Main; + +var rootCommand = new RootCommand( + "Create Codes for OpenRouter Models") +{ + new Option( + ["--underscore", "-u"], "Add underscores into enum member name"), + new Option( + ["--output", "-o"], getDefaultValue: () => "../../../../../src", "Output directory") +}; + +rootCommand.Handler = CommandHandler.Create(async (bool underscore, string output) => +{ + var replaces = new Dictionary + { + { " ", "" }, + { "+", "Plus" }, + { "BV0", "Bv0" }, + { "7b", "7B" }, + { "bBa", "BBa" }, + { "bSf", "BSf" }, + { "DPO", "Dpo" }, + { "SFT", "Sft" }, + { "Openai", "OpenAi" }, + { "Openchat", "OpenChat" }, + { "Openher", "OpenHer" }, + { "Openorca", "OpenOrca" }, + }; + if (underscore) + { + replaces[" "] = "_"; + } + + await DeepInfraCodeGenerator.GenerateCodesAsync(new GenerationOptions + { + OutputFolder = output, + ReplaceEnumNameFunc = ReplaceFunc, + }).ConfigureAwait(false); + return; + + + string ReplaceFunc(string enumType, string modelId, string modelName) + { + foreach (var (oldValue, newValue) in replaces) + { + enumType = enumType.Replace(oldValue, newValue, StringComparison.InvariantCulture); + } + + if (modelId == "openai/gpt-3.5-turbo-0125") + { + enumType = underscore + ? "OpenAi_Gpt_3_5_Turbo_16K_0125" + : "OpenAiGpt35Turbo16K0125"; + } + + return enumType; + } +}); + +return await rootCommand.InvokeAsync(args).ConfigureAwait(false); \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Properties/launchSettings.json b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Properties/launchSettings.json new file mode 100644 index 00000000..dcc7d78a --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "LangChain.Providers.OpenRouter.CodeGenerator": { + "commandName": "Project", + "commandLineArgs": "" + } + } +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/AllModels.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/AllModels.cs new file mode 100644 index 00000000..38c58730 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/AllModels.cs @@ -0,0 +1,3 @@ +namespace LangChain.Providers.DeepInfra.Predefined; + +{{DeepInfraClasses}} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelIds.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelIds.cs new file mode 100644 index 00000000..37f52be3 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelIds.cs @@ -0,0 +1,9 @@ +namespace LangChain.Providers.DeepInfra; + +/// +/// List of all the Predefined DeepInfra Models +/// +public enum DeepInfraModelIds +{ +{{ModelIds}} +} \ No newline at end of file diff --git a/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelProvider.cs b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelProvider.cs new file mode 100644 index 00000000..324c98f9 --- /dev/null +++ b/src/Providers/DeepInfra/tools/LangChain.Providers.DeepInfra.CodeGenerator/Resources/DeepInfraModelProvider.cs @@ -0,0 +1,24 @@ +using OpenAI.Constants; + +namespace LangChain.Providers.DeepInfra; + +/// +/// Contains all the DeepInfra models. +/// +public static class DeepInfraModelProvider +{ + private static Dictionary Models { get; set; } = new() + { + {{DicAdd}} + }; + + public static ChatModels GetModelById(DeepInfraModelIds modelId) + { + if (Models.TryGetValue(modelId, out var id)) + { + return id; + } + + throw new ArgumentException($"Invalid Deep Infra Model {modelId}"); + } +} \ No newline at end of file diff --git a/src/Providers/OpenAI/src/LangChain.Providers.OpenAI.csproj b/src/Providers/OpenAI/src/LangChain.Providers.OpenAI.csproj index 44350a23..2c16fb3e 100644 --- a/src/Providers/OpenAI/src/LangChain.Providers.OpenAI.csproj +++ b/src/Providers/OpenAI/src/LangChain.Providers.OpenAI.csproj @@ -4,6 +4,10 @@ net4.6.2;netstandard2.0;net6.0;net7.0;net8.0 + + + + @@ -14,10 +18,6 @@ $(PackageTags);openai;api - - - - diff --git a/src/Providers/OpenAI/src/OpenAiProvider.cs b/src/Providers/OpenAI/src/OpenAiProvider.cs index d7689818..2e063672 100644 --- a/src/Providers/OpenAI/src/OpenAiProvider.cs +++ b/src/Providers/OpenAI/src/OpenAiProvider.cs @@ -46,13 +46,13 @@ public OpenAiProvider(OpenAiConfiguration configuration) public OpenAiProvider( string apiKey, - string? customEndpoint = null) + string? customEndpoint = null, string? apiVersion = "v1") : base(id: OpenAiConfiguration.SectionName) { apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); Api = customEndpoint != null - ? new OpenAIClient(apiKey, new OpenAIClientSettings(domain: customEndpoint)) + ? new OpenAIClient(apiKey, new OpenAIClientSettings(domain: customEndpoint,apiVersion: apiVersion)) : new OpenAIClient(apiKey); }