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

Serve example #146

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions examples/LangChain.Samples.Serve/LangChain.Samples.Serve.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\libs\LangChain.Core\LangChain.Core.csproj" />
<ProjectReference Include="..\..\src\libs\Providers\LangChain.Providers.Ollama\LangChain.Providers.Ollama.csproj" />
<ProjectReference Include="..\..\src\libs\Utilities\LangChain.Serve\LangChain.Serve.csproj" />
</ItemGroup>

</Project>
92 changes: 92 additions & 0 deletions examples/LangChain.Samples.Serve/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using LangChain.Memory;
using LangChain.Providers;
using LangChain.Providers.Ollama;
using LangChain.Serve;
using LangChain.Utilities.Classes.Repository;
using static LangChain.Chains.Chain;
using Message = LangChain.Providers.Message;


var builder = WebApplication.CreateBuilder();



// 1. Add LangChainServe
builder.Services.AddLangChainServe();

// 2. Create a model
var model = new OllamaChatModel(new OllamaProvider(options:new OllamaOptions()
{
Temperature = 0,
Stop = new[] { "User:" },
}),"mistral:latest");


// 3. Optional. Add custom name generator
// After initiating conversation, this will generate a name for it
// If skipped, the conversation will be stored with current datetime as a name
builder.Services.AddCustomNameGenerator(async messages =>
{
var template =
@"You will be given conversation between User and Assistant. Your task is to give name to this conversation using maximum 3 words
Conversation:
{chat_history}
Your name: ";
var conversationBufferMemory = await ConvertToConversationBuffer(messages);
var chain = LoadMemory(conversationBufferMemory, "chat_history")
| Template(template)
| LLM(model);
return await chain.Run("text");

});

// 4. Optional. Add swagger to be able to test the API
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// 5. Configure LangChainServe
app.UseLangChainServe(options =>
{
// Register a model. Can be called multiple times to register multiple models
// this will receive entire conversation and should return a response
options.RegisterModel("Test model", async (messages) =>
{
var template = @"You are helpful assistant. Keep your answers short.
{chat_history}
Assistant:";
// Convert messages to use with conversation buffer memory
var conversationBufferMemory = await ConvertToConversationBuffer(messages);
var chain = LoadMemory(conversationBufferMemory, "chat_history")
| Template(template)
| LLM(model);

// get response and send it as AI answer
var response = await chain.Run("text");
return new StoredMessage()
{
Author = MessageAuthor.AI,
Content = response
};
});
});

// 6. Optional. Add swagger middleware
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.Run();

async Task<ConversationBufferMemory> ConvertToConversationBuffer(List<StoredMessage> list)
{
var conversationBufferMemory = new ConversationBufferMemory();
conversationBufferMemory.Formatter.HumanPrefix = "User";
conversationBufferMemory.Formatter.AiPrefix = "Assistant";
List<Message> converted = list
.Select(x => new Message(x.Content, x.Author == MessageAuthor.User ? MessageRole.Human : MessageRole.Ai)).ToList();
await conversationBufferMemory.ChatHistory.AddMessages(converted);
return conversationBufferMemory;
}
29 changes: 29 additions & 0 deletions examples/LangChain.Samples.Serve/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:26462",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions examples/LangChain.Samples.Serve/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions examples/LangChain.Samples.Serve/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Loading