Skip to content

Commit

Permalink
feat: Add support for LLM Compute Configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Thorsten Hans <[email protected]>
  • Loading branch information
ThorstenHans committed May 30, 2024
1 parent 92ccb92 commit 855f3b7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Aspire.Hosting.Spin.Tests/SpinRuntimeConfigurationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,26 @@ public async Task ItShouldSetFileNameOnRuntimeConfiguration()
.Build();
actual.Name.Should().Be(name);
}

[Theory]
[InlineData("", "")]
[InlineData(" ", " ")]
[InlineData(null, null)]
[InlineData("https://some.url", "")]
[InlineData("https://some.url", null)]
[InlineData("https://some.url", " ")]
[InlineData("", "sdfsf")]
[InlineData(" ", "sdfsf")]
[InlineData(null, "sdfsf")]

public async Task ItShouldThrowWhenLLMConfigurationIsProvidedNullOrEmpty(string url, string token)
{
var action = async () =>
{
await SpinRuntimeConfigurationBuilder.Create("foo.yaml")
.WithLLM(url, token)
.Build();
};
await action.Should().ThrowAsync<ArgumentException>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text;

namespace Aspire.Hosting;

public class LargeLanguageModelCompute : ITomlize, IEquatable<LargeLanguageModelCompute>
{
public LargeLanguageModelCompute(string url, string token)
{
Url = url;
Token = token;
}

public string Url { get; }
public string Token { get; }

public bool Equals(LargeLanguageModelCompute? other)
{
if (other == null) return false;
return ReferenceEquals(this, other) || (Url.Equals(other.Url) && Token.Equals(other.Token));
}

public string ToToml()
{
var builder = new StringBuilder();
builder.AppendLine("type = \"remote_http\"");
builder.AppendLine($"url = \"{Url}\"");
builder.AppendLine($"auth_token = \"{Token}\"");
return builder.ToString();
}

public override int GetHashCode()
{
const int prime1 = 17;
const int prime2 = 31;
var hash = prime1;
hash = hash * prime2 + Url.GetHashCode();
hash = hash * prime2 + Token.GetHashCode();
return hash;
}

public override bool Equals(object? obj)
{
return Equals(obj as LargeLanguageModelCompute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ internal RuntimeConfiguration(string name)
Name = name;
KeyValueStores = new Dictionary<string, KeyValueStore>();
SqliteDatabases = new Dictionary<string, SqliteDatabase>();
LLMCompute = null;
}

// todo: consider hiding the dictionaries and expose only necessary APIs
public IDictionary<string, KeyValueStore> KeyValueStores { get; set; }
public IDictionary<string, SqliteDatabase> SqliteDatabases { get; set; }

public LargeLanguageModelCompute? LLMCompute { get; set; }
public string Name { get; private set; }

public string ToToml()
Expand All @@ -34,6 +35,12 @@ public string ToToml()
builder.AppendLine(sqlite.Value.ToToml());
}

if (LLMCompute != null)
{
builder.AppendLine("[llm_compute]");
builder.AppendLine(LLMCompute.ToToml());
}

return builder.ToString();
}
}
15 changes: 15 additions & 0 deletions Aspire.Hosting.Spin/SpinRuntimeConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class SpinRuntimeConfigurationBuilder
private readonly IDictionary<string, string> _keyValueStores;
private string _name = null!;
private readonly IDictionary<string, string> _sqliteDatabases;
private string _llmUrl;
private string _llmToken;

private SpinRuntimeConfigurationBuilder()
{
Expand Down Expand Up @@ -47,6 +49,14 @@ public SpinRuntimeConfigurationBuilder WithSqliteDatabase(string name, string pa
return this;
}

public SpinRuntimeConfigurationBuilder WithLLM(string url, string token)
{
ArgumentException.ThrowIfNullOrWhiteSpace(url);
ArgumentException.ThrowIfNullOrWhiteSpace(token);
_llmUrl = url;
_llmToken = token;
return this;
}

public async Task<RuntimeConfiguration> Build()
{
Expand All @@ -60,6 +70,11 @@ public async Task<RuntimeConfiguration> Build()
foreach (var kv in _keyValueStores) cfg.KeyValueStores.Add(kv.Key, new SpinKeyValueStore(kv.Value));

foreach (var sqlite in _sqliteDatabases) cfg.SqliteDatabases.Add(sqlite.Key, new SqliteDatabase(sqlite.Value));

if (!string.IsNullOrWhiteSpace(_llmUrl))
{
cfg.LLMCompute = new LargeLanguageModelCompute(_llmUrl, _llmToken);
}
return cfg;
}
}

0 comments on commit 855f3b7

Please sign in to comment.