Hey there, fellow Ruby enthusiast! Ready to dive into the world of AI with Azure OpenAI Service? You're in for a treat. We'll be using the nifty azure_openai_client
package to make our lives easier. Buckle up!
Before we jump in, make sure you've got:
Let's start by adding the azure_openai_client
gem to your project. It's as simple as:
gem install azure_openai_client
Or add it to your Gemfile:
gem 'azure_openai_client'
Now, let's set things up:
require 'azure_openai_client' client = AzureOpenAIClient.new( api_key: ENV['AZURE_OPENAI_API_KEY'], endpoint: ENV['AZURE_OPENAI_ENDPOINT'], api_version: '2023-05-15' )
Pro tip: Use environment variables for your API key and endpoint. Keep it secure!
Time to make your first API call:
response = client.completions.create( model: 'text-davinci-003', prompt: 'Hello, Azure OpenAI!', max_tokens: 50 ) puts response.choices.first.text
Easy peasy, right?
For those long responses, streaming is your friend:
client.completions.create( model: 'text-davinci-003', prompt: 'Write a short story', max_tokens: 200, stream: true ) do |chunk| print chunk.choices.first.text end
Switching models is a breeze:
response = client.completions.create( model: 'text-curie-001', prompt: 'Explain quantum computing', max_tokens: 100 )
Be nice to the API:
begin response = client.completions.create(...) rescue AzureOpenAIClient::RateLimitError => e puts "Oops! Rate limit hit. Retrying in #{e.retry_after} seconds." sleep e.retry_after retry end
Always be prepared:
begin response = client.completions.create(...) rescue AzureOpenAIClient::APIError => e puts "Error: #{e.message}" end
And there you have it! You're now ready to harness the power of Azure OpenAI in your Ruby projects. Remember, with great power comes great responsibility. Use it wisely, and happy coding!
Want to dive deeper? Check out the Azure OpenAI documentation and the azure_openai_client GitHub repo.
Now go forth and build something awesome!