Created
May 16, 2024 12:35
-
-
Save alexschreyer/1e3d3417335cc31cb41cb4625013b63b to your computer and use it in GitHub Desktop.
This code sends a screenshot of the current model to OpenAI's API and asks questions about it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'uri' | |
require 'json' | |
require 'base64' | |
# Ask something about what you see in SketchUp | |
prompt = "Is there anything wrong with this building?" | |
# Set the endpoint and API key for the OpenAI API | |
endpoint = "https://api.openai.com/v1/chat/completions" | |
api_key = "<YOUR API KEY>" | |
# Set up the system message if desired | |
sys_message = "" | |
# Save the screenshot and encode the file as base64 | |
dir = File.join( ENV['HOME'], 'Documents' ) | |
file_loc = File.join( dir, "temp.png" ) | |
keys = { | |
:filename => file_loc, | |
:width => 1024, | |
:height => 1024, | |
:antialias => true, | |
:scale_factor => 1, | |
:compression => 0.8, | |
:transparent => false | |
} | |
img = Sketchup.active_model.active_view.write_image keys | |
base64_image = File.open(file_loc, "rb") do |file| | |
Base64.strict_encode64(file.read) | |
end | |
# Set up the HTTP request with the API key and prompt | |
request = { | |
# Enter the AI model here | |
"model" => "gpt-4o", | |
"messages" => [ | |
{ "role" => "system", "content" => "#{sys_message}" }, | |
{ "role" => "user", "content" => [ | |
{ "type" => "text", "text" => "#{prompt}" }, | |
{ "type" => "image_url", "image_url" => | |
{ "url" => "data:image/png;base64,#{base64_image}", | |
"detail" => "low" } | |
} | |
] } | |
], | |
"max_tokens" => 1024, | |
"top_p" => 1, | |
"n" => 1, | |
"temperature" => 0.1, | |
"stream" => false | |
} | |
# Show the request in the console | |
p request | |
# Execute the request | |
uri = URI(endpoint) | |
req = Net::HTTP::Post.new(uri) | |
req["Content-Type"] = "application/json" | |
req["Authorization"] = "Bearer #{api_key}" | |
req.body = JSON.dump( request ) | |
# Make the HTTP request to the OpenAI API and parse the response | |
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| | |
http.request(req) | |
end | |
response_body = JSON.parse(res.body) | |
# Show the response in the console | |
p response_body | |
# Get the text response from the API response JSON | |
response = response_body["choices"][0]["message"]["content"] | |
# Display the text response in a message | |
UI.messagebox response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment