Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.12.0"
distribution-version = "2201.12.7"

[[package]]
org = "ballerina"
name = "ai"
version = "1.1.0"
version = "1.1.1"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "data.jsondata"},
Expand Down
84 changes: 84 additions & 0 deletions ballerina/tests/test_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,38 @@ isolated function getExpectedParameterSchema(string message) returns map<json> {
};
}

if message.startsWith("Give me a random joke about cricketers") {
return expectedParameterSchemaForRecUnionBasicType;
}

if message.startsWith("Give me a random joke") {
return {"type":"object","properties":{"result":{"anyOf":[{"type":"string"},{"type":"null"}]}}};
}

if message.startsWith("Name a random world class cricketer in India") {
return expectedParameterSchemaForRecUnionNull;
}

if message.startsWith("Name 10 world class cricketers in India") {
return expectedParameterSchemaForArrayOnly;
}

if message.startsWith("Name 10 world class cricketers as string") {
return expectedParameterSchemaForArrayUnionBasicType;
}

if message.startsWith("Name top 10 world class cricketers") {
return expectedParameterSchemaForArrayUnionRec;
}

if message.startsWith("Name a random world class cricketer") {
return expectedParameterSchemaForArrayUnionRec;
}

if message.startsWith("Name 10 world class cricketers") {
return expectedParamSchemaForArrayUnionNull;
}

return {};
}

Expand Down Expand Up @@ -154,6 +186,26 @@ isolated function getTheMockLLMResult(string message) returns string {
return review;
}

if message.startsWith("Name a random world class cricketer in India") {
return "{\"result\": {\"name\": \"Sanga\"}}";
}

if message.startsWith("Name a random world class cricketer") {
return "{\"result\": {\"name\": \"Sanga\"}}";
}

if message.startsWith("Name 10 world class cricketers") {
return "{\"result\": [{\"name\": \"Virat Kohli\"}, {\"name\": \"Joe Root\"}, {\"name\": \"Steve Smith\"}, {\"name\": \"Kane Williamson\"}, {\"name\": \"Babar Azam\"}, {\"name\": \"Ben Stokes\"}, {\"name\": \"Jasprit Bumrah\"}, {\"name\": \"Pat Cummins\"}, {\"name\": \"Shaheen Afridi\"}, {\"name\": \"Rashid Khan\"}]}";
}

if message.startsWith("Name top 10 world class cricketers") {
return "{\"result\": [{\"name\": \"Virat Kohli\"}, {\"name\": \"Joe Root\"}, {\"name\": \"Steve Smith\"}, {\"name\": \"Kane Williamson\"}, {\"name\": \"Babar Azam\"}, {\"name\": \"Ben Stokes\"}, {\"name\": \"Jasprit Bumrah\"}, {\"name\": \"Pat Cummins\"}, {\"name\": \"Shaheen Afridi\"}, {\"name\": \"Rashid Khan\"}]}";
}

if message.startsWith("Give me a random joke") {
return "{\"result\": \"This is a random joke\"}";
}

return "INVALID";
}

Expand Down Expand Up @@ -246,5 +298,37 @@ isolated function getExpectedPrompt(string message) returns string {
their name?`;
}

if message.startsWith("Name 10 world class cricketers in India") {
return "Name 10 world class cricketers in India";
}

if message.startsWith("Name 10 world class cricketers as string") {
return "Name 10 world class cricketers as string";
}

if message.startsWith("Name 10 world class cricketers") {
return "Name 10 world class cricketers";
}

if message.startsWith("Name top 10 world class cricketers") {
return "Name top 10 world class cricketers";
}

if message.startsWith("Name a random world class cricketer in India") {
return "Name a random world class cricketer in India";
}

if message.startsWith("Name a random world class cricketer") {
return "Name a random world class cricketer";
}

if message.startsWith("Give me a random joke about cricketers") {
return "Give me a random joke about cricketers";
}

if message.startsWith("Give me a random joke") {
return "Give me a random joke";
}

return "INVALID";
}
163 changes: 163 additions & 0 deletions ballerina/tests/test_values.bal
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,166 @@ const expectedParamterSchemaStringForBalProgram =

const expectedParamterSchemaStringForCountry =
{"type": "object", "properties": {"result": {"type": "string"}}};

const expectedParamSchemaForArrayUnionNull =
{
"type": "object",
"properties": {
"result": {
"anyOf": [
{
"type": "array",
"items": {
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
},
{
"type": "null"
}
]
}
}
};

const expectedParameterSchemaForArrayUnionRec =
{
"type": "object",
"properties": {
"result": {
"anyOf": [
{
"type": "array",
"items": {
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
},
{
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
]
}
}
};

const expectedParameterSchemaForArrayUnionBasicType =
{
"type": "object",
"properties": {
"result": {
"anyOf": [
{
"type": "array",
"items": {
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
},
{
"type": "string"
}
]
}
}
};

const expectedParameterSchemaForArrayOnly =
{
"type": "object",
"properties": {
"result": {
"type": "array",
"items": {
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
};

const expectedParameterSchemaForRecUnionBasicType =
{
"type": "object",
"properties": {
"result": {
"anyOf": [
{
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
{
"type": "string"
}
]
}
}
};

const expectedParameterSchemaForRecUnionNull =
{
"type": "object",
"properties": {
"result": {
"anyOf": [
{
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
{
"type": "null"
}
]
}
}
};
89 changes: 88 additions & 1 deletion ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type ProductName record {|

@test:Config
function testGenerateMethodWithInvalidRecordType() returns ai:Error? {
ProductName[]|error rating = trap provider->generate(
ProductName[]|map<string>|error rating = trap provider->generate(
`Tell me name and the age of the top 10 world class cricketers`);
string msg = (<error>rating).message();
test:assertTrue(rating is error);
Expand All @@ -155,3 +155,90 @@ function testGenerateMethodWithInvalidRecordArrayType2() returns ai:Error? {
test:assertTrue(rating is error);
test:assertTrue((<error>rating).message().includes(ERROR_MESSAGE));
}


type Cricketers record {|
string name;
|};

type Cricketers1 record {|
string name;
|};

type Cricketers2 record {|
string name;
|};

type Cricketers3 record {|
string name;
|};

type Cricketers4 record {|
string name;
|};

type Cricketers5 record {|
string name;
|};

type Cricketers6 record {|
string name;
|};

type Cricketers7 record {|
string name;
|};

type Cricketers8 record {|
string name;
|};

@test:Config
function testGenerateMethodWithStringUnionNull() returns error? {
string? result = check provider->generate(`Give me a random joke`);
test:assertTrue(result is string);
}

@test:Config
function testGenerateMethodWithRecUnionBasicType() returns error? {
Cricketers|string result = check provider->generate(`Give me a random joke about cricketers`);
test:assertTrue(result is string);
}

@test:Config
function testGenerateMethodWithRecUnionNull() returns error? {
Cricketers1? result = check provider->generate(`Name a random world class cricketer in India`);
test:assertTrue(result is Cricketers1);
}

@test:Config
function testGenerateMethodWithArrayOnly() returns error? {
Cricketers2[] result = check provider->generate(`Name 10 world class cricketers in India`);
test:assertTrue(result is Cricketers2[]);
}

@test:Config
function testGenerateMethodWithArrayUnionBasicType() returns error? {
Cricketers3[]|string result = check provider->generate(`Name 10 world class cricketers as string`);
test:assertTrue(result is Cricketers3[]);
}


@test:Config
function testGenerateMethodWithArrayUnionNull() returns error? {
Cricketers4[]? result = check provider->generate(`Name 10 world class cricketers`);
test:assertTrue(result is Cricketers4[]);
}

@test:Config
function testGenerateMethodWithArrayUnionRecord() returns ai:Error? {
Cricketers5[]|Cricketers6|error result = provider->generate(`Name top 10 world class cricketers`);
test:assertTrue(result is Cricketers5[]);
}

@test:Config
function testGenerateMethodWithArrayUnionRecord2() returns ai:Error? {
Cricketers7[]|Cricketers8|error result = provider->generate(`Name a random world class cricketer`);
test:assertTrue(result is Cricketers8);
}

Loading
Loading