const hammingUrl = "https://app.hamming.ai";
const hammingApiKey = "<hamming-api-key>";
type Field = {
name: string;
desc: string;
isInput: boolean;
};
type Example = {
input: {
[key: string]: string;
};
output: {
[key: string]: string;
};
};
type StartJobResponse = {
jobId: string;
};
type JobResponse = {
jobId: string;
status: string;
prompt: string;
};
const headers = {
Authorization: `Bearer ${hammingApiKey}`,
'Content-Type': 'application/json',
};
async function startJob(): Promise<StartJobResponse> {
const url = `${hammingUrl}/api/rest/prompt-optimizer/start-job`;
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json() as Promise<StartJobResponse>;
}
async function extractFields(
jobId: string,
task: string
): Promise<{ fields: Field[] }> {
const url = `${hammingUrl}/api/rest/prompt-optimizer/extract-fields`;
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({ jobId, task }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json() as Promise<{ fields: Field[] }>;
}
async function generateExamples(
jobId: string,
task: string,
fields: Field[],
examples: Example[]
): Promise<{ examples: Example[] }> {
const url = `${hammingUrl}/api/rest/prompt-optimizer/generate-examples`;
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({ jobId, task, fields, examples }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json() as Promise<{ examples: Example[] }>;
}
async function optimize(
jobId: string,
task: string,
fields: Field[],
examples: Example[]
) {
const url = `${hammingUrl}/api/rest/prompt-optimizer/optimize`;
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({ jobId, task, fields, examples }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
}
async function getJobStatus(jobId: string): Promise<JobResponse> {
const url = `${hammingUrl}/api/rest/prompt-optimizer/job-status?jobId=${jobId}`;
const response = await fetch(url, {
method: 'GET',
headers: headers,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json() as Promise<JobResponse>;
}
async function main() {
try {
const jobResponse = await startJob();
const jobId = jobResponse.jobId;
const taskDescription =
"I will give you a description of the data I'm trying to visualize. Return back to me the type of chart that I should use, so my data is easy to understand. Choose from pie chart, bar chart, waterfall chart, etc.";
const fieldsResponse = await extractFields(jobId, taskDescription);
const fields = fieldsResponse.fields;
const sampleExamples: Example[] = [];
const examplesResponse = await generateExamples(
jobId,
taskDescription,
fields,
sampleExamples
);
const examples = examplesResponse.examples;
console.log('Starting job optimization, this will take 30 seconds...');
await optimize(jobId, taskDescription, fields, examples);
let done = false;
let jobStatusResponse: JobResponse | undefined;
while (!done) {
jobStatusResponse = await getJobStatus(jobId);
const status = jobStatusResponse.status;
console.log('Watching job status: ', status);
if (status === 'complete') {
done = true;
} else {
await new Promise((resolve) => setTimeout(resolve, 10000));
}
}
if (!jobStatusResponse) {
throw new Error('Job status response is undefined');
}
console.log('Optimization Complete:', jobStatusResponse.prompt);
} catch (error) {
console.error('Error:', error);
}
}
main();