1414// specific language governing permissions and limitations
1515// under the License.
1616
17- import ballerina /http ;
1817import ballerina /test ;
19- import ballerinax /azure .openai .chat as azureOpenAIChat ;
20- import ballerinax /openai .chat as openAIChat ;
2118
22- final readonly & Blog blog1 = {
23- // Generated.
24- title : " Tips for Growing a Beautiful Garden" ,
25- content : string ` Spring is the perfect time to start your garden.
26- Begin by preparing your soil with organic compost and ensure proper drainage.
27- Choose plants suitable for your climate zone, and remember to water them regularly.
28- Don't forget to mulch to retain moisture and prevent weeds.`
29- };
30-
31- final readonly & Blog blog2 = {
32- // Generated.
33- title : " Essential Tips for Sports Performance" ,
34- content : string ` Success in sports requires dedicated preparation and training.
35- Begin by establishing a proper warm-up routine and maintaining good form.
36- Choose the right equipment for your sport, and stay consistent with training.
37- Don't forget to maintain proper hydration and nutrition for optimal performance.`
38- };
39-
40- final readonly & Review review2 = {
41- rating : 8 ,
42- comment : " Talks about essential aspects of sports performance including warm-up, form, equipment, and nutrition."
43- };
19+ const ERROR_MESSAGE = " Error occurred while converting the LLM response to the given type. Please refined your prompt to get a better result." ;
4420
4521@test :Config
4622function testPromptAsCodeFunctionWithSimpleExpectedTypeWithDefaultAzureOpenAIClient() returns error ? {
@@ -58,90 +34,24 @@ function testPromptAsCodeFunctionWithStructuredExpectedTypeWithOpenAIClient() re
5834 },
5935 serviceUrl : " http://localhost:8080/llm/openai"
6036 }, " gpt4o" );
61- Review review = check callLlm (` Rate this blog out of 10.
37+ Review review = check callLlm (` Please rate this blog out of 10.
6238 Title: ${blog2 .title }
6339 Content: ${blog2 .content }` , {model });
6440 test : assertEquals (review , review2 );
6541}
6642
67- type Blog record {
68- string title;
69- string content;
70- };
71-
72- type Review record {|
73- int rating;
74- string comment;
75- | };
76-
77- service /llm on new http:Listener(8080 ) {
78- resource function post azureopenai/deployments/gpt4onew/chat/completions(
79- string api \- version , azureOpenAIChat : CreateChatCompletionRequest payload )
80- returns json {
81- test : assertEquals (api \- version , " 2023-08-01-preview" );
82- string expectedPromptString = string ` Rate this blog out of 10.
83- Title: ${blog1 .title }
84- Content: ${blog1 .content }.
85- The output should be a JSON value that satisfies the following JSON schema,
86- returned within a markdown snippet enclosed within ${" ```json" } and ${" ```" }
87-
88- Schema:
89- {"type":"integer"}` ;
90- azureOpenAIChat : ChatCompletionRequestMessage []? messages = payload .messages ;
91- if messages is () {
92- test : assertFail (" Expected messages in the payload" );
93- }
94- azureOpenAIChat : ChatCompletionRequestMessage message = messages [0 ];
95- test : assertEquals (message .role , " user" );
96- test : assertEquals (message [" content" ], expectedPromptString );
97- return {
98- 'object : " chat.completion" ,
99- created : 0 ,
100- model : " " ,
101- id : " " ,
102- choices : [
103- {
104- message : {
105- content : " 4"
106- }
107- }
108- ]
109- };
110- }
111-
112- resource function post openai/chat/completions(openAIChat : CreateChatCompletionRequest payload )
113- returns json {
114- string expectedPromptString = string ` Rate this blog out of 10.
115- Title: ${blog2 .title }
116- Content: ${blog2 .content }.
117- The output should be a JSON value that satisfies the following JSON schema,
118- returned within a markdown snippet enclosed within ${" ```json" } and ${" ```" }
119-
120- Schema:
121- {"$schema":"https://json-schema.org/draft/2020-12/schema", "type":"object", "properties":{"rating":{"type":"integer"}, "comment":{"type":"string"}}, "required":["rating", "comment"]}` ;
122- azureOpenAIChat : ChatCompletionRequestMessage message = payload .messages [0 ];
123- test : assertEquals (message .role , " user" );
124- test : assertEquals (message [" content" ], expectedPromptString );
125-
126- test : assertEquals (payload .model , " gpt4o" );
127- return {
128- 'object : " chat.completion" ,
129- created : 0 ,
130- model : " " ,
131- id : " " ,
132- choices : [
133- {
134- finish_reason : " stop" ,
135- index : 0 ,
136- logprobs : (),
137- message : {
138- role : " assistant" ,
139- content : review2 .toJsonString (),
140- refusal : ()
141- }
142- }
143- ]
144- };
145- }
43+ @test :Config
44+ function testJsonConversionError() {
45+ boolean | error rating = callLlm (` What is 1 + 1?` );
46+ test : assertTrue (rating is error );
47+ test : assertTrue ((< error > rating ).message ().includes (ERROR_MESSAGE ));
14648}
14749
50+ @test :Config
51+ function testJsonConversionError2() {
52+ record {|
53+ string name;
54+ | }[]| error rating = callLlm (` Tell me name and the age of the top 10 world class cricketers` );
55+ test : assertTrue (rating is error );
56+ test : assertTrue ((< error > rating ).message ().includes (ERROR_MESSAGE ));
57+ }
0 commit comments