Skip to content

Commit 006cf65

Browse files
committed
examples: Updated JSON examples
1 parent 9a95801 commit 006cf65

File tree

4 files changed

+130
-6
lines changed

4 files changed

+130
-6
lines changed

examples/json/gemini.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @file examples/json/native-json-output.js
3+
* @description This example demonstrates native JSON output by specifying JSON requirements in the prompt and enabling native JSON mode. This ensures server-side JSON validation but may return a null response if the result set exceeds the response token limit.
4+
*
5+
* To run this example, you first need to install the required module by executing:
6+
*
7+
* npm install dotenv
8+
*/
9+
10+
const { LLMInterface } = require('../../src/index.js');
11+
const { simplePrompt } = require('../../src/utils/defaults.js');
12+
const { prettyHeader, prettyResult } = require('../../src/utils/utils.js');
13+
14+
require('dotenv').config({ path: '../../.env' });
15+
16+
// Setup your key and interface
17+
const interfaceName = 'cloudflare';
18+
const apiKey = process.env.CLOUDFLARE_API_KEY;
19+
const accountId = process.env.CLOUDFLARE_ACCOUNT_ID;
20+
21+
// Example description
22+
const description = `This example demonstrates native JSON output by specifying JSON requirements in the prompt and enabling native JSON mode. This ensures server-side JSON validation but may return a null response if the result set exceeds the response token limit.
23+
24+
To run this example, you first need to install the required modules by executing:
25+
26+
npm install dotenv
27+
28+
Note that not all providers support native JSON mode, so it is important to check the provider documentation.`;
29+
30+
/**
31+
* Main exampleUsage() function.
32+
*/
33+
async function exampleUsage() {
34+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a valid JSON object; validate the object before responding.\n\nJSON Output Format: [{title, reason}]`;
35+
36+
prettyHeader(
37+
'Native JSON Output Example',
38+
description,
39+
prompt,
40+
interfaceName,
41+
);
42+
43+
LLMInterface.setApiKey(interfaceName, apiKey);
44+
45+
let messageObject = {
46+
model: 'mistralai/Mistral-Nemo-Instruct-2407',
47+
messages: [
48+
{
49+
role: 'system',
50+
content:
51+
"You are an expert assistant assuming the roles of an expert 'GoFundTravel' crowdfunding copywriter, expert ghostwriter, expert editor, JSON data generator, and JSON validator to create, refine, validate, and return a single RFC8259 compliant JSON object adhering to the specified schema. Do not show any work, only return a single RFC8529 compliant JSON object.",
52+
},
53+
{
54+
role: 'user',
55+
content:
56+
'## Title\n' +
57+
'Family in need after devastating car accident\n' +
58+
'## Description\n' +
59+
'Hello I am Alondra. My aunt Giselle Toscano and uncle Luis Páez were hit by a drunk driver on the night of Tuesday June 4. My aunt is in critical condition and is fighting for her life. She has received multiple surgeries and is pending another major surgery in the next few days. She is facing a long recovery. Any help will be greatly appreciated during this difficult time for our family.\n' +
60+
'\n' +
61+
'## Schema\n' +
62+
'{\n' +
63+
' "crowdfunding_description": {\n' +
64+
' "title": "String - The main title of the crowdfunding campaign, summarizing the purpose or need.",\n' +
65+
' "sections": [\n' +
66+
' {\n' +
67+
' "heading": "String - A heading for a specific section of the description, highlighting a key point or part of the story.",\n' +
68+
' "paragraph": "String - A detailed paragraph providing more information and context related to the heading, sharing personal stories, updates, or specific needs."\n' +
69+
' }\n' +
70+
' ]\n' +
71+
' }\n' +
72+
'}\n' +
73+
'## Steps\n' +
74+
"1. You are an **Expert Crowdfunding Copywriter**. Your task is to write an engaging 'GoFundTravel' crowdfunding campaign story based on the 'Title', and 'Description'. Properly format money and dates. \n" +
75+
' 1. The story should have 3 section(s), each with 1 paragraph(s) and _exactly 5 sentence(s)_.\n' +
76+
' 2. The tone should be optimistic, the style casual, and the language accessible.\n' +
77+
' 3. Write at a general public writing level. \n' +
78+
" 4. Count the sentences in each section creating a 'Count' for each.\n" +
79+
'2. You are an **Expert Ghostwriter**: Your task is to improve the campaign story by adding relevant sentences in the same writing style as the **Expert Crowdfunding Copywriter**. Evaluate each section:\n' +
80+
" 1. If the 'Count' for this section is less than 5, _add exactly (5 - 'Count') additional sentences_ to this section.\n" +
81+
" 2. Count the sentences in the updated section, this is now the 'Count' for this section.\n" +
82+
" 3. Repeat adding sentences until 'Count' has (5 - 'Count') sentences.\n" +
83+
"3. You are an **Expert Editor**. Your task is to review the 'Title' and 'Story' and create a 'Revised Story' that follows the 'Story Rules'. Follow these steps in order:\n" +
84+
" 1. **Strip Count from Sections**: Remove the 'Count' and its value from all sections.\n" +
85+
" 2. **Title and Content Optimization**: Optimize the 'Title' for a crowdfunding campaign. Revise the content to be conversational, as if chatting with a friend. Ensure the tone is optimistic, make complaints polite and constructive, and convert technical jargon into everyday language.\n" +
86+
' 3. **Grammar and Style**: Correct grammar mistakes, convert passive paragraphs to active ones, maintain a consistent voice, and streamline by removing repetitive statements.\n' +
87+
"4. You are a **JSON data generator**. Your task is to generate a valid JSON object from the 'Revised Story'. You have the following tasks:\n" +
88+
' 1. Ensure the JSON object conforms to the provided schema.\n' +
89+
" 2. Validate all fields to ensure 'String' is not used as a value for any field.\n" +
90+
' 3. Correct any invalid fields and return the valid JSON object.\n' +
91+
'5. You are a **JSON validator**. Your task is to detect, correct invalid JSON, and return a single valid JSON object. You have the following tasks:\n' +
92+
' 1. Review the JSON object and correct as needed\n' +
93+
" 1. Correct any missing {}'s\n" +
94+
' 2. Correct any invalid or undefined fields\n' +
95+
' 3. Correct any invalid or undefined values\n' +
96+
" 2. Validate the JSON object against the 'Schema'. Guarantee the JSON object is fully compliant with the schema.\n" +
97+
' 3. Return the JSON object.\n',
98+
},
99+
],
100+
max_tokens: 1024,
101+
temperature: 1,
102+
};
103+
104+
try {
105+
console.time('Timer');
106+
const response = await LLMInterface.sendMessage(
107+
interfaceName,
108+
messageObject,
109+
{
110+
max_tokens: 1024,
111+
},
112+
{ attemptJsonRepair: true, includeOriginalResponse: true },
113+
);
114+
115+
console.log(response.results);
116+
console.log();
117+
console.timeEnd('Timer');
118+
console.log();
119+
} catch (error) {
120+
console.error('Error processing LLMInterface.sendMessage:', error);
121+
}
122+
}
123+
124+
exampleUsage();

examples/json/json-output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
const { LLMInterface } = require('../../src/index.js');
11-
const { simpleprompt } = require('../../src/utils/defaults.js');
11+
const { simplePrompt } = require('../../src/utils/defaults.js');
1212
const { prettyHeader, prettyResult } = require('../../src/utils/utils.js');
1313

1414
require('dotenv').config({ path: '../../.env' });
@@ -28,7 +28,7 @@ To run this example, you first need to install the required modules by executing
2828
* Main exampleUsage() function.
2929
*/
3030
async function exampleUsage() {
31-
let prompt = `${simpleprompt} Return 5 results.\n\nProvide the response as a JSON object.\n\nFollow this output format, only responding with the JSON object and nothing else:\n\n{title, reason}`;
31+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a JSON object.\n\nFollow this output format, only responding with the JSON object and nothing else:\n\n{title, reason}`;
3232

3333
prettyHeader(
3434
'JSON Output (Prompt Based) Example',

examples/json/json-repair.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
const { LLMInterface } = require('../../src/index.js');
11-
const { simpleprompt } = require('../../src/utils/defaults.js');
11+
const { simplePrompt } = require('../../src/utils/defaults.js');
1212
const { prettyHeader, prettyResult } = require('../../src/utils/utils.js');
1313

1414
require('dotenv').config({ path: '../../.env' });
@@ -28,7 +28,7 @@ To run this example, you first need to install the required modules by executing
2828
* Main exampleUsage() function.
2929
*/
3030
async function exampleUsage() {
31-
let prompt = `${simpleprompt} Return 5 results.\n\nProvide the response as a JSON object.\n\nFollow this output format, only responding with the JSON object and nothing else:\n\n{title, reason}`;
31+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a JSON object.\n\nFollow this output format, only responding with the JSON object and nothing else:\n\n{title, reason}`;
3232
prettyHeader('JSON Repair Example', description, prompt, interfaceName);
3333

3434
LLMInterface.setApiKey(interfaceName, apiKey);

examples/json/native-json-output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
const { LLMInterface } = require('../../src/index.js');
11-
const { simpleprompt } = require('../../src/utils/defaults.js');
11+
const { simplePrompt } = require('../../src/utils/defaults.js');
1212
const { prettyHeader, prettyResult } = require('../../src/utils/utils.js');
1313

1414
require('dotenv').config({ path: '../../.env' });
@@ -30,7 +30,7 @@ Note that not all providers support native JSON mode, so it is important to chec
3030
* Main exampleUsage() function.
3131
*/
3232
async function exampleUsage() {
33-
let prompt = `${simpleprompt} Return 5 results.\n\nProvide the response as a valid JSON object; validate the object before responding.\n\nJSON Output Format: [{title, reason}]`;
33+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a valid JSON object; validate the object before responding.\n\nJSON Output Format: [{title, reason}]`;
3434

3535
prettyHeader(
3636
'Native JSON Output Example',

0 commit comments

Comments
 (0)