Skip to content

Commit

Permalink
add premade examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stoerr committed Mar 6, 2024
1 parent 11132a9 commit 76078ac
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 169 deletions.
2 changes: 1 addition & 1 deletion examples/differentialReTranslation/4examplejs.prompt
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.
16 changes: 8 additions & 8 deletions examples/differentialReTranslation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ In this dialog appear the following elements, in the order from top to bottom:

1. **Examples Dropdown**: Allows users to select from predefined examples to fill in the translation fields automatically. This feature is designed to demonstrate the capabilities of the application without requiring the user to input original texts manually.

2. **Instructions Field**: A text input area where users can specify the instructions for the
2. **Original Text Field**: A text input area for the user to enter the text that needs to be translated. This field represents the source material in its original language.

3. **Instructions Field**: A text input area where users can specify the instructions for the
translation. These instructions have to include the language, and can include further information like desired
writing style, target audience considerations, and
any additional context or background information relevant to the translation task.

3. **Original Text Field**: A text input area for the user to enter the text that needs to be translated. This field represents the source material in its original language.

4. **Translate Button**: Positioned after the Original Text Field, this button initiates the automatic translation of the text entered in the Original Text Field based on the General Instructions provided.

5. **Automatically Translated Original Text Field**: Displays the result of the automatic translation of the original text. This field is automatically populated after the user clicks the Translate button.
Expand Down Expand Up @@ -72,16 +72,16 @@ Text fields are given a label and a one sentence description.
*Label: Choose an Example*
*Description: Select from predefined scenarios to automatically populate the translation fields and explore different translation challenges and solutions.*

2. **Instructions Field**
2. **Original Text Field**
*Label: Original Text*
*Description: Input the text you wish to translate. This will serve as the base for the initial automatic translation.*

3. **Instructions Field**
*Label: Instructions for Translation*
*Description: Enter the specific requirements for the translation, such as tone, formality level, or additional
contextual information.*
Initial value / content of the textarea: "Translate this text into "

3. **Original Text Field**
*Label: Original Text*
*Description: Input the text you wish to translate. This will serve as the base for the initial automatic translation.*

4. **Translate Button**
*Description: Click to translate the original text according to the provided general instructions.*

Expand Down
240 changes: 131 additions & 109 deletions examples/differentialReTranslation/differentialReTranslation.js
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);
Loading

0 comments on commit 76078ac

Please sign in to comment.