-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
feat: added AiLabs Jamba model. updated Amazon.Bedrock nugets #376
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c45103a
feat: added Amazon Bedrock Titan Text Premier model
curlyfro 96dcb63
Merge branch 'main' of github.com:curlyfro/LangChain
curlyfro 73a80af
Merge branch 'main' of github.com:curlyfro/LangChain
curlyfro 7acbb7f
feat: added AiLabs Jamba model. updated Amazon.Bedrock nugets
curlyfro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Providers/Amazon.Bedrock/src/Chat/Ai21LabsJambaChatModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Diagnostics; | ||
using System.Text.Json.Nodes; | ||
using LangChain.Providers.Amazon.Bedrock.Internal; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace LangChain.Providers.Amazon.Bedrock; | ||
|
||
public class Ai21LabsJambaChatModel( | ||
BedrockProvider provider, | ||
string id) | ||
: ChatModel(id) | ||
{ | ||
/// <summary> | ||
/// Generates a chat response based on the provided `ChatRequest`. | ||
/// </summary> | ||
/// <param name="request">The `ChatRequest` containing the input messages and other parameters.</param> | ||
/// <param name="settings">Optional `ChatSettings` to override the model's default settings.</param> | ||
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param> | ||
/// <returns>A `ChatResponse` containing the generated messages and usage information.</returns> | ||
public override async Task<ChatResponse> GenerateAsync( | ||
ChatRequest request, | ||
ChatSettings? settings = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
request = request ?? throw new ArgumentNullException(nameof(request)); | ||
|
||
var watch = Stopwatch.StartNew(); | ||
var prompt = request.Messages.ToSimplePrompt(); | ||
|
||
var usedSettings = Ai21LabJambaChatSettings.Calculate( | ||
requestSettings: settings, | ||
modelSettings: Settings, | ||
providerSettings: provider.ChatSettings); | ||
|
||
var bodyJson = CreateBodyJson(prompt, usedSettings); | ||
|
||
var response = await provider.Api.InvokeModelAsync(Id, bodyJson, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
var generatedText = response?["choices"]?.AsArray() | ||
[0]?["message"]?.AsObject() | ||
.AsObject()["content"]?.GetValue<string>() ?? ""; | ||
|
||
var result = request.Messages.ToList(); | ||
result.Add(generatedText.AsAiMessage()); | ||
|
||
var usage = Usage.Empty with | ||
{ | ||
Time = watch.Elapsed, | ||
}; | ||
AddUsage(usage); | ||
provider.AddUsage(usage); | ||
|
||
return new ChatResponse | ||
{ | ||
Messages = result, | ||
UsedSettings = usedSettings, | ||
Usage = usage, | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Creates the request body JSON for the Ai21Labs model based on the provided prompt and settings. | ||
/// </summary> | ||
/// <param name="prompt">The input prompt for the model.</param> | ||
/// <param name="usedSettings">The settings to use for the request.</param> | ||
/// <returns>A `JsonObject` representing the request body.</returns> | ||
private static JsonObject CreateBodyJson(string prompt, Ai21LabJambaChatSettings usedSettings) | ||
{ | ||
var bodyJson = new JsonObject | ||
{ | ||
["messages"] = new JsonArray | ||
{ | ||
new JsonObject | ||
{ | ||
["role"] = "user", | ||
["content"] = prompt | ||
} | ||
}, | ||
["max_tokens"] = usedSettings.MaxTokens!.Value, | ||
["top_p"] = usedSettings.TopP!.Value, | ||
["temperature"] = usedSettings.Temperature!.Value, | ||
|
||
}; | ||
return bodyJson; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Providers/Amazon.Bedrock/src/Chat/Settings/Ai21LabJambaChatSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace LangChain.Providers.Amazon.Bedrock; | ||
|
||
public class Ai21LabJambaChatSettings : BedrockChatSettings | ||
{ | ||
public new static Ai21LabJambaChatSettings Default { get; } = new() | ||
{ | ||
StopSequences = ChatSettings.Default.StopSequences, | ||
User = ChatSettings.Default.User, | ||
UseStreaming = false, | ||
Temperature = 0.7, | ||
MaxTokens = 4000, | ||
TopP = 0.8, | ||
TopK = 0.0 | ||
}; | ||
|
||
/// <summary> | ||
/// Calculate the settings to use for the request. | ||
/// </summary> | ||
/// <param name="requestSettings"></param> | ||
/// <param name="modelSettings"></param> | ||
/// <param name="providerSettings"></param> | ||
/// <returns></returns> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
public new static Ai21LabJambaChatSettings Calculate( | ||
ChatSettings? requestSettings, | ||
ChatSettings? modelSettings, | ||
ChatSettings? providerSettings) | ||
{ | ||
var requestSettingsCasted = requestSettings as Ai21LabJambaChatSettings; | ||
var modelSettingsCasted = modelSettings as Ai21LabJambaChatSettings; | ||
var providerSettingsCasted = providerSettings as Ai21LabJambaChatSettings; | ||
|
||
return new Ai21LabJambaChatSettings | ||
{ | ||
StopSequences = | ||
requestSettingsCasted?.StopSequences ?? | ||
modelSettingsCasted?.StopSequences ?? | ||
providerSettingsCasted?.StopSequences ?? | ||
Default.StopSequences ?? | ||
throw new InvalidOperationException("Default StopSequences is not set."), | ||
User = | ||
requestSettingsCasted?.User ?? | ||
modelSettingsCasted?.User ?? | ||
providerSettingsCasted?.User ?? | ||
Default.User ?? | ||
throw new InvalidOperationException("Default User is not set."), | ||
UseStreaming = | ||
requestSettings?.UseStreaming ?? | ||
modelSettings?.UseStreaming ?? | ||
providerSettings?.UseStreaming ?? | ||
Default.UseStreaming ?? | ||
throw new InvalidOperationException("Default UseStreaming is not set."), | ||
Temperature = | ||
requestSettingsCasted?.Temperature ?? | ||
modelSettingsCasted?.Temperature ?? | ||
providerSettingsCasted?.Temperature ?? | ||
Default.Temperature ?? | ||
throw new InvalidOperationException("Default Temperature is not set."), | ||
MaxTokens = | ||
requestSettingsCasted?.MaxTokens ?? | ||
modelSettingsCasted?.MaxTokens ?? | ||
providerSettingsCasted?.MaxTokens ?? | ||
Default.MaxTokens ?? | ||
throw new InvalidOperationException("Default MaxTokens is not set."), | ||
TopP = | ||
requestSettingsCasted?.TopP ?? | ||
modelSettingsCasted?.TopP ?? | ||
providerSettingsCasted?.TopP ?? | ||
Default.TopP ?? | ||
throw new InvalidOperationException("Default TopP is not set."), | ||
TopK = | ||
requestSettingsCasted?.TopK ?? | ||
modelSettingsCasted?.TopK ?? | ||
providerSettingsCasted?.TopK ?? | ||
Default.TopK ?? | ||
throw new InvalidOperationException("Default TopK is not set."), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,11 @@ | |
<ProjectReference Include="..\..\Abstractions\src\LangChain.Providers.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the future - this has already been applied to all packages by default via the Directory.Build.props file |
||
<PackageReference Update="DotNet.ReproducibleBuilds"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method
GenerateAsync
looks good!Consider adding error handling and logging to improve robustness and traceability.