-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
223 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Write a js that implements the functionalities related to the drop down list. | ||
It should include at least 3 examples, using German / English languages. | ||
Include the given examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
240 changes: 131 additions & 109 deletions
240
examples/differentialReTranslation/differentialReTranslation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,143 @@ | ||
/* AIGenVersion(31485649, 3js.prompt-15bb6c39, README.md-0243cfe5, dialogelements.txt-4684af8d, requests.jsonl-10415dab) */ | ||
/* AIGenVersion(1516c86e, 3js.prompt-29a550a7, README.md-2deb7062, dialogelements.txt-4684af8d, requests.jsonl-5dddc8c5) */ | ||
|
||
// Function to fetch the API key from localStorage or prompt the user to enter it | ||
function getApiKey() { | ||
let apiKey = localStorage.getItem('openai_api_key'); | ||
if (!apiKey) { | ||
apiKey = prompt('Please enter your OpenAI API key:'); | ||
localStorage.setItem('openai_api_key', apiKey); | ||
} | ||
return apiKey; | ||
} | ||
// Function to handle translation request | ||
async function handleTranslate() { | ||
const originalText = document.getElementById('originalTextField').value; | ||
const instructions = document.getElementById('instructionsField').value; | ||
const apiKey = localStorage.getItem('openai_api_key') || prompt('Enter OpenAI API Key:'); | ||
localStorage.setItem('openai_api_key', apiKey); | ||
|
||
// Function to handle the translation request | ||
function handleTranslationRequest() { | ||
const apiKey = getApiKey(); | ||
const instructions = document.getElementById('instructionsField').value; | ||
const originalText = document.getElementById('originalTextField').value; | ||
const requestBody = { | ||
model: "gpt-3.5-turbo", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: "You are tasked as an expert translator to translate texts with utmost fidelity, preserving the original style, tone, sentiment, and all formatting elements (markdown, HTML tags, special characters) to the greatest extent possible.\nIMPORTANT: Only provide the translated text, maintaining all original formatting and non-translatable elements. Avoid any extraneous comments or actions not directly related to the translation." | ||
}, | ||
{ | ||
role: "user", | ||
content: "Print the original text you have to translate exactly without any comments." | ||
}, | ||
{ | ||
role: "assistant", | ||
content: originalText | ||
}, | ||
{ | ||
role: "user", | ||
content: instructions | ||
} | ||
] | ||
}; | ||
|
||
const requestData = { | ||
"model": "gpt-3.5-turbo", | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "You are tasked as an expert translator to translate texts with utmost fidelity, preserving the original style, tone, sentiment, and all formatting elements (markdown, HTML tags, special characters) to the greatest extent possible.\nIMPORTANT: Only provide the translated text, maintaining all original formatting and non-translatable elements. Avoid any extraneous comments or actions not directly related to the translation." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": originalText | ||
}, | ||
{ | ||
"role": "user", | ||
"content": instructions | ||
} | ||
] | ||
}; | ||
|
||
fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + apiKey | ||
}, | ||
body: JSON.stringify(requestData) | ||
}) | ||
.then(response => { | ||
if (response.status !== 200) { | ||
alert('Error: Unable to complete translation. Please try again.'); | ||
} else { | ||
return response.json(); | ||
} | ||
}) | ||
.then(data => { | ||
const translatedText = data.choices[0].message.content; | ||
document.getElementById('autoTranslatedTextField').value = translatedText; | ||
}) | ||
.catch(error => { | ||
alert('Error: ' + error.message); | ||
try { | ||
document.getElementById('translateButton').disabled = true; | ||
console.log('Sending translation request:', requestBody); | ||
const response = await fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + apiKey | ||
}, | ||
body: JSON.stringify(requestBody) | ||
}); | ||
|
||
if (response.status === 200) { | ||
const data = await response.json(); | ||
console.log('Received response:', data); | ||
document.getElementById('autoTranslatedTextField').value = data.choices[0].message.content; | ||
} else { | ||
alert('Failed to translate text. Response status: ' + response.status); | ||
} | ||
} catch (error) { | ||
console.error('Error during translation request:', error); | ||
alert('An error occurred during the translation request.'); | ||
} finally { | ||
document.getElementById('translateButton').disabled = false; | ||
} | ||
} | ||
|
||
// Function to handle the re-translation request | ||
function handleRetranslationRequest() { | ||
const apiKey = getApiKey(); | ||
const instructions = document.getElementById('instructionsField').value; | ||
const originalText = document.getElementById('originalTextField').value; | ||
const correctedText = document.getElementById('correctedTextField').value; | ||
const revisedText = document.getElementById('changedTextField').value; | ||
// Function to handle differential re-translation request | ||
async function handleRetranslate() { | ||
const originalText = document.getElementById('originalTextField').value; | ||
const instructions = document.getElementById('instructionsField').value; | ||
const autoTranslatedText = document.getElementById('autoTranslatedTextField').value; | ||
const manuallyCorrectedTranslation = document.getElementById('correctedTextField').value; | ||
const revisedOriginalText = document.getElementById('changedTextField').value; | ||
const apiKey = localStorage.getItem('openai_api_key'); | ||
|
||
const requestData = { | ||
"model": "gpt-3.5-turbo", | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "You are tasked as an expert translator to translate texts with utmost fidelity, preserving the original style, tone, sentiment, and all formatting elements (markdown, HTML tags, special characters) to the greatest extent possible.\nIMPORTANT: Only provide the translated text, maintaining all original formatting and non-translatable elements. Avoid any extraneous comments or actions not directly related to the translation." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": originalText | ||
}, | ||
{ | ||
"role": "user", | ||
"content": instructions | ||
}, | ||
{ | ||
"role": "assistant", | ||
"content": correctedText | ||
}, | ||
{ | ||
"role": "user", | ||
"content": revisedText | ||
} | ||
] | ||
}; | ||
const requestBody = { | ||
model: "gpt-3.5-turbo", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: "You are tasked as an expert translator to translate texts with utmost fidelity, preserving the original style, tone, sentiment, and all formatting elements (markdown, HTML tags, special characters) to the greatest extent possible.\nIMPORTANT: Only provide the translated text, maintaining all original formatting and non-translatable elements. Avoid any extraneous comments or actions not directly related to the translation." | ||
}, | ||
{ | ||
role: "user", | ||
content: "Print the original text you have to translate exactly without any comments." | ||
}, | ||
{ | ||
role: "assistant", | ||
content: originalText | ||
}, | ||
{ | ||
role: "user", | ||
content: instructions | ||
}, | ||
{ | ||
role: "assistant", | ||
content: autoTranslatedText | ||
}, | ||
{ | ||
role: "user", | ||
content: "Print this original text as it was manually adapted." | ||
}, | ||
{ | ||
role: "assistant", | ||
content: manuallyCorrectedTranslation | ||
}, | ||
{ | ||
role: "user", | ||
content: "Print the new text that is to be translated." | ||
}, | ||
{ | ||
role: "assistant", | ||
content: revisedOriginalText | ||
}, | ||
{ | ||
role: "user", | ||
content: "Translate the new text. Take care to include the manual adaptions for the original text." | ||
} | ||
] | ||
}; | ||
|
||
fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + apiKey | ||
}, | ||
body: JSON.stringify(requestData) | ||
}) | ||
.then(response => { | ||
if (response.status !== 200) { | ||
alert('Error: Unable to complete re-translation. Please try again.'); | ||
} else { | ||
return response.json(); | ||
} | ||
}) | ||
.then(data => { | ||
const retranslatedText = data.choices[0].message.content; | ||
document.getElementById('retranslatedResultField').value = retranslatedText; | ||
}) | ||
.catch(error => { | ||
alert('Error: ' + error.message); | ||
try { | ||
document.getElementById('retranslateButton').disabled = true; | ||
console.log('Sending differential re-translation request:', requestBody); | ||
const response = await fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ' + apiKey | ||
}, | ||
body: JSON.stringify(requestBody) | ||
}); | ||
|
||
if (response.status === 200) { | ||
const data = await response.json(); | ||
console.log('Received response:', data); | ||
document.getElementById('retranslatedResultField').value = data.choices[0].message.content; | ||
} else { | ||
alert('Failed to re-translate text. Response status: ' + response.status); | ||
} | ||
} catch (error) { | ||
console.error('Error during differential re-translation request:', error); | ||
alert('An error occurred during the differential re-translation request.'); | ||
} finally { | ||
document.getElementById('retranslateButton').disabled = false; | ||
} | ||
} | ||
|
||
// Bind functions to buttons | ||
document.getElementById('translateButton').addEventListener('click', handleTranslationRequest); | ||
document.getElementById('retranslateButton').addEventListener('click', handleRetranslationRequest); | ||
document.getElementById('translateButton').addEventListener('click', handleTranslate); | ||
document.getElementById('retranslateButton').addEventListener('click', handleRetranslate); |
Oops, something went wrong.