Search Knowledge Base by Keyword
OpenAI ChatGPT Integration
Goal:
Ask ChatGPT a question based on the data in a Legito Document and insert a response to an Element in the Document.
Instructions:
In your document, tag every input you will want to use in the prompt and then tag the output. Output Element can also be an Input Element, question, and select.
Create a script for the last tag that the user will fill in the document, so the script will be called when the user inserts all tags for the prompt and insert this script:
Script Example:
/* TAG script that takes values from all inputElements with defined TAG, construct prompt with them and gets response
// Prompt that will be sent to openAi will have this structure: const OUTPUT_TAG = ”; const OPENAI_API_KEY = ”; getResponseFromOpenAI(PROMPT, OUTPUT_TAG, IS_SELECTABLE_ELEMENT, OPENAI_API_KEY);
/** let response = makeApiCallToOpenAI(completedPrompt, apiKey); if(isSelectableElement){ setValueOfOutputElements(outputElements, response); // FUNCTIONS LEGITO.debug.log(optionsIndexes);
function setValueOfOutputElements(outputElements, value) { function getElementsByTag(tag) { function makeApiCallToOpenAI(prompt, apiKey) { const headers = { const body = { const response = restClient.post(‘https://api.openai.com/v1/chat/completions’, body, headers); return response.data.choices[0].message.content; function getTagsFromPrompt(prompt) { while ((matches = regex.exec(prompt)) !== null) { return […new Set(tags)]; function getValuesForTags(tags) { LEGITO.debug.log(tags); for (let tag of tags) { LEGITO.debug.log(elements); if (elements !== null && elements.length > 0) { if(elements[0].getElementName() === ‘TextElement’){ return tagValueMap; function constructPrompt(prompt, tagValueMap) { |
How to construct the prompt
In the script, change the const PROMPT = ‘’. This is the whole prompt what will the AI get via the API. If you want to use inputs in your prompt, insert it as the name of the tag in the square brackets, as shown below:
const PROMPT = ‘I am taking loan in size of [money_debt]. And if I cannot pay I will pay penalization in size of [money_percent] % of the loan. Is it too much or not much for standard rates?’;
How to set output tag
In the const OUTPUT_TAG = ‘’; just write the tag of the element/s you want to print the response to. Like:
const OUTPUT_TAG = ‘money_debt_percent_output’;
Where to set OpenAI API key
Just insert the OpenAI API key to const OPENAI_API_KEY = ‘’;
So it looks something like this:
const OPENAI_API_KEY = ‘dwadwadwadwad213213123sadadwafWgEyaez3ua7NlkXR1’;
How to set up the script for questions or selects in output
Change the IS_SELECTABLE_ELEMENT to true if the output is question or select. Otherwise set to false.
If the output is a Question or Select Element:
const IS_SELECTABLE_ELEMENT = true;
If the output Text or Text Input element:
const IS_SELECTABLE_ELEMENT = false;