Module: Utils::OpenAI::RunAssitant
- Defined in:
- lib/bas/utils/openai/run_assistant.rb
Overview
This module is an OpenAI utility for using an already created OpenAI Assistant and get an AI response depending on the Assistant’s instructions and prompt.
Constant Summary collapse
- OPENAI_BASE_URL =
"https://api.openai.com"
- DEFAULT_N_CHOICES =
1
Class Method Summary collapse
-
.body ⇒ Object
Request body.
-
.create_thread_and_run ⇒ Object
Creates an OpenAI Thread and a Run using the given assistant_id and prompt.
-
.execute(params) ⇒ Object
Implements the request process logic to the OpenAI Assitant.
-
.headers ⇒ Object
Request headers.
-
.list_messages(run) ⇒ Object
Retrieve the list of messages of a thread.
-
.poll_run(run) ⇒ Object
Polls the Run until it is processed.
Class Method Details
.body ⇒ Object
Request body
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bas/utils/openai/run_assistant.rb', line 59 def self.body { assistant_id: @params[:assistant_id], thread: { messages: [ role: "user", content: @params[:prompt] ] } }.to_json end |
.create_thread_and_run ⇒ Object
Creates an OpenAI Thread and a Run using the given assistant_id and prompt.
43 44 45 46 47 |
# File 'lib/bas/utils/openai/run_assistant.rb', line 43 def self.create_thread_and_run url = "#{OPENAI_BASE_URL}/v1/threads/runs" HTTParty.post(url, { body:, headers: }) end |
.execute(params) ⇒ Object
Implements the request process logic to the OpenAI Assitant.
Params:
-
assistant_id
Assistant id -
prompt
Text that communicates to AI to provide it more context. -
secret
OpenAI Secret API Key.
returns HTTParty::Response
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bas/utils/openai/run_assistant.rb', line 27 def self.execute(params) @params = params run = create_thread_and_run return run unless run.code == 200 run_fetched = poll_run(run.parsed_response) return run_fetched unless run_fetched["status"] == "completed" (run_fetched) end |
.headers ⇒ Object
Request headers
73 74 75 76 77 78 79 |
# File 'lib/bas/utils/openai/run_assistant.rb', line 73 def self.headers { "Authorization" => "Bearer #{@params[:secret]}", "Content-Type" => "application/json", "OpenAI-Beta" => "assistants=v2" } end |
.list_messages(run) ⇒ Object
Retrieve the list of messages of a thread.
51 52 53 54 55 |
# File 'lib/bas/utils/openai/run_assistant.rb', line 51 def self.(run) url = "#{OPENAI_BASE_URL}/v1/threads/#{run["thread_id"]}/messages" HTTParty.get(url, { headers: }) end |
.poll_run(run) ⇒ Object
Polls the Run until it is processed.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bas/utils/openai/run_assistant.rb', line 83 def self.poll_run(run) url = "#{OPENAI_BASE_URL}/v1/threads/#{run["thread_id"]}/runs/#{run["id"]}" while true run_fetched = HTTParty.get(url, { headers: }) status = run_fetched["status"] case status when "queued", "in_progress", "cancelling" then sleep 1 # Wait one second and poll again when "completed", "requires_action", "cancelled", "failed", "expired" then break end end run_fetched end |