Skip to content

Commit f6388b0

Browse files
authored
Grammar refactored to support multiple formats (#94)
1 parent 9ea75bf commit f6388b0

36 files changed

+739
-652
lines changed

Examples/Examples/Chat/ChatCustomGrammarExample.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
using LLama.Sampling;
21
using MaIN.Core.Hub;
32
using MaIN.Domain.Entities;
3+
using MaIN.Domain.Models;
4+
using Grammar = MaIN.Domain.Models.Grammar;
45

5-
namespace Examples;
6+
namespace Examples.Chat;
67

78
public class ChatCustomGrammarExample : IExample
89
{
910
public async Task Start()
1011
{
1112
Console.WriteLine("ChatExample with grammar is running!");
1213

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-
""";
14+
var personGrammar = new Grammar("""
15+
root ::= person
16+
person ::= "{" ws "\"name\":" ws name "," ws "\"age\":" ws age "," ws "\"city\":" ws city ws "}"
17+
name ::= "\"" [A-Za-z ]+ "\""
18+
age ::= [1-9] | [1-9][0-9]
19+
city ::= "\"" [A-Za-z ]+ "\""
20+
ws ::= [ \t]*
21+
""", GrammarFormat.GBNF);
2122

2223
await AIHub.Chat()
23-
.WithInferenceParams(new InferenceParams()
24+
.WithInferenceParams(new InferenceParams
2425
{
2526
Grammar = personGrammar
2627
})

Examples/Examples/Chat/ChatExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using MaIN.Core.Hub;
22

3-
namespace Examples;
3+
namespace Examples.Chat;
44

55
public class ChatExample : IExample
66
{

Examples/Examples/Chat/ChatExampleAnthropic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Examples.Utils;
22
using MaIN.Core.Hub;
33

4-
namespace Examples;
4+
namespace Examples.Chat;
55

66
public class ChatExampleAnthropic : IExample
77
{

Examples/Examples/Chat/ChatExampleGemini.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Examples.Utils;
22
using MaIN.Core.Hub;
33

4-
namespace Examples;
4+
namespace Examples.Chat;
55

66
public class ChatExampleGemini : IExample
77
{

Examples/Examples/Chat/ChatExampleGroqCloud.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using Examples.Utils;
22
using MaIN.Core.Hub;
3-
using MaIN.Domain.Configuration;
43

5-
namespace Examples;
4+
namespace Examples.Chat;
65

76
public class ChatExampleGroqCloud : IExample
87
{

Examples/Examples/Chat/ChatExampleOpenAi.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using Examples.Utils;
22
using MaIN.Core.Hub;
3-
using MaIN.Domain.Configuration;
43

5-
namespace Examples;
4+
namespace Examples.Chat;
65

76
public class ChatExampleOpenAi : IExample
87
{

Examples/Examples/Chat/ChatFromExistingExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Text.Json;
22
using MaIN.Core.Hub;
33

4-
namespace Examples;
4+
namespace Examples.Chat;
55

66
public class ChatFromExistingExample : IExample
77
{
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Examples.Utils;
2+
using MaIN.Core.Hub;
3+
using MaIN.Domain.Entities;
4+
using MaIN.Domain.Models;
5+
6+
namespace Examples.Chat;
7+
8+
public class ChatGrammarExampleGemini : IExample
9+
{
10+
public async Task Start()
11+
{
12+
GeminiExample.Setup(); //We need to provide Gemini API key
13+
Console.WriteLine("(Gemini) ChatExample is running!");
14+
15+
var grammarValue = """
16+
{
17+
"$schema": "https://json-schema.org/draft/2020-12/schema",
18+
"title": "User",
19+
"type": "object",
20+
"properties": {
21+
"name": {
22+
"type": "string",
23+
"description": "Full name of the user."
24+
},
25+
"age": {
26+
"type": "integer",
27+
"minimum": 0,
28+
"description": "User's age in years."
29+
},
30+
"email": {
31+
"type": "string",
32+
"format": "email",
33+
"description": "User's email address."
34+
}
35+
},
36+
"required": ["name", "email"]
37+
}
38+
""";
39+
40+
await AIHub.Chat()
41+
.WithInferenceParams(new InferenceParams
42+
{
43+
Grammar = new Grammar(grammarValue, GrammarFormat.JSONSchema)
44+
})
45+
.WithModel("gemini-2.5-flash")
46+
.WithMessage("Generate random person")
47+
.CompleteAsync(interactive: true);
48+
}
49+
}

Examples/Examples/Chat/ChatWithFilesExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using MaIN.Core.Hub;
22

3-
namespace Examples;
3+
namespace Examples.Chat;
44

55
public class ChatWithFilesExample : IExample
66
{

Examples/Examples/Chat/ChatWithFilesExampleGemini.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Examples.Utils;
22
using MaIN.Core.Hub;
33

4-
namespace Examples;
4+
namespace Examples.Chat;
55

66
public class ChatWithFilesExampleGemini : IExample
77
{

0 commit comments

Comments
 (0)