diff --git a/SampleConsoleAppRuby/Program.rb b/SampleConsoleAppRuby/Program.rb new file mode 100644 index 0000000..26325c4 --- /dev/null +++ b/SampleConsoleAppRuby/Program.rb @@ -0,0 +1,80 @@ +require 'json' +require 'net/http' +require 'uri' + +def load_config + JSON.parse(File.read('config.json')) +end + +def translate_text(text, to_language, config) + uri = URI("#{config['AzureTranslateURL']}?to=#{to_language}") + request = Net::HTTP::Post.new(uri) + request['Ocp-Apim-Subscription-Key'] = config['Ocp-Apim-Subscription-Key'] + request['Ocp-Apim-Subscription-Region'] = config['Ocp-Apim-Subscription-Region'] + request['Content-Type'] = 'application/json' + request.body = [{ 'Text' => text }].to_json + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| + http.request(request) + end + + JSON.parse(response.body) +end + +def call_openai(content, config) + uri = URI("#{config['YOUR_RESOURCE_NAME']}/openai/deployments/#{config['YOUR_DEPLOYMENT_NAME']}/extensions/chat/completions?api-version=2023-06-01-preview") + request = Net::HTTP::Post.new(uri) + request['api-key'] = config['api-key'] + request['Content-Type'] = 'application/json' + request.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 + } + ] + }.to_json + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| + http.request(request) + end + + JSON.parse(response.body) +end + +def main + config = load_config + + # Change me to ask a new question! + input_text = "Boutons et voyants du panneau de commande\n" + + # FIRST API CALL + first_response = translate_text(input_text, 'en', config) + translated_text = first_response[0]['translations'][0]['text'] + detected_language = first_response[0]['detectedLanguage']['language'] + + # SECOND API CALL + openai_response = call_openai(translated_text, config) + ai_response = openai_response['choices'][0]['messages'][1]['content'] + + # THIRD API CALL + final_response = translate_text(ai_response, detected_language, config) + final_translated_text = final_response[0]['translations'][0]['text'] + + puts final_translated_text +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/SampleConsoleAppRuby/config.json b/SampleConsoleAppRuby/config.json new file mode 100644 index 0000000..92c21ac --- /dev/null +++ b/SampleConsoleAppRuby/config.json @@ -0,0 +1,11 @@ +{ + "Ocp-Apim-Subscription-Key": "YOUR_SUBSCRIPTION_KEY", + "Ocp-Apim-Subscription-Region": "YOUR_SUBSCRIPTION_REGION", + "AzureTranslateURL": "YOUR_AZURE_TRANSLATE_URL", + "YOUR_DEPLOYMENT_NAME": "YOUR_DEPLOYMENT_NAME", + "YOUR_RESOURCE_NAME": "YOUR_RESOURCE_NAME", + "api-key": "YOUR_API_KEY", + "endpoint": "YOUR_ENDPOINT", + "key": "YOUR_KEY", + "indexName": "YOUR_INDEX_NAME" +}