In recent years, OpenAI and its potent AI models have proliferated online, drawing a lot of interest and acclaim from programmers and tech enthusiasts all around the world. The models developed by OpenAI have shown an astounding range of abilities that have been utilised to develop a variety of intelligent applications, from producing text that is similar to human speech to creating beautiful photographs.
Particularly, ChatGPT has attracted the interest of many because of its ability to carry on a range of realistic, human-like conversations. As a result, a large number of ChatGPT-powered chatbots and other interactive applications have been developed, enabling people to engage with and personally experience the power of AI.
As a Ruby developer, you can immediately incorporate AI into your applications through the use of the OpenAI API.
Using the power of OpenAI’s models, you can create intelligent applications that can create text, respond to queries, and much more with just a few lines of code.
This post is a basic introduction to getting started. In future posts, I’ll dive into the technical details so you can gain deeper AI insights. I’ll show you how simple it is to get started by writing just a few lines of Ruby code.
Before doing any coding it's a good idea to play with ChatGPT directly, to get a sense of the sorts of questions you can ask and the responses you will get. https://chat.openai.com/chat.
Integrating Ruby and OpenAI
You need to sign up with OpenAI and get your API Key. At the moment there is free (albeit limited) access to the OpenAI API. Go to https://openai.com/api/ to signup.
Install the OpenAI gem by running the following command in your terminal:
gem install ruby-openai
You can use the gem to access the OpenAI API once it has been installed. We can now write a few lines of Ruby code and begin asking GPT-3 a question.
Let’s ask it ‘What is ruby metaprogramming”.
require "ruby/openai"
client = OpenAI::Client.new(access_token: 'YOUR_API_TOKEN')
prompt = 'What is ruby metaprogramming?'
response = client.completions(
parameters: {
model: "text-davinci-003",
prompt: prompt,
max_tokens: 2000
})
puts response['choices'][0]['text']
This will produce a brief explanation of what metaprogramming is.
The model variable is used to identify which AI model should be used when generating text using the OpenAI API. The OpenAI GPT-3 model is referenced by the term “davinci”. There are other models, but this one is the most advanced.
The text that the OpenAI model will respond to is specified via the prompt variable in the code sample. This text may be a brief comment, an enquiry, or even a lengthy paragraph. The model will then build on this prompt to produce more text that is connected to it.
For example, in the code below, the prompt is set to “What is ruby metaprogramming?”:
prompt = "What is ruby metaprogramming?"
The OpenAI model will create a paragraph defining metaprogramming using this question.
The prompt text you choose should be carefully considered as it will have an impact on the tone and content of the created text. A more explicit and detailed prompt, for instance, will probably provide more accurate and educational content, whereas a more wide or ambiguous prompt may produce more imaginative and abstract created prose.
Let’s say you want to use OpenAI to teach a 10-year-old about metaprogramming. The following code could be used:
require "ruby/openai"
client = OpenAI::Client.new(access_token: 'YOUR_API_TOKEN')
prompt = 'Explain metaprogramming to a 10-year-old?'
response = client.completions(
parameters: {
model: "text-davinci-003",
prompt: prompt,
max_tokens: 2000
})
puts response['choices'][0]['text']
The AI will attempt to create a response that a 10-year-old will understand.
Using the command “Explain metaprogramming to a 10-year-old,” the following is an example of the results you might get:
Metaprogramming is a way for a computer program to write or modify itself while it is running. It’s kind of like having a special set of instructions that can change the way the program works on the fly. For example, imagine you have a computer game where you can collect different power-ups. With metaprogramming, you could write the game so that it can automatically create new power-ups whenever you collect one. So, if you collect a power-up that lets you fly, the game might create a new power-up that lets you breathe underwater. It might sound a little confusing, but metaprogramming can be really useful for making computer programs more flexible and adaptable. And who knows, maybe one day you’ll be the one writing programs that use metaprogramming!
There are ways to control the creativity of the output through other settings, which will be covered in the future.
Summary
You can have a lot of fun with OpenAI. Ask it to write you a poem, write Ruby code for a particular problem or even check the problems with your code.
I hope this introduction to using Ruby with OpenAI was useful. If you have any enquiries or would like to see more examples, please let me know.
In the next article, we will discuss prompt engineering and how to train the AI model to produce the specific results you are looking for.
Kane Hooper is the CEO of reinteractive, a specialist Ruby on Rails development firm.
If you need help with your Rails application you can reach out to Kane directly.
kane.hooper@reinteractive.com
reinteractive.com
Top comments (0)