Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/all-2ae299a549
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV authored Feb 12, 2024
2 parents fa60f17 + 40a6831 commit 6b6a02b
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 46 deletions.
16 changes: 7 additions & 9 deletions LangChain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Automat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Automatic1111.IntegrationTests", "src\tests\LangChain.Providers.Automatic1111.IntegrationTests\LangChain.Providers.Automatic1111.IntegrationTests.csproj", "{A6CF79BC-8365-46E8-9230-1A4AD615D40B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Bedrock.IntegrationTests", "src\tests\LangChain.Providers.Bedrock.IntegrationTests\LangChain.Providers.Bedrock.IntegrationTests.csproj", "{73C76E80-95C5-4C96-A319-4F32043C903E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Bedrock", "src\libs\Providers\LangChain.Providers.Bedrock\LangChain.Providers.Bedrock.csproj", "{67985CCB-F606-41F8-9D36-513459F58882}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Amazon.IntegrationTests", "src\tests\LangChain.Providers.Bedrock.IntegrationTests\LangChain.Providers.Amazon.IntegrationTests.csproj", "{73C76E80-95C5-4C96-A319-4F32043C903E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Amazon.Bedrock", "src\libs\Providers\LangChain.Providers.Amazon.Bedrock\LangChain.Providers.Amazon.Bedrock.csproj", "{67985CCB-F606-41F8-9D36-513459F58882}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangChain.Providers.Amazon.SageMaker", "src\libs\Providers\LangChain.Providers.Amazon.Sagemaker\LangChain.Providers.Amazon.SageMaker.csproj", "{F1AD6925-219C-4B17-B8D8-0ACCA6F401C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Providers.Bedrock.IntegrationTests", "src\tests\LangChain.Providers.Bedrock.IntegrationTests\LangChain.Providers.Bedrock.IntegrationTests.csproj", "{4F37D6C5-38C9-485B-AD87-2691EED225E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -416,10 +414,6 @@ Global
{BA701280-0BEB-4DA4-92B3-9C777082C2AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA701280-0BEB-4DA4-92B3-9C777082C2AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA701280-0BEB-4DA4-92B3-9C777082C2AF}.Release|Any CPU.Build.0 = Release|Any CPU
{73C76E80-95C5-4C96-A319-4F32043C903E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73C76E80-95C5-4C96-A319-4F32043C903E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73C76E80-95C5-4C96-A319-4F32043C903E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73C76E80-95C5-4C96-A319-4F32043C903E}.Release|Any CPU.Build.0 = Release|Any CPU
{67985CCB-F606-41F8-9D36-513459F58882}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67985CCB-F606-41F8-9D36-513459F58882}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67985CCB-F606-41F8-9D36-513459F58882}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -428,6 +422,10 @@ Global
{F1AD6925-219C-4B17-B8D8-0ACCA6F401C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1AD6925-219C-4B17-B8D8-0ACCA6F401C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1AD6925-219C-4B17-B8D8-0ACCA6F401C4}.Release|Any CPU.Build.0 = Release|Any CPU
{4F37D6C5-38C9-485B-AD87-2691EED225E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F37D6C5-38C9-485B-AD87-2691EED225E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F37D6C5-38C9-485B-AD87-2691EED225E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F37D6C5-38C9-485B-AD87-2691EED225E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -496,8 +494,8 @@ Global
{4913844F-74EC-4E74-AE8A-EA825569E6BA} = {E55391DE-F8F3-4CC2-A0E3-2406C76E9C68}
{BF4C7B87-0997-4208-84EF-D368DF7B9861} = {E55391DE-F8F3-4CC2-A0E3-2406C76E9C68}
{A6CF79BC-8365-46E8-9230-1A4AD615D40B} = {FDEE2E22-C239-4921-83B2-9797F765FD6A}
{73C76E80-95C5-4C96-A319-4F32043C903E} = {FDEE2E22-C239-4921-83B2-9797F765FD6A}
{67985CCB-F606-41F8-9D36-513459F58882} = {E55391DE-F8F3-4CC2-A0E3-2406C76E9C68}
{4F37D6C5-38C9-485B-AD87-2691EED225E1} = {FDEE2E22-C239-4921-83B2-9797F765FD6A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5C00D0F1-6138-4ED9-846B-97E43D6DFF1C}
Expand Down
4 changes: 2 additions & 2 deletions examples/LangChain.Samples.Memory/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The following is a friendly conversation between a human and an AI.
Console.WriteLine();

Console.Write("Human: ");
var input = Console.ReadLine();
var input = Console.ReadLine() ?? string.Empty;
if (input == "exit")
{
break;
Expand Down Expand Up @@ -122,7 +122,7 @@ private static string PromptForChoice(string[] choiceTexts)
Console.WriteLine();
Console.Write("Enter choice: ");

string choiceEntry = Console.ReadLine();
string choiceEntry = Console.ReadLine() ?? string.Empty;
if (int.TryParse(choiceEntry, out int choiceIndex))
{
string choiceText = choiceTexts[choiceIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static async Task<VectorStoreIndexWrapper> CreateIndexFromDocuments(
return index;
}

public static SQLIteVectorStoreOptions DefaultOptions = new SQLIteVectorStoreOptions();
public static SQLIteVectorStoreOptions DefaultOptions { get; } = new();

/// <summary>
/// If database does not exists, it loads documents from the documentsSource, creates an index from these documents and returns the created index.
Expand All @@ -59,14 +59,16 @@ public static async Task<VectorStoreIndexWrapper> GetIndex(

TextSplitter textSplitter = new RecursiveCharacterTextSplitter(chunkSize: options.ChunkSize, chunkOverlap: options.ChunkOverlap);

if (!System.IO.File.Exists("vectors.db"))
if (!System.IO.File.Exists("vectors.db") && documentsSource != null)
{
var documents = await documentsSource.LoadAsync();
return await SQLiteVectorStore.CreateIndexFromDocuments(embeddings, documents, options.Filename, options.TableName, textSplitter: textSplitter);
var documents = await documentsSource.LoadAsync().ConfigureAwait(false);
return await CreateIndexFromDocuments(embeddings, documents, options.Filename, options.TableName, textSplitter: textSplitter).ConfigureAwait(false);
}


#pragma warning disable CA2000
var vectorStore = new SQLiteVectorStore(options.Filename, options.TableName, embeddings);
#pragma warning restore CA2000
var index = new VectorStoreIndexWrapper(vectorStore);
return index;
}
Expand Down
15 changes: 9 additions & 6 deletions src/libs/Extensions/LangChain.Extensions.Docker/Chain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using LangChain.Chains.HelperChains;

namespace LangChain.Extensions.Docker;
namespace LangChain.Extensions.Docker;

/// <summary>
///
Expand All @@ -11,12 +9,17 @@ public static class Chain
///
/// </summary>
/// <param name="image"></param>
/// <param name="filename"></param>
/// <param name="arguments"></param>
/// <param name="command"></param>
/// <param name="inputKey"></param>
/// <param name="attachVolume"></param>
/// <param name="outputKey"></param>
/// <returns></returns>
public static DockerChain RunCodeInDocker(string image= "python:3.10", string arguments = "main.py", string command = "python", string? attachVolume=null, string outputKey = "result")
public static DockerChain RunCodeInDocker(
string image= "python:3.10",
string arguments = "main.py",
string command = "python",
string? attachVolume = null,
string outputKey = "result")
{
return new DockerChain(image, arguments, command, attachVolume, outputKey);
}
Expand Down
12 changes: 10 additions & 2 deletions src/libs/Extensions/LangChain.Extensions.Docker/DockerChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public void Report(JSONMessage value)
/// <param name="image"></param>
/// <param name="arguments"></param>
/// <param name="command"></param>
/// <param name="attachVolume"></param>
/// <param name="outputKey"></param>
public DockerChain(string image= "python:3", string arguments="main.py", string command="python", string? attachVolume=null, string outputKey="result")
public DockerChain(
string image= "python:3",
string arguments="main.py",
string command="python",
string? attachVolume=null,
string outputKey="result")
{
Image = image;
Arguments = arguments;
Expand Down Expand Up @@ -77,7 +83,9 @@ await _client.Images.CreateImageAsync(new ImagesCreateParameters()

if (AttachVolume != null)
{
var absolutePath = Path.GetFullPath(AttachVolume).Replace("\\","/").Replace(":","");
var absolutePath = Path.GetFullPath(AttachVolume)
.Replace("\\","/", StringComparison.Ordinal)
.Replace(":","", StringComparison.Ordinal);
binds.Add($"/{absolutePath}:/app");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LangChain.Abstractions.Chains.Base;
using System.Globalization;
using LangChain.Abstractions.Chains.Base;
using LangChain.Abstractions.Schema;
using LangChain.Chains.HelperChains;
using LangChain.Chains.StackableChains.ReAct;
Expand Down Expand Up @@ -134,8 +135,8 @@ protected override async Task<IChainValues> InternalCall(IChainValues values)
if (res.Value[ReActAnswer] is AgentAction)
{
var action = (AgentAction)res.Value[ReActAnswer];
var tool = _tools[action.Action.ToLower()];
var toolRes = await tool.ToolTask(action.ActionInput);
var tool = _tools[action.Action.ToLower(CultureInfo.InvariantCulture)];
var toolRes = await tool.ToolTask(action.ActionInput).ConfigureAwait(false);
await _conversationBufferMemory.ChatHistory.AddMessage(new Message("Observation: " + toolRes, MessageRole.System))
.ConfigureAwait(false);
await _conversationBufferMemory.ChatHistory.AddMessage(new Message("Thought:", MessageRole.System))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ public class GoogleResult
{
public class Item
{
public string title { get; set; }
public string link { get; set; }
public string snippet { get; set; }
public string title { get; set; } = string.Empty;
public string link { get; set; } = string.Empty;
public string snippet { get; set; } = string.Empty;

}
public List<Item> items { get; set; }

public List<Item> items { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ void SaveCachedAnswer(string prompt, string answer)
File.WriteAllText(file, answer);
}

public override async Task<string> ToolTask(string input, CancellationToken token = default)
public override async Task<string> ToolTask(string input, CancellationToken cancellationToken = default)
{
string responseString;
string? responseString;
if (!useCache||(responseString = GetCachedAnswer(input))==null)
{
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://www.googleapis.com");
var response = await httpClient.GetAsync(
$"/customsearch/v1?key={key}&cx={cx}&q={input}", token).ConfigureAwait(false);
new Uri($"/customsearch/v1?key={key}&cx={cx}&q={input}"), cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
responseString = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public static class HookExtension
{
public static T Hook<T>(this T chain, Action<IChainValues> hook) where T : BaseStackableChain
{
chain = chain ?? throw new ArgumentNullException(nameof(chain));

chain.SetHook(hook);
return chain;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ string ExtractCode(string md)
var inCode = false;
foreach (var line in lines)
{
if (line.StartsWith("```"))
if (line.StartsWith("```", StringComparison.OrdinalIgnoreCase))
{
inCode = !inCode;
continue;
Expand All @@ -43,7 +43,7 @@ string ExtractCode(string md)

protected override Task<IChainValues> InternalCall(IChainValues values)
{

values = values ?? throw new ArgumentNullException(nameof(values));

if (values.Value[InputKeys[0]] is string text)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override Task<IChainValues> InternalCall(IChainValues values)
{
values = values ?? throw new ArgumentNullException(nameof(values));

string memoryVariableName = _chatMemory.MemoryVariables.FirstOrDefault();
var memoryVariableName = _chatMemory.MemoryVariables.FirstOrDefault();
if (memoryVariableName == null)
{
throw new InvalidOperationException("Missing memory variable name");
Expand Down
2 changes: 1 addition & 1 deletion src/libs/LangChain.Core/LangChain.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>LangChain</RootNamespace>
<NoWarn>$(NoWarn);CA1031;CA1822;CA1307;CA1002;CA1303;CA1003;CA1724</NoWarn>
<NoWarn>$(NoWarn);CA1031;CA1822;CA1307;CA1002;CA1303;CA1003;CA1724;CA1034;CA1849</NoWarn>
<NoWarn>$(NoWarn);CA2227;CA2214;CA1040;CA1812;CA1720;CS9107</NoWarn>
<NoWarn>$(NoWarn);CA1308;CA1862;CA1510;CA1850;CA1305;CA1725</NoWarn>
</PropertyGroup>
Expand Down
17 changes: 13 additions & 4 deletions src/libs/LangChain.Core/Memory/FileChatMessageHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ private async Task SaveMessages()
await Task.Run(() => File.WriteAllText(MessagesFilePath, json)).ConfigureAwait(false);
}

private async Task LoadMessages()
private Task LoadMessages()
{
if (File.Exists(MessagesFilePath))
try
{
string json = await Task.Run(() => File.ReadAllText(MessagesFilePath)).ConfigureAwait(false);
_messages = JsonSerializer.Deserialize<List<Message>>(json);
if (File.Exists(MessagesFilePath))
{
string json = File.ReadAllText(MessagesFilePath);
_messages = JsonSerializer.Deserialize<List<Message>>(json) ?? new List<Message>();
}

return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
}
2 changes: 1 addition & 1 deletion src/libs/LangChain.Core/Memory/MessageSummarizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public async Task<string> Summarize(IEnumerable<Message> newMessages, string exi
| Template(SummaryPrompt)
| LLM(Model);

return await chain.Run("text").ConfigureAwait(false);
return await chain.Run("text").ConfigureAwait(false) ?? string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ReSharper disable once CheckNamespace
namespace System.Net.Http;

public static partial class HttpContextExtensions
{
#if !NET6_0_OR_GREATER
public static async Task<string> ReadAsStringAsync(
this HttpContent content,
CancellationToken cancellationToken = default)
{
content = content ?? throw new ArgumentNullException(nameof(content));

return await content.ReadAsStringAsync().ConfigureAwait(false);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using LangChain.Indexes;
using LangChain.Prompts;
using LangChain.Providers.Amazon.Bedrock;
using LangChain.Providers.Amazon.Bedrock.Embeddings;
using LangChain.Providers.Amazon.Bedrock.Models;
using LangChain.Providers.Bedrock.Embeddings;
using LangChain.Providers.Bedrock.Models;
using LangChain.Schema;
using LangChain.Sources;
using LangChain.TextSplitters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ProjectReference Include="..\..\libs\LangChain.Core\LangChain.Core.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Amazon.Bedrock\LangChain.Providers.Amazon.Bedrock.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Amazon.Sagemaker\LangChain.Providers.Amazon.SageMaker.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Bedrock\LangChain.Providers.Amazon.Bedrock.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Bedrock\LangChain.Providers.Bedrock.csproj" />
<ProjectReference Include="..\..\libs\Sources\LangChain.Sources.Pdf\LangChain.Sources.Pdf.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<ProjectReference Include="..\..\libs\Databases\LangChain.Databases.InMemory\LangChain.Databases.InMemory.csproj" />
<ProjectReference Include="..\..\libs\Databases\LangChain.Databases.Sqlite\LangChain.Databases.Sqlite.csproj" />
<ProjectReference Include="..\..\libs\LangChain.Core\LangChain.Core.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Amazon.Bedrock\LangChain.Providers.Amazon.Bedrock.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Amazon.Sagemaker\LangChain.Providers.Amazon.SageMaker.csproj" />
<ProjectReference Include="..\..\libs\Providers\LangChain.Providers.Bedrock\LangChain.Providers.Bedrock.csproj" />
<ProjectReference Include="..\..\libs\Sources\LangChain.Sources.Pdf\LangChain.Sources.Pdf.csproj" />
</ItemGroup>

</Project>

0 comments on commit 6b6a02b

Please sign in to comment.