Skip to content

Instantly share code, notes, and snippets.

@tarasglek
Created August 13, 2023 17:49
Show Gist options
  • Save tarasglek/26b01018fd5cd0fb77280073d00ffcad to your computer and use it in GitHub Desktop.
Save tarasglek/26b01018fd5cd0fb77280073d00ffcad to your computer and use it in GitHub Desktop.
/* 1. Name of your function (must be unique) */
export const name = "pdf_url2markdown";
/* 2. Description of function, used to describe what it does to an LLM */
export const description = "Converts pdf links to markdown";
/**
* 3. A JSON Schema defining the function's parameters. See:
*
* - https://platform.openai.com/docs/guides/gpt/function-calling
* - https://json-schema.org/learn/getting-started-step-by-step
*/
export const parameters = {
type: "object",
properties: {
pdf_url: {
type: "string",
description: "Url to a pdf that needs conversion",
}
},
required: ["pdf_url"],
};
export default async function (value) {
const url = value.pdf_url;
if (!window.MATHPIX_APP_ID) {
window.MATHPIX_APP_ID = prompt('Please enter your Mathpix App ID');
}
if (!window.MATHPIX_OCR_API_KEY) {
window.MATHPIX_OCR_API_KEY = prompt('Please enter your Mathpix OCR API Key');
}
const headers = {
'app_id': window.MATHPIX_APP_ID,
'app_key': window.MATHPIX_OCR_API_KEY,
'Content-Type': 'application/json'
};
const body = JSON.stringify({
url: url,
conversion_formats: { md: true }
});
const response = await fetch('https://api.mathpix.com/v3/pdf', {
method: 'POST',
headers: headers,
body: body
});
const data = await response.json();
const pdfId = data.pdf_id;
let status = '';
do {
const statusResponse = await fetch(`https://api.mathpix.com/v3/pdf/${pdfId}`, {
method: 'GET',
headers: headers
});
const statusData = await statusResponse.json();
status = statusData.status;
if (status !== 'completed') {
await new Promise(resolve => setTimeout(resolve, 1000));
}
} while (status !== 'completed');
const mdResponse = await fetch(`https://api.mathpix.com/v3/pdf/${pdfId}.md`, {
method: 'GET',
headers: headers
});
const mdData = await mdResponse.text();
return mdData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment