Skip to content

Commit

Permalink
feat: Added DeepSeek and Together.ai Providers (#194)
Browse files Browse the repository at this point in the history
* fix: fixed open router model pricings, added doc, fixed naming conventions

* fix: open router provider unit test case

* feat: added open router code generator

* fix: some code fixes

* refactor: Rearranged and documented the codes of open router code generator

* fix: some fixes

* feat: added command line parameters into Open Router Code Generator and some code rearrangements and refactoring.

* fix: formatting issue

* fix: fixed some formatting issue in the open router code generator

* fix: code formatting

* fix: parameter doc of GenerateCodesAsync

* feat: updated anthropic sdk

* feat: added function calling support for Anthropic Claude 3 models

* fix: removed unused codes

* fix: Anthropic dependency injection

* fix: added anthropic into meta

* fix: anthropic generator path

* fix: anthropic generator path

* feat: added Claude v2.1 and Claude instant 1.2 in the Anthropic provider.

* fix: Anthropic model docs

* feat: Added DeepSeek AI Provider

* feat: Added proper implementation for Together.ai

* fix: together.ai docs

* fix: code cleanups

* fix: code cleanups
  • Loading branch information
gunpal5 authored Apr 6, 2024
1 parent d40ee3b commit 16be418
Show file tree
Hide file tree
Showing 21 changed files with 1,721 additions and 10 deletions.
34 changes: 34 additions & 0 deletions LangChain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Anthrop
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Anthropic.Generators", "src\Providers\Anthropic\libs\Anthropic.Generator\LangChain.Providers.Anthropic.Generators.csproj", "{358ACF7D-0859-4009-BFC3-1F3B6FE4BB86}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DeepSeek", "DeepSeek", "{674D7125-B893-45FA-94D8-8CE0946578A5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.DeepSeek", "src\Providers\DeepSeek\src\LangChain.Providers.DeepSeek.csproj", "{6217F9EB-35EE-4E36-BF29-B1E49CEDC5A5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.DeepSeek.Tests", "src\Providers\DeepSeek\tests\LangChain.Providers.DeepSeek.Tests.csproj", "{33813E14-1889-49F5-AD4B-81EBABBFCC49}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TogetherAi", "TogetherAi", "{CD001ADE-7B22-4325-9B3C-5C7D3B812797}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.TogetherAi", "src\Providers\TogetherAI\src\LangChain.Providers.TogetherAi.csproj", "{15D318BC-3954-4B32-B72A-B0FC97048EB3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.TogetherAi.Tests", "src\Providers\TogetherAI\tests\LangChain.Providers.TogetherAi.Tests.csproj", "{0161A0F5-D55C-418A-BF54-AEE8EC73C52C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -596,6 +608,22 @@ Global
{358ACF7D-0859-4009-BFC3-1F3B6FE4BB86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{358ACF7D-0859-4009-BFC3-1F3B6FE4BB86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{358ACF7D-0859-4009-BFC3-1F3B6FE4BB86}.Release|Any CPU.Build.0 = Release|Any CPU
{6217F9EB-35EE-4E36-BF29-B1E49CEDC5A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6217F9EB-35EE-4E36-BF29-B1E49CEDC5A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6217F9EB-35EE-4E36-BF29-B1E49CEDC5A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6217F9EB-35EE-4E36-BF29-B1E49CEDC5A5}.Release|Any CPU.Build.0 = Release|Any CPU
{33813E14-1889-49F5-AD4B-81EBABBFCC49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33813E14-1889-49F5-AD4B-81EBABBFCC49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33813E14-1889-49F5-AD4B-81EBABBFCC49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33813E14-1889-49F5-AD4B-81EBABBFCC49}.Release|Any CPU.Build.0 = Release|Any CPU
{15D318BC-3954-4B32-B72A-B0FC97048EB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15D318BC-3954-4B32-B72A-B0FC97048EB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15D318BC-3954-4B32-B72A-B0FC97048EB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15D318BC-3954-4B32-B72A-B0FC97048EB3}.Release|Any CPU.Build.0 = Release|Any CPU
{0161A0F5-D55C-418A-BF54-AEE8EC73C52C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0161A0F5-D55C-418A-BF54-AEE8EC73C52C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0161A0F5-D55C-418A-BF54-AEE8EC73C52C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0161A0F5-D55C-418A-BF54-AEE8EC73C52C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -713,6 +741,12 @@ Global
{CAE7BB48-A6FF-41A5-A418-D34E18A1BC00} = {7A2A589D-F8EF-4744-9BEE-B06A5F109851}
{2FB02C8A-4537-401D-9DAB-171E8562EB6A} = {7A2A589D-F8EF-4744-9BEE-B06A5F109851}
{358ACF7D-0859-4009-BFC3-1F3B6FE4BB86} = {7A2A589D-F8EF-4744-9BEE-B06A5F109851}
{674D7125-B893-45FA-94D8-8CE0946578A5} = {E2B9833C-0397-4FAF-A3A8-116E58749750}
{6217F9EB-35EE-4E36-BF29-B1E49CEDC5A5} = {674D7125-B893-45FA-94D8-8CE0946578A5}
{33813E14-1889-49F5-AD4B-81EBABBFCC49} = {674D7125-B893-45FA-94D8-8CE0946578A5}
{CD001ADE-7B22-4325-9B3C-5C7D3B812797} = {E2B9833C-0397-4FAF-A3A8-116E58749750}
{15D318BC-3954-4B32-B72A-B0FC97048EB3} = {CD001ADE-7B22-4325-9B3C-5C7D3B812797}
{0161A0F5-D55C-418A-BF54-AEE8EC73C52C} = {CD001ADE-7B22-4325-9B3C-5C7D3B812797}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5C00D0F1-6138-4ED9-846B-97E43D6DFF1C}
Expand Down
12 changes: 12 additions & 0 deletions src/Providers/DeepSeek/src/DeepSeekConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using LangChain.Providers.OpenAI;

namespace LangChain.Providers.DeepSeek;

/// <summary>
/// </summary>
public class DeepSeekConfiguration : OpenAiConfiguration
{
/// <summary>
/// </summary>
public new const string SectionName = "DeepSeek";
}
10 changes: 10 additions & 0 deletions src/Providers/DeepSeek/src/DeepSeekModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using LangChain.Providers.OpenAI;

namespace LangChain.Providers.DeepSeek;

/// <summary>
/// </summary>
public class DeepSeekModel(
DeepSeekProvider provider,
string id)
: OpenAiChatModel(provider, DeepSeekModels.GetModelById(id));
31 changes: 31 additions & 0 deletions src/Providers/DeepSeek/src/DeepSeekModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using OpenAI.Constants;

namespace LangChain.Providers.DeepSeek;

public static class DeepSeekModels
{
/// <summary>
/// Good at general tasks
/// Context Length 16k
/// </summary>
public const string DeepSeekChat = "deepseek-chat";

/// <summary>
/// Good at coding tasks
/// Context Length 16k
/// </summary>
public const string DeepSeekCoder = "deepseek-coder";

public static ChatModels GetModelById(string id)
{
switch (id)
{
case DeepSeekChat:
return new ChatModels(DeepSeekChat, 16 * 1000, 0, 0);
case DeepSeekCoder:
return new ChatModels(DeepSeekCoder, 16 * 1000, 0, 0);
default:
throw new NotImplementedException("Not a valid DeepSeek model.");
}
}
}
14 changes: 14 additions & 0 deletions src/Providers/DeepSeek/src/DeepSeekProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using LangChain.Providers.OpenAI;

namespace LangChain.Providers.DeepSeek;

public class DeepSeekProvider : OpenAiProvider
{
public DeepSeekProvider(DeepSeekConfiguration configuration) : base(configuration)
{
}

public DeepSeekProvider(string apiKey) : base(apiKey, "api.deepseek.com")
{
}
}
21 changes: 21 additions & 0 deletions src/Providers/DeepSeek/src/LangChain.Providers.DeepSeek.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup Label="Usings">
<Using Include="System.Net.Http" />
</ItemGroup>

<PropertyGroup Label="NuGet">
<Description>DeepSeek Chat model provider.</Description>
<PackageTags>$(PackageTags);deepseek;api</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Abstractions\src\LangChain.Providers.Abstractions.csproj" />
<ProjectReference Include="..\..\OpenAI\src\LangChain.Providers.OpenAI.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions src/Providers/DeepSeek/src/Predefined/DeepSeek.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace LangChain.Providers.DeepSeek.Predefined;

/// <inheritdoc cref="DeepSeekModels.DeepSeekChat" />
public class DeepSeekChatModel(DeepSeekProvider provider)
: DeepSeekModel(provider, DeepSeekModels.DeepSeekChat);

/// <inheritdoc cref="DeepSeekModels.DeepSeekChat" />
public class DeepSeekCoderModel(DeepSeekProvider provider)
: DeepSeekModel(provider, DeepSeekModels.DeepSeekCoder);
40 changes: 40 additions & 0 deletions src/Providers/DeepSeek/tests/DeepSeekTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using LangChain.Providers.DeepSeek.Predefined;

namespace LangChain.Providers.DeepSeek.Tests;

[TestFixture]
[Explicit]
public class DeepSeekTests
{
[Test]
public async Task ShouldGenerateFine_WithChatModel()
{
var apiKey =
Environment.GetEnvironmentVariable("DeepSeek_API_Key", EnvironmentVariableTarget.User) ??
throw new InvalidOperationException("DeepSeek_API_Key is not set");

var model = new DeepSeekChatModel(new DeepSeekProvider(apiKey));

var result = await model.GenerateAsync("Write a Poem".AsHumanMessage());

result.Messages.Count.Should().BeGreaterThan(0);
result.Messages.Last().Content.Should().NotBeNullOrEmpty();
Console.WriteLine(result.LastMessageContent);
}

[Test]
public async Task ShouldGenerateFine_With_CoderModel()
{
var apiKey =
Environment.GetEnvironmentVariable("DeepSeek_API_Key", EnvironmentVariableTarget.User) ??
throw new InvalidOperationException("DeepSeek_API_Key is not set");

var model = new DeepSeekCoderModel(new DeepSeekProvider(apiKey));

var result = await model.GenerateAsync("Write a python script to count from 0 to 100".AsHumanMessage());

result.Messages.Count.Should().BeGreaterThan(0);
result.Messages.Last().Content.Should().NotBeNullOrEmpty();
Console.WriteLine(result.LastMessageContent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\LangChain.Providers.DeepSeek.csproj" />
</ItemGroup>

</Project>
11 changes: 1 addition & 10 deletions src/Providers/OpenRouter/src/OpenRouterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,5 @@ public OpenRouterModel(OpenRouterProvider provider,
{
}

#region MyRegion

protected override Task CallFunctionsAsync(global::OpenAI.Chat.Message message, List<Message> messages, CancellationToken cancellationToken = default)
{
if (!this.Id.Contains("openai/"))
throw new NotImplementedException("Function calling is only supported with OpenAI Models.");
return base.CallFunctionsAsync(message, messages, cancellationToken);
}

#endregion

}
22 changes: 22 additions & 0 deletions src/Providers/TogetherAI/src/LangChain.Providers.TogetherAi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup Label="Usings">
<Using Include="System.Net.Http" />
</ItemGroup>

<PropertyGroup Label="NuGet">
<Description>Together.ai Chat model provider.</Description>
<PackageTags>$(PackageTags);together;ai;together.ai;api</PackageTags>
</PropertyGroup>


<ItemGroup>
<ProjectReference Include="..\..\Abstractions\src\LangChain.Providers.Abstractions.csproj" />
<ProjectReference Include="..\..\OpenAI\src\LangChain.Providers.OpenAI.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 16be418

Please sign in to comment.