Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,14 @@ dotnet run --project SampleConsoleApp "Your phrase to be translated"

### Default Phrase
If no command line argument is provided, the program will use the default phrase: "Boutons et voyants du panneau de commande\n".

## Running the Program with a Command Line Argument (Javascript Version)
The Javascript version of the program also accepts a command line argument for the phrase to be translated. If no command line argument is provided, the program will use a default phrase.

### Example Command
```bash
node SampleConsoleAppJS/Program.js "Your phrase to be translated"
```

### Default Phrase
If no command line argument is provided, the program will use the default phrase: "Boutons et voyants du panneau de commande\n".
68 changes: 68 additions & 0 deletions SampleConsoleAppJS/Program.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const axios = require('axios');
const fs = require('fs');

const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));

const translateText = async (text, toLanguage) => {
const url = `${config.AzureTranslateURL}?to=${toLanguage}`;
const headers = {
'Ocp-Apim-Subscription-Key': config['Ocp-Apim-Subscription-Key'],
'Ocp-Apim-Subscription-Region': config['Ocp-Apim-Subscription-Region'],
'Content-Type': 'application/json'
};
const body = [{ Text: text }];
const response = await axios.post(url, body, { headers });
return response.data;
};

const callOpenAI = async (content) => {
const url = `${config['YOUR_RESOURCE_NAME']}/openai/deployments/${config['YOUR_DEPLOYMENT_NAME']}/extensions/chat/completions?api-version=2023-06-01-preview`;
const headers = {
'api-key': config['api-key'],
'Content-Type': 'application/json'
};
const body = {
temperature: 0,
max_tokens: 1000,
top_p: 1.0,
dataSources: [
{
type: 'AzureCognitiveSearch',
parameters: {
endpoint: config.endpoint,
key: config.key,
indexName: config.indexName
}
}
],
messages: [
{
role: 'user',
content: content
}
]
};
const response = await axios.post(url, body, { headers });
return response.data;
};

const main = async () => {
const inputText = process.argv[2] || "Boutons et voyants du panneau de commande\n";

// FIRST API CALL
const firstResponse = await translateText(inputText, 'en');
const translatedText = firstResponse[0].translations[0].text;
const detectedLanguage = firstResponse[0].detectedLanguage.language;

// SECOND API CALL
const openaiResponse = await callOpenAI(translatedText);
const aiResponse = openaiResponse.choices[0].messages[1].content;

// THIRD API CALL
const finalResponse = await translateText(aiResponse, detectedLanguage);
const finalTranslatedText = finalResponse[0].translations[0].text;

console.log(finalTranslatedText);
};

main().catch(console.error);
11 changes: 11 additions & 0 deletions SampleConsoleAppJS/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Ocp-Apim-Subscription-Key": "cc8c9c8152f14f169694366847a86d1d",
"Ocp-Apim-Subscription-Region": "eastus",
"AzureTranslateURL": "https://eastus.api.cognitive.microsoft.com/translator/text/v3.0/translate",
"YOUR_DEPLOYMENT_NAME": "gpt-4o-mini",
"YOUR_RESOURCE_NAME": "https://eastusaiforworkshop.openai.azure.com",
"api-key": "e3aae9d9a652421687c4cceccc2f91e2",
"endpoint": "https://aisearchfordemo.search.windows.net",
"key": "uxw5r9Nvwxdumirxg6f1Xi6xbytxDDwqXvo5Tbcj4CAzSeDqq2kB",
"indexName": "azureblob-index"
}