| 
									
										
										
										
											2023-03-07 16:14:39 -03:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-14 16:03:50 -03:00
										 |  |  | module ::DiscourseAi | 
					
						
							| 
									
										
										
										
											2023-03-07 16:14:39 -03:00
										 |  |  |   module Inference | 
					
						
							|  |  |  |     class DiscourseClassifier | 
					
						
							|  |  |  |       def self.perform!(endpoint, model, content, api_key) | 
					
						
							|  |  |  |         headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         headers["X-API-KEY"] = api_key if api_key.present? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         response = Faraday.post(endpoint, { model: model, content: content }.to_json, headers) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-11 15:35:39 -03:00
										 |  |  |         raise Net::HTTPBadResponse if ![200, 415].include?(response.status) | 
					
						
							| 
									
										
										
										
											2023-03-07 16:14:39 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         JSON.parse(response.body, symbolize_names: true) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |