Added AI Features to This Blog Application
This blog uses Ruby on Rails, but I’ve added AI to automatically generate slugs and summaries after writing articles.
I used the open-weight qwen3 as the LLM, and called it as an API through Cloudflare Workers AI.
While Qwen and other Chinese LLMs are lightweight and inexpensive, some people might be concerned about sending data to Chinese data centers.
In this case, using Cloudflare Workers AI to call these services keeps the data on Cloudflare, which feels a bit more secure.
Sample Code
I can’t show the source code of this blog, but if you were to call the API with CURL, it would look like this
(please set the Cloudflare API token and account ID to the environment variables CLOUDFLARE_AI_API_TOKEN and CLOUDFLARE_ACCOUNT_ID before running it).
export CLOUDFLARE_ACCOUNT_ID="..."
export CLOUDFLARE_AI_API_TOKEN="..."
Sample with CURL
curl -X POST \
"https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/run/@cf/qwen/qwen3-30b-a3b-fp8"
\
-H "Authorization: Bearer ${CLOUDFLARE_AI_API_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "system",
"content": "You are a technical blog editor. Please respond in short, accurate Japanese."
},
{
"role": "user",
"content": "Please summarize the article about connecting during the migration period from on-premises to AWS Aurora PostgreSQL in about 70 characters."
}
]
}'
Sample with Ruby
require "json"
require "net/http"
require "uri"
account_id = ENV.fetch("CLOUDFLARE_ACCOUNT_ID")
api_token = ENV.fetch("CLOUDFLARE_AI_API_TOKEN")
model = "@cf/qwen/qwen3-30b-a3b-fp8"
uri = URI("https://api.cloudflare.com/client/v4/accounts/#{account_id}/ai/run/#{model}")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{api_token}"
request["Content-Type"] = "application/json"
request.body = JSON.generate(
messages: [
{
role: "system",
content: "You are a technical blog editor. Please respond in short, accurate Japanese."
},
{
role: "user",
content: "Please summarize the article about connecting during the migration period from on-premises to AWS Aurora PostgreSQL in about 70 characters."
}
]
)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.open_timeout = 60
http.read_timeout = 60
response = http.request(request)
payload = JSON.parse(response.body)
puts "HTTP #{response.code}"
puts payload.dig("result", "response") || payload["result"] || payload
It’s about how well you can create prompts, but it was relatively easy to integrate AI into my own application.
Comments
No comments yet.