| 
									
										
										
										
											2023-09-05 11:08:23 -03:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module DiscourseAi | 
					
						
							|  |  |  |   module Embeddings | 
					
						
							|  |  |  |     module HydeGenerators | 
					
						
							|  |  |  |       class Anthropic < DiscourseAi::Embeddings::HydeGenerators::Base | 
					
						
							|  |  |  |         def prompt(search_term) | 
					
						
							|  |  |  |           <<~TEXT | 
					
						
							| 
									
										
										
										
											2023-09-06 10:06:31 -03:00
										 |  |  |             Human: Given a search term given between <input> tags, generate a forum post about a given subject. | 
					
						
							|  |  |  |             #{basic_prompt_instruction} | 
					
						
							|  |  |  |             <input>#{search_term}</input> | 
					
						
							| 
									
										
										
										
											2023-09-05 16:11:07 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 10:06:31 -03:00
										 |  |  |             Respond with the generated post between <ai> tags. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Assistant:\n | 
					
						
							|  |  |  |           TEXT | 
					
						
							| 
									
										
										
										
											2023-09-05 11:08:23 -03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def models | 
					
						
							|  |  |  |           %w[claude-instant-1 claude-2] | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def hypothetical_post_from(query) | 
					
						
							|  |  |  |           response = | 
					
						
							|  |  |  |             ::DiscourseAi::Inference::AnthropicCompletions.perform!( | 
					
						
							|  |  |  |               prompt(query), | 
					
						
							|  |  |  |               SiteSetting.ai_embeddings_semantic_search_hyde_model, | 
					
						
							| 
									
										
										
										
											2023-09-05 16:11:07 -03:00
										 |  |  |               max_tokens: 400, | 
					
						
							| 
									
										
										
										
											2023-09-06 10:06:31 -03:00
										 |  |  |               stop_sequences: ["</ai>"], | 
					
						
							| 
									
										
										
										
											2023-09-05 11:08:23 -03:00
										 |  |  |             ).dig(:completion) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           Nokogiri::HTML5.fragment(response).at("ai").text | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |