Skip to content

Commit b90421f

Browse files
Merge pull request #12 from Thevakumar-Luheerathan/ballerina-lib-iss-8101
Improve support for runtime JSON schema generation
2 parents 8cc8fc5 + f75fcb0 commit b90421f

File tree

6 files changed

+446
-33
lines changed

6 files changed

+446
-33
lines changed

ballerina/Dependencies.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
[ballerina]
77
dependencies-toml-version = "2"
8-
distribution-version = "2201.12.0"
8+
distribution-version = "2201.12.7"
99

1010
[[package]]
1111
org = "ballerina"
1212
name = "ai"
13-
version = "1.1.0"
13+
version = "1.1.1"
1414
dependencies = [
1515
{org = "ballerina", name = "constraint"},
1616
{org = "ballerina", name = "data.jsondata"},

ballerina/tests/test_utils.bal

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@ isolated function getExpectedParameterSchema(string message) returns map<json> {
9191
};
9292
}
9393

94+
if message.startsWith("Give me a random joke about cricketers") {
95+
return expectedParameterSchemaForRecUnionBasicType;
96+
}
97+
98+
if message.startsWith("Give me a random joke") {
99+
return {"type":"object","properties":{"result":{"anyOf":[{"type":"string"},{"type":"null"}]}}};
100+
}
101+
102+
if message.startsWith("Name a random world class cricketer in India") {
103+
return expectedParameterSchemaForRecUnionNull;
104+
}
105+
106+
if message.startsWith("Name 10 world class cricketers in India") {
107+
return expectedParameterSchemaForArrayOnly;
108+
}
109+
110+
if message.startsWith("Name 10 world class cricketers as string") {
111+
return expectedParameterSchemaForArrayUnionBasicType;
112+
}
113+
114+
if message.startsWith("Name top 10 world class cricketers") {
115+
return expectedParameterSchemaForArrayUnionRec;
116+
}
117+
118+
if message.startsWith("Name a random world class cricketer") {
119+
return expectedParameterSchemaForArrayUnionRec;
120+
}
121+
122+
if message.startsWith("Name 10 world class cricketers") {
123+
return expectedParamSchemaForArrayUnionNull;
124+
}
125+
94126
return {};
95127
}
96128

@@ -167,6 +199,52 @@ isolated function getTheMockLLMResult(string message) returns map<json> {
167199
}
168200
}
169201

202+
if message.startsWith("Name a random world class cricketer in India") {
203+
return {"result": {"name": "Sanga"}};
204+
}
205+
206+
if message.startsWith("Name a random world class cricketer") {
207+
return {"result": {"name": "Sanga"}};
208+
}
209+
210+
if message.startsWith("Name 10 world class cricketers") {
211+
return {
212+
"result": [
213+
{"name": "Virat Kohli"},
214+
{"name": "Joe Root"},
215+
{"name": "Steve Smith"},
216+
{"name": "Kane Williamson"},
217+
{"name": "Babar Azam"},
218+
{"name": "Ben Stokes"},
219+
{"name": "Jasprit Bumrah"},
220+
{"name": "Pat Cummins"},
221+
{"name": "Shaheen Afridi"},
222+
{"name": "Rashid Khan"}
223+
]
224+
};
225+
}
226+
227+
if message.startsWith("Name top 10 world class cricketers") {
228+
return {
229+
"result": [
230+
{"name": "Virat Kohli"},
231+
{"name": "Joe Root"},
232+
{"name": "Steve Smith"},
233+
{"name": "Kane Williamson"},
234+
{"name": "Babar Azam"},
235+
{"name": "Ben Stokes"},
236+
{"name": "Jasprit Bumrah"},
237+
{"name": "Pat Cummins"},
238+
{"name": "Shaheen Afridi"},
239+
{"name": "Rashid Khan"}
240+
]
241+
};
242+
}
243+
244+
if message.startsWith("Give me a random joke") {
245+
return {"result": "This is a random joke"};
246+
}
247+
170248
return {};
171249
}
172250

@@ -246,5 +324,45 @@ isolated function getExpectedPrompt(string message) returns string {
246324
their name?`;
247325
}
248326

327+
if message.startsWith("Name 10 world class cricketers in India") {
328+
return "Name 10 world class cricketers in India\nYou must call the `getResults`" +
329+
" tool to obtain the correct answer.";
330+
}
331+
332+
if message.startsWith("Name 10 world class cricketers as string") {
333+
return "Name 10 world class cricketers as string\nYou must call the `getResults`" +
334+
" tool to obtain the correct answer.";
335+
}
336+
337+
if message.startsWith("Name 10 world class cricketers") {
338+
return "Name 10 world class cricketers\nYou must call the `getResults`" +
339+
" tool to obtain the correct answer.";
340+
}
341+
342+
if message.startsWith("Name top 10 world class cricketers") {
343+
return "Name top 10 world class cricketers\nYou must call the `getResults`" +
344+
" tool to obtain the correct answer.";
345+
}
346+
347+
if message.startsWith("Name a random world class cricketer in India") {
348+
return "Name a random world class cricketer in India\nYou must call the `getResults`" +
349+
" tool to obtain the correct answer.";
350+
}
351+
352+
if message.startsWith("Name a random world class cricketer") {
353+
return "Name a random world class cricketer\nYou must call the `getResults`" +
354+
" tool to obtain the correct answer.";
355+
}
356+
357+
if message.startsWith("Give me a random joke about cricketers") {
358+
return "Give me a random joke about cricketers\nYou must call the `getResults`" +
359+
" tool to obtain the correct answer.";
360+
}
361+
362+
if message.startsWith("Give me a random joke") {
363+
return "Give me a random joke\nYou must call the `getResults`" +
364+
" tool to obtain the correct answer.";
365+
}
366+
249367
return "INVALID";
250368
}

ballerina/tests/test_values.bal

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,168 @@ const expectedParamterSchemaStringForBalProgram =
167167

168168
const expectedParamterSchemaStringForCountry =
169169
{"type": "object", "properties": {"result": {"type": "string"}}};
170+
171+
172+
173+
const expectedParamSchemaForArrayUnionNull =
174+
{
175+
"type": "object",
176+
"properties": {
177+
"result": {
178+
"anyOf": [
179+
{
180+
"type": "array",
181+
"items": {
182+
"required": [
183+
"name"
184+
],
185+
"type": "object",
186+
"properties": {
187+
"name": {
188+
"type": "string"
189+
}
190+
}
191+
}
192+
},
193+
{
194+
"type": "null"
195+
}
196+
]
197+
}
198+
}
199+
};
200+
201+
const expectedParameterSchemaForArrayUnionRec =
202+
{
203+
"type": "object",
204+
"properties": {
205+
"result": {
206+
"anyOf": [
207+
{
208+
"type": "array",
209+
"items": {
210+
"required": [
211+
"name"
212+
],
213+
"type": "object",
214+
"properties": {
215+
"name": {
216+
"type": "string"
217+
}
218+
}
219+
}
220+
},
221+
{
222+
"required": [
223+
"name"
224+
],
225+
"type": "object",
226+
"properties": {
227+
"name": {
228+
"type": "string"
229+
}
230+
}
231+
}
232+
]
233+
}
234+
}
235+
};
236+
237+
const expectedParameterSchemaForArrayUnionBasicType =
238+
{
239+
"type": "object",
240+
"properties": {
241+
"result": {
242+
"anyOf": [
243+
{
244+
"type": "array",
245+
"items": {
246+
"required": [
247+
"name"
248+
],
249+
"type": "object",
250+
"properties": {
251+
"name": {
252+
"type": "string"
253+
}
254+
}
255+
}
256+
},
257+
{
258+
"type": "string"
259+
}
260+
]
261+
}
262+
}
263+
};
264+
265+
const expectedParameterSchemaForArrayOnly =
266+
{
267+
"type": "object",
268+
"properties": {
269+
"result": {
270+
"type": "array",
271+
"items": {
272+
"required": [
273+
"name"
274+
],
275+
"type": "object",
276+
"properties": {
277+
"name": {
278+
"type": "string"
279+
}
280+
}
281+
}
282+
}
283+
}
284+
};
285+
286+
const expectedParameterSchemaForRecUnionBasicType =
287+
{
288+
"type": "object",
289+
"properties": {
290+
"result": {
291+
"anyOf": [
292+
{
293+
"required": [
294+
"name"
295+
],
296+
"type": "object",
297+
"properties": {
298+
"name": {
299+
"type": "string"
300+
}
301+
}
302+
},
303+
{
304+
"type": "string"
305+
}
306+
]
307+
}
308+
}
309+
};
310+
311+
const expectedParameterSchemaForRecUnionNull =
312+
{
313+
"type": "object",
314+
"properties": {
315+
"result": {
316+
"anyOf": [
317+
{
318+
"required": [
319+
"name"
320+
],
321+
"type": "object",
322+
"properties": {
323+
"name": {
324+
"type": "string"
325+
}
326+
}
327+
},
328+
{
329+
"type": "null"
330+
}
331+
]
332+
}
333+
}
334+
};

0 commit comments

Comments
 (0)