Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Anthropic Provider #192

Merged
merged 21 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions LangChain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Amazon.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Amazon.SageMaker.Tests", "src\Providers\Amazon.Sagemaker\test\LangChain.Providers.Amazon.SageMaker.Tests.csproj", "{08270801-B335-4DDE-8329-9D9198C3D3A1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Anthropic", "src\Providers\Anthropic\src\LangChain.Providers.Anthropic.csproj", "{24D2455C-5429-4001-83D9-6E80B3134127}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Anyscale", "src\Providers\Anyscale\src\LangChain.Providers.Anyscale.csproj", "{F9B5D1B9-3390-47B5-BE37-219B66E44F70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Automatic1111", "src\Providers\Automatic1111\src\LangChain.Providers.Automatic1111.csproj", "{DA40F290-6B2D-451B-8BE2-9C577C4F1B11}"
Expand Down Expand Up @@ -280,7 +278,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.OpenRou
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.OpenRouter.Tests", "src\Providers\OpenRouter\tests\LangChain.Providers.OpenRouter.Tests.csproj", "{4C3A06A7-5603-426C-B57C-D34336679002}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Providers.OpenRouter.CodeGenerator", "src\Providers\OpenRouter\tools\LangChain.Providers.OpenRouter.CodeGenerator\LangChain.Providers.OpenRouter.CodeGenerator.csproj", "{1BC041D3-F70D-4906-BA8F-49DB4FB7AEF0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.OpenRouter.CodeGenerator", "src\Providers\OpenRouter\tools\LangChain.Providers.OpenRouter.CodeGenerator\LangChain.Providers.OpenRouter.CodeGenerator.csproj", "{1BC041D3-F70D-4906-BA8F-49DB4FB7AEF0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Anthropic.Tests", "src\Providers\Anthropic\tests\LangChain.Providers.Anthropic.Tests.csproj", "{CAE7BB48-A6FF-41A5-A418-D34E18A1BC00}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Anthropic", "src\Providers\Anthropic\libs\Anthropic\LangChain.Providers.Anthropic.csproj", "{2FB02C8A-4537-401D-9DAB-171E8562EB6A}"
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -344,10 +348,6 @@ Global
{08270801-B335-4DDE-8329-9D9198C3D3A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08270801-B335-4DDE-8329-9D9198C3D3A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08270801-B335-4DDE-8329-9D9198C3D3A1}.Release|Any CPU.Build.0 = Release|Any CPU
{24D2455C-5429-4001-83D9-6E80B3134127}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24D2455C-5429-4001-83D9-6E80B3134127}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24D2455C-5429-4001-83D9-6E80B3134127}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24D2455C-5429-4001-83D9-6E80B3134127}.Release|Any CPU.Build.0 = Release|Any CPU
{F9B5D1B9-3390-47B5-BE37-219B66E44F70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9B5D1B9-3390-47B5-BE37-219B66E44F70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9B5D1B9-3390-47B5-BE37-219B66E44F70}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -584,6 +584,18 @@ Global
{1BC041D3-F70D-4906-BA8F-49DB4FB7AEF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BC041D3-F70D-4906-BA8F-49DB4FB7AEF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BC041D3-F70D-4906-BA8F-49DB4FB7AEF0}.Release|Any CPU.Build.0 = Release|Any CPU
{CAE7BB48-A6FF-41A5-A418-D34E18A1BC00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CAE7BB48-A6FF-41A5-A418-D34E18A1BC00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CAE7BB48-A6FF-41A5-A418-D34E18A1BC00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CAE7BB48-A6FF-41A5-A418-D34E18A1BC00}.Release|Any CPU.Build.0 = Release|Any CPU
{2FB02C8A-4537-401D-9DAB-171E8562EB6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FB02C8A-4537-401D-9DAB-171E8562EB6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FB02C8A-4537-401D-9DAB-171E8562EB6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FB02C8A-4537-401D-9DAB-171E8562EB6A}.Release|Any CPU.Build.0 = Release|Any CPU
{358ACF7D-0859-4009-BFC3-1F3B6FE4BB86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -616,7 +628,6 @@ Global
{10B7C532-327F-4424-85EE-A7D4E06E9976} = {96CCA968-1CE4-4DAB-9495-F8BA1249ADCD}
{08BA819F-538B-44A8-9463-3F90381F7F0D} = {96CCA968-1CE4-4DAB-9495-F8BA1249ADCD}
{08270801-B335-4DDE-8329-9D9198C3D3A1} = {96CCA968-1CE4-4DAB-9495-F8BA1249ADCD}
{24D2455C-5429-4001-83D9-6E80B3134127} = {7A2A589D-F8EF-4744-9BEE-B06A5F109851}
{F9B5D1B9-3390-47B5-BE37-219B66E44F70} = {B165FD5F-43B9-4A0C-A627-C4C94F6C027C}
{DA40F290-6B2D-451B-8BE2-9C577C4F1B11} = {0ACB8140-8198-4FA4-94DF-95A159E38E5C}
{4724E9F2-A344-4B64-9492-AD6A6964AF43} = {0ACB8140-8198-4FA4-94DF-95A159E38E5C}
Expand Down Expand Up @@ -699,6 +710,9 @@ Global
{A9442EA6-7567-4F1A-86E8-289DF51C9695} = {7FE91F38-E133-4F58-9852-BCB6A15A82D1}
{4C3A06A7-5603-426C-B57C-D34336679002} = {7FE91F38-E133-4F58-9852-BCB6A15A82D1}
{1BC041D3-F70D-4906-BA8F-49DB4FB7AEF0} = {7FE91F38-E133-4F58-9852-BCB6A15A82D1}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5C00D0F1-6138-4ED9-846B-97E43D6DFF1C}
Expand Down
7 changes: 5 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ItemGroup>
<PackageVersion Include="AngleSharp" Version="1.1.0" />
<PackageVersion Include="Anthropic" Version="0.3.1" />
<PackageVersion Include="Anthropic.SDK" Version="2.0.1" />
<PackageVersion Include="Anyscale" Version="1.0.2" />
<PackageVersion Include="Aspose.PDF" Version="24.2.0" />
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="3.7.301.43" />
Expand All @@ -17,13 +18,15 @@
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.1.1" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageVersion Include="Google_GenerativeAI" Version="0.1.18" />
<PackageVersion Include="Google_GenerativeAI" Version="0.1.19" />
<PackageVersion Include="H.Generators.Extensions" Version="1.22.0" />
<PackageVersion Include="H.Resources.Generator" Version="1.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="HtmlAgilityPack" Version="1.11.60" />
<PackageVersion Include="HuggingFace" Version="0.2.4" />
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
<PackageVersion Include="LeonardoAi" Version="0.1.0" />
<PackageVersion Include="LLamaSharp" Version="0.10.0" />
<PackageVersion Include="LLamaSharp.Backend.Cpu" Version="0.10.0" />
Expand Down Expand Up @@ -66,7 +69,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="Tiktoken" Version="1.1.3" />
<PackageVersion Include="tryAGI.OpenAI" Version="2.0.2" />
<PackageVersion Include="tryAGI.OpenAI" Version="2.0.4" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Providers\Anthropic\src\LangChain.Providers.Anthropic.csproj" />
<ProjectReference Include="..\..\..\Providers\Anthropic\libs\Anthropic\LangChain.Providers.Anthropic.csproj" />
<ProjectReference Include="..\..\..\Providers\Anyscale\src\LangChain.Providers.Anyscale.csproj" />
<ProjectReference Include="..\..\..\Providers\HuggingFace\src\LangChain.Providers.HuggingFace.csproj" />
<ProjectReference Include="..\..\..\Providers\OpenAI\src\LangChain.Providers.OpenAI.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static IServiceCollection AddAnthropic(
_ = services
.AddScoped<AnthropicProvider>(static services => AnthropicProvider.FromConfiguration(
configuration: services.GetRequiredService<IOptions<AnthropicConfiguration>>().Value,
httpClient: services.GetRequiredService<HttpClient>()));
httpClientFactory: services.GetRequiredService<IHttpClientFactory>()));

return services;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Meta/src/LangChain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<ProjectReference Include="..\..\Core\src\LangChain.Core.csproj" />
<ProjectReference Include="..\..\Databases\InMemory\src\LangChain.Databases.InMemory.csproj" />
<ProjectReference Include="..\..\Extensions\DependencyInjection\src\LangChain.Extensions.DependencyInjection.csproj" />

HavenDV marked this conversation as resolved.
Show resolved Hide resolved
<ProjectReference Include="..\..\Providers\Anthropic\src\LangChain.Providers.Anthropic.csproj" />
<ProjectReference Include="..\..\Providers\Anthropic\libs\Anthropic\LangChain.Providers.Anthropic.csproj" />
<ProjectReference Include="..\..\Providers\Anyscale\src\LangChain.Providers.Anyscale.csproj" />
<ProjectReference Include="..\..\Providers\HuggingFace\src\LangChain.Providers.HuggingFace.csproj" />
<ProjectReference Include="..\..\Providers\OpenAI\src\LangChain.Providers.OpenAI.csproj" />
Expand Down
20 changes: 20 additions & 0 deletions src/Providers/Anthropic/Delete-BIN-OBJ-Folders.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@ECHO off
cls

ECHO Deleting all BIN and OBJ folders...
ECHO.

FOR /d /r . %%d in (bin,obj,node_modules,Logs) DO (
IF EXIST "%%d" (
ECHO %%d | FIND /I "\node_modules\" > Nul && (
ECHO.Skipping: %%d
) || (
ECHO.Deleting: %%d
rd /s/q "%%d"
)
)
)

ECHO.
ECHO.BIN and OBJ folders have been successfully deleted. Press any key to exit.
pause > nul
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
using System.ComponentModel;
using H.Generators.Extensions;
using LangChain.Providers.Anthropic.Generators;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace H.Generators;

[Generator]
public class AnthropicToolsGenerator : IIncrementalGenerator
{
#region Constants

public const string Name = nameof(AnthropicToolsGenerator);
public const string Id = "OAFG";

#endregion

#region Methods

public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.SyntaxProvider
.ForAttributeWithMetadataName("LangChain.Providers.Anthropic.Tools.AnthropicToolsAttribute")
.SelectManyAllAttributesOfCurrentInterfaceSyntax()
.SelectAndReportExceptions(PrepareData, context, Id)
.SelectAndReportExceptions(GetClientSourceCode, context, Id)
.AddSource(context);
}

private static string GetDescription(ISymbol symbol)
{
return symbol.GetAttributes()
.FirstOrDefault(static x => x.AttributeClass?.Name == nameof(DescriptionAttribute))?
.ConstructorArguments.First().Value?.ToString() ?? string.Empty;
}

private static InterfaceData PrepareData(
(SemanticModel SemanticModel, AttributeData AttributeData, InterfaceDeclarationSyntax InterfaceSyntax, INamedTypeSymbol InterfaceSymbol) tuple)
{
var (_, _, _, interfaceSymbol) = tuple;

var methods = interfaceSymbol
.GetMembers()
.OfType<IMethodSymbol>()
.Where(static x => x.MethodKind == MethodKind.Ordinary)
.Select(static x => new MethodData(
Name: x.Name,
Description: GetDescription(x),
IsAsync: x.IsAsync || x.ReturnType.Name == "Task",
IsVoid: x.ReturnsVoid,
Parameters: x.Parameters
.Where(static x => x.Type.MetadataName != "CancellationToken")
.Select(static x => ToParameterData(
typeSymbol: x.Type,
name: x.Name,
description: GetDescription(x),
isRequired: !x.IsOptional))
.ToArray()))
.ToArray();

return new InterfaceData(
Namespace: interfaceSymbol.ContainingNamespace.ToDisplayString(),
Name: interfaceSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat),
Methods: methods);
}

private static ParameterData ToParameterData(ITypeSymbol typeSymbol, string? name = null, string? description = null, bool isRequired = true)
{
string schemaType;
string? format = null;
var properties = Array.Empty<ParameterData>();
ParameterData? arrayItem = null;
switch (typeSymbol.TypeKind)
{
case TypeKind.Enum:
schemaType = "string";
break;

case TypeKind.Structure:
switch (typeSymbol.SpecialType)
{
case SpecialType.System_Int32:
schemaType = "integer";
format = "int32";
break;

case SpecialType.System_Int64:
schemaType = "integer";
format = "int64";
break;

case SpecialType.System_Single:
schemaType = "number";
format = "double";
break;

case SpecialType.System_Double:
schemaType = "number";
format = "float";
break;

case SpecialType.System_DateTime:
schemaType = "string";
format = "date-time";
break;

case SpecialType.System_Boolean:
schemaType = "boolean";
break;

case SpecialType.None:
switch (typeSymbol.Name)
{
case "DateOnly":
schemaType = "string";
format = "date";
break;

default:
throw new NotImplementedException($"{typeSymbol.Name} is not implemented.");
}
break;

default:
throw new NotImplementedException($"{typeSymbol.SpecialType} is not implemented.");
}
break;

case TypeKind.Class:
switch (typeSymbol.SpecialType)
{
case SpecialType.System_String:
schemaType = "string";
break;


case SpecialType.None:
schemaType = "object";
properties = typeSymbol.GetMembers()
.OfType<IPropertySymbol>()
.Select(static y => ToParameterData(
typeSymbol: y.Type,
name: y.Name,
description: GetDescription(y),
isRequired: true))
.ToArray();
break;

default:
throw new NotImplementedException($"{typeSymbol.SpecialType} is not implemented.");
}
break;

case TypeKind.Interface when typeSymbol.MetadataName == "IReadOnlyCollection`1":
schemaType = "array";
arrayItem = (typeSymbol as INamedTypeSymbol)?.TypeArguments
.Select(static y => ToParameterData(y))
.FirstOrDefault();
break;

case TypeKind.Array:
schemaType = "array";
arrayItem = ToParameterData((typeSymbol as IArrayTypeSymbol)?.ElementType!);
break;

default:
throw new NotImplementedException($"{typeSymbol.TypeKind} is not implemented.");
}

return new ParameterData(
Name: !string.IsNullOrWhiteSpace(name)
? name!
: typeSymbol.Name,
Description: !string.IsNullOrWhiteSpace(description)
? description!
: GetDescription(typeSymbol),
Type: typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
DefaultValue: GetDefaultValue(typeSymbol),
SchemaType: schemaType,
Format: format,
Properties: properties,
ArrayItem: arrayItem != null
? new []{ arrayItem.Value }
: Array.Empty<ParameterData>(),
EnumValues: typeSymbol.TypeKind == TypeKind.Enum
? typeSymbol
.GetMembers()
.OfType<IFieldSymbol>()
.Select(static x => x.Name.ToLowerInvariant())
.ToArray()
: Array.Empty<string>(),
IsNullable: IsNullable(typeSymbol),
IsRequired: isRequired);
}

private static bool IsNullable(ITypeSymbol typeSymbol)
{
if (typeSymbol.TypeKind == TypeKind.Enum)
{
return false;
}
if (typeSymbol.TypeKind == TypeKind.Structure)
{
return false;
}

return typeSymbol.SpecialType switch
{
SpecialType.System_String => false,
_ => true,
};
}

private static string GetDefaultValue(ITypeSymbol typeSymbol)
{
switch (typeSymbol.SpecialType)
{
case SpecialType.System_String:
return "string.Empty";

default:
return string.Empty;
}
}

private static FileWithName GetClientSourceCode(InterfaceData @interface)
{
return new FileWithName(
Name: $"{@interface.Name}.Functions.generated.cs",
Text: SourceGenerationHelper.GenerateClientImplementation(@interface));
}

#endregion
}
Loading
Loading