From c6c4ebb7dd2b616c2b370f15415fd45e73b33347 Mon Sep 17 00:00:00 2001 From: Alec Harrison Date: Thu, 17 Oct 2024 18:31:58 -0500 Subject: [PATCH] Rewrite the console app in Javascript Fixes #11 Add a Javascript version of the console app. * **New Directory and Files** - Add `SampleConsoleAppJS/Program.js` implementing the main program logic in Javascript. - Add `SampleConsoleAppJS/config.json` for configuration settings. * **Program Logic** - Implement the main program logic in `SampleConsoleAppJS/Program.js` using `axios` for HTTP requests and `JSON.parse` for handling JSON responses. - Implement three API calls: translation, OpenAI call, and final translation. * **Configuration** - Add configuration settings in `SampleConsoleAppJS/config.json` similar to `SampleConsoleApp/example-appsettings.json`. * **README Update** - Update `README.md` to include instructions for running the Javascript version of the console app. - Add a section for the Javascript version under "Running the Program with a Command Line Argument". - Provide an example command for running the Javascript version. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/AzureCloudWorkshops/ACW-GenAI-UniversalTranslator/issues/11?shareId=XXXX-XXXX-XXXX-XXXX). --- README.md | 11 ++++++ SampleConsoleAppJS/Program.js | 68 ++++++++++++++++++++++++++++++++++ SampleConsoleAppJS/config.json | 11 ++++++ 3 files changed, 90 insertions(+) create mode 100644 SampleConsoleAppJS/Program.js create mode 100644 SampleConsoleAppJS/config.json diff --git a/README.md b/README.md index 22edfbc..ba77fc0 100644 --- a/README.md +++ b/README.md @@ -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". diff --git a/SampleConsoleAppJS/Program.js b/SampleConsoleAppJS/Program.js new file mode 100644 index 0000000..197ab76 --- /dev/null +++ b/SampleConsoleAppJS/Program.js @@ -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); diff --git a/SampleConsoleAppJS/config.json b/SampleConsoleAppJS/config.json new file mode 100644 index 0000000..3abb84c --- /dev/null +++ b/SampleConsoleAppJS/config.json @@ -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" +}