Skip to content

Commit 4a91cea

Browse files
Merge pull request #62 from wisedev-code/43-include-grammar-in-chat-agent-contexts
feat: include grammar in chat agent contexts
2 parents 7e6d116 + a462b2d commit 4a91cea

15 files changed

Lines changed: 91 additions & 17 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using LLama.Sampling;
2+
using MaIN.Core.Hub;
3+
using MaIN.Domain.Entities;
4+
5+
namespace Examples;
6+
7+
public class ChatCustomGrammarExample : IExample
8+
{
9+
public async Task Start()
10+
{
11+
Console.WriteLine("ChatExample with grammar is running!");
12+
13+
var personGrammar = """
14+
root ::= person
15+
person ::= "{" ws "\"name\":" ws name "," ws "\"age\":" ws age "," ws "\"city\":" ws city ws "}"
16+
name ::= "\"" [A-Za-z ]+ "\""
17+
age ::= [1-9] | [1-9][0-9]
18+
city ::= "\"" [A-Za-z ]+ "\""
19+
ws ::= [ \t]*
20+
""";
21+
22+
await AIHub.Chat()
23+
.WithInferenceParams(new InferenceParams()
24+
{
25+
Grammar = personGrammar
26+
})
27+
.WithModel("gemma2:2b")
28+
.WithMessage("Generate random person")
29+
.CompleteAsync(interactive: true);
30+
}
31+
}

Examples/Examples/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void RegisterExamples(IServiceCollection services)
4242
services.AddTransient<ExampleRegistry>();
4343
services.AddTransient<McpExample>();
4444
services.AddTransient<ChatExample>();
45+
services.AddTransient<ChatCustomGrammarExample>();
4546
services.AddTransient<ChatWithFilesExample>();
4647
services.AddTransient<ChatWithFilesFromStreamExample>();
4748
services.AddTransient<ChatWithVisionExample>();
@@ -128,6 +129,7 @@ public class ExampleRegistry(IServiceProvider serviceProvider)
128129
{
129130
("\u25a0 Basic Chat", serviceProvider.GetRequiredService<ChatExample>()),
130131
("\u25a0 Chat with Files", serviceProvider.GetRequiredService<ChatWithFilesExample>()),
132+
("\u25a0 Chat with custom grammar", serviceProvider.GetRequiredService<ChatCustomGrammarExample>()),
131133
("\u25a0 Chat with Files from stream", serviceProvider.GetRequiredService<ChatWithFilesFromStreamExample>()),
132134
("\u25a0 Chat with Vision", serviceProvider.GetRequiredService<ChatWithVisionExample>()),
133135
("\u25a0 Chat with Image Generation", serviceProvider.GetRequiredService<ChatWithImageGenExample>()),

Releases/0.2.7.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 0.2.7 release
2+
3+
- Include grammar as inference param (allow you to enforce format that model should use for response)
4+
- New example
5+
- Fix issues with "infer" cli command (visual models for openai & gemini)

src/MaIN.Core/.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>MaIN.NET</id>
5-
<version>0.2.6</version>
5+
<version>0.2.7</version>
66
<authors>Wisedev</authors>
77
<owners>Wisedev</owners>
88
<icon>favicon.png</icon>

src/MaIN.Domain/Entities/InferenceParams.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using LLama.Sampling;
2+
13
namespace MaIN.Domain.Entities;
24

35
public class InferenceParams
@@ -17,4 +19,5 @@ public class InferenceParams
1719

1820
public int TopK { get; init; } = 40;
1921
public float TopP { get; init; } = 0.9f;
22+
public string? Grammar { get; init; }
2023
}

src/MaIN.Domain/Entities/MemoryParams.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public class MemoryParams
1616
public int AnswerTokens { get; set; } = 500;
1717

1818
public bool MultiModalMode { get; set; } = false;
19+
20+
public string? Grammar { get; init; }
1921
}

src/MaIN.InferPage/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
settings.BackendType = BackendType.OpenAi;
8282
});
8383
}
84-
if (Utils.Gemini)
84+
else if (Utils.Gemini)
8585
{
8686
builder.Services.AddMaIN(builder.Configuration, settings =>
8787
{

src/MaIN.InferPage/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static class Utils
66
{
77
public static string? Model = "gemma2:2b";
88
public static bool Visual => VisualModels.Contains(Model);
9-
private static readonly string[] VisualModels = ["FLUX.1_Shnell", "dall-e-3"];
9+
private static readonly string[] VisualModels = ["FLUX.1_Shnell", "FLUX.1", "dall-e-3", "dall-e", "imagen", "imagen-3"]; //user might type different names
1010
public static bool OpenAi { get; set; }
1111
public static bool Gemini { get; set; }
1212
public static string? Path { get; set; }

src/MaIN.Infrastructure/Models/InterferenceParamsDocument.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using LLama.Sampling;
2+
13
namespace MaIN.Infrastructure.Models;
24

35
public class InferenceParamsDocument
@@ -15,4 +17,5 @@ public class InferenceParamsDocument
1517
public int MaxTokens { get; set; }
1618
public int TopK { get; init; }
1719
public float TopP { get; init; }
20+
public string? Grammar { get; set; }
1821
}

src/MaIN.Infrastructure/Models/MemoryParamsDocument.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public class MemoryParamsDocument
99
public float Temperature { get; set; }
1010
public int AnswerTokens { get; set; }
1111
public bool MultiModalMode { get; set; }
12+
public string? Grammar { get; set; }
1213
}

0 commit comments

Comments
 (0)