Note:
- This tutorial requires access to Oracle Cloud. To sign up for a free account, see Get started with Oracle Cloud Infrastructure Free Tier.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
Access OCI Generative AI Service Programmatically using Ruby on Rails Web Application
Introduction
Oracle Cloud Infrastructure Generative AI (OCI Generative AI) is a revolutionary new artificial intelligence tool that has the potential to transform the way we interact with technology. It is a large language model, which is trained on a massive amount of text data and learns to predict the next word in a sequence based on the words that came before it. This allows OCI Generative AI to generate human-like responses to text prompts, making it an incredibly powerful tool for creating natural-sounding language.
One of the most impressive features of OCI Generative AI is its ability to comprehend complex topics and provide in-depth explanations and responses. These days, every organization want to leverage the power of Generative AI in their applications and create an innovative offering for their customers.
In this tutorial, we will discuss and understand how OCI can help organizations across the world to leverage the power of Generative AI with OCI. In this tutorial, we will use Ruby on Rails (RoR) as our development framework, and will call OCI Generative AI APIs based on Ruby programming language.
Objectives
-
Set up OCI API keys for the user based on which GitLab agent will connect to the OCI tenancy.
-
Create and configure a compute instance so that we can use it for RoR development.
-
Set up a RoR application, write appropriate code to call and display OCI Generative AI service.
Prerequisites
-
Access to OCI and policies allowed in Oracle Cloud Infrastructure Identity and Access Management (OCI IAM). The user should have access to corresponding resources which they want to create or update using Terraform code.
-
Knowledge of Ruby and RoR and its working methodology based on Model View Controller (MVC) web application development.
-
Basic understanding of agile methodology of software development.
Task 1: Set up Oracle Cloud Infrastructure API Keys
-
Log in to the OCI tenancy with the service account or the admin user and go to the User settings.
Or
Go to the user from the OCI IAM console. Ensure the user has all the required access for creating and updating infrastructure on OCI.
-
Go to the Resources section for the user, click API Keys and Add API Key. Select for either downloading the new public and private keys, upload your public key or paste your public key.
-
Save the keys at a convenient location, and save the configuration file as shown in the following page. You will need this configuration file and private key when connecting to your tenancy using Ruby SDK.
Note:
-
OCI supports Generative AI service in
US-CHICAGO-1
region as of now. So make sure to enable and subscribe to this region in your tenancy. -
To call OCI services from OCI Compute instances, OCI provides a more secure way using instance principals. Avoid using user API keys as best practices. For more information, see Calling Services from an Instance.
-
Task 2: Create and Configure an OCI Compute Instance to use for RoR Development
-
Go to Launching a Windows Instance on OCI to create your Windows compute instance on OCI.
-
Once your Windows instance is created, log in using your initial password as shown in the following screenshot.
-
After you log in to your Windows compute instance, install and configure Ruby, Ruby on Rails and create a new application using rails command.
rails new GenAIApp
Go to your app directory and run the following command to bring your rails app server up and running. You should be able to access your default Rails app on
http://127.0.0.1:3000
.cd GenAIApp rails server -b 0.0.0.0
-
Go to your users home directory, which is
c:/users/opc
, and create theconfig
file for Ruby SDK. Configure the config file as you have downloaded in Task 1.3, also place the private key file downloaded. For more information, see Oracle Cloud Infrastructure Ruby SDK.Note:
-
You have to reset the Windows password on first log in to the OCI compute instance. Remember the new password, as you will need this to log in from the next time.
-
To access your Rails app from outside the network, you must open port
3000
, and also switch off Windows firewall from the created compute instance.
-
Task 3: Set up a RoR Application, Write Appropriate Code to Call and Display OCI Generative AI service
-
After you are able to run your Rails app and access it locally and from the required network, our very first step is to set up required gems for the application. Open your
Gemfile
and update its content as shown in the following code snippet.Gemfile
:source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.2.3' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.4', '>= 6.1.4.1' # Gem for OCI SDK gem 'oci' # Use sqlite3 as the database for Active Record gem 'sqlite3', '~> 1.4' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets gem 'sass-rails', '>= 6' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 5.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' gem 'tzinfo-data' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.4', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 4.1.0' # Display performance information such as SQL time and flame graphs for each request in your browser. # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md gem 'rack-mini-profiler', '~> 2.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 3.26' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
-
Execute
bundle install
command to install all the packages.bundle install
Your bundle install should shows the output as shown in the following image.
-
After bundle install is done, add routes to the
routes.rb
file. This will provide the APIs exposed on your application and action needs to be performed when called.config/routes.rb
:Rails.application.routes.draw do root "genai_app#checkin" get "/checkin", to: "genai_app#checkin" post "/checkin", to: "genai_app#checkinpost" end
-
We have to develop the code for app
controller
file as shown below. This is the main code which will execute when you call the OCI Generative AI APIs using app.app/controller/genai_app_controller.rb
:require 'oci' class GenAIAppController < ApplicationController def checkin lines = File.readlines("C:/Users/opc/GenAIApp/app/controllers/chatlog.txt") @chatLines = lines end def checkinpost File.open("C:/Users/opc/GenAIApp/app/controllers/chatlog.txt", "a") { |f| f.write "\nusr::#{params[:message]}" } if params[:message].include? "AskAI:" fullMessage = params[:message].split("AskAI:")[1].strip() region = 'us-chicago-1' gen_ai_inference_client = OCI::GenerativeAiInference::GenerativeAiInferenceClient.new(region: region) compartment_id = 'ocid1.compartment.oc1..aaaaaaaasdk6oyucuqzcaixwxtnxzlt6xxxxxxxxxxuzrm6vmdqgh6wipa' # compartmentId that has policies grant permissions for using Generative AI Service model_id = 'cohere.command' prompt = params[:message].split("AskAI:")[1].strip() max_tokens = 500 temperature = 0.75 frequency_penalty = 1.0 top_p = 0.7 details = OCI::GenerativeAiInference::Models::GenerateTextDetails.new('compartment_id': compartment_id, 'serving_mode': OCI::GenerativeAiInference::Models::OnDemandServingMode.new( 'model_id': model_id ), 'inference_request': OCI::GenerativeAiInference::Models:: CohereLlmInferenceRequest.new( 'prompt': prompt, 'is_stream': false, 'max_tokens': max_tokens, 'temperature': temperature, 'top_p': top_p, 'frequency_penalty': frequency_penalty )) response = gen_ai_inference_client.generate_text(details) text_response = response.data.to_s[170...-150] File.open("C:/Users/opc/GenAIApp/app/controllers/chatlog.txt", "a") { |f| f.write "\nnex::#{text_response}" } print(text_response) end redirect_to("/checkin") end end
-
Update the content for our view file. This will be used to display the UI and call the Get and Post API calls on your applications. This UI page will be used by the user to interact with your app.
app/view/checkin.html.erb
:<style> .greenB { background-color: white; color: black; font-size: 16px; border-bottom: 5px solid darkgreen; text-decoration: none; font-family:verdana; font-weight:bold; width: 100%; height: 50px; display: inline-block; display:flex;/*CSS3*/ align-items:center;/*Vertical align*/ justify-content:center;/*horizontal align*/ } .blueB { background-color: white; color: black; font-size: 16px; border-bottom: 5px solid #2E86C1; text-decoration: none; font-family:verdana; font-weight:bold; width: 100%; height: 50px; display: inline-block; display:flex;/*CSS3*/ align-items:center;/*Vertical align*/ justify-content:center;/*horizontal align*/ } .greenB:hover {background-color: darkgreen; color: white} .blueB:hover {background-color: #2E86C1; color: white} </style> </br> <table width=100%> <tr> <td width=2%> </td> <td width=98%> <div style="color:black;font-family:verdana;font-size:25px;"> <b>Hey, welcome to Nex!</b> </div> </br> <div style="color:black;font-family:verdana;font-size:17px;"> <b>New Checking/Chat</b> </div> </br> <div style="color:darkgreen;font-family:verdana;font-size:14px;padding-bottom:20px;"> <b> Start a new chat with Nex </b> </div> </br> <div style="font-family:verdana;font-size:15px;display:block;height:450px;overflow:auto;"> <% for line in @chatLines do %> <% if line.include? "nex::" %> <table width=90%> <tr> <td width=70% style="color:white;background-color:#2E86C1;padding:10px;"> <%= line.split("nex::")[1] %> </td> <td width=30%> </td> </tr><tr height=2px></tr> </table> <% end %> <% if line.include? "usr::" %> <table width=97%> <tr> <td width=30%> </td> <td width=70% style="color:white;background-color:darkgreen;padding:10px;"> <%= line.split("usr::")[1] %> </td> </tr><tr height=2px></tr> </table> <% end %> <% end %> <table width=90%> <tr> <td width=70%> <%= submit_tag 'Track it!', name: nil, class: "greenB", :style => "width: 200px;" %> </td> <td width=30%> </td> </tr><tr height=5px></tr> </table> </div> <%= form_tag({:controller=>"genai_app", :action=>"checkinpost"}, method: :post) do %> <table width=100%> <tr> <td width=80%> <%= text_field_tag :message, "", :placeholder => "Enter your message", :style => "width: 100%; font-size:17px; padding:15px;" %> </td> <td width=3%> </td> <td width=15%> <%= submit_tag 'Send Message', name: nil, class: "blueB" %> </td> <td width=3%> </td> </tr> </table> <% end %> </td> </tr> </table>
After making all these changes, you should be able to see the updated application and also should be able to invoke OCI Generative AI by asking questions to the prompt.
Note:
-
You have to take care of all gems and its versions, as there may be cases where you need to update the versions in
Gemfile
according to your configuration. -
Take care of networking, so that Ruby can connect to the required gem provider from your instance to download the required gems.
-
Related Links
Acknowledgments
- Author - Lovelesh Saxena (Principal Software Engineering Architect)
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Access OCI Generative AI Service Programmatically using Ruby on Rails Web Application
F95724-01
April 2024