Class: Plain::AiDocs
- Inherits:
-
Object
- Object
- Plain::AiDocs
- Defined in:
- app/services/plain/ai_docs.rb
Overview
class Markdownray < Redcarpet::Render::HTML
def block_code(code, language)
CodeRay.scan(code, language).div() rescue "xxx"
end
end
Class Method Summary collapse
- .convert_markdown(text) ⇒ Object
- .create_file_with_path(path, content) ⇒ Object
- .functions ⇒ Object
- .set_conversation_title(conversation) ⇒ Object
Instance Method Summary collapse
- #client ⇒ Object
- #conversation_client(&block) ⇒ Object
- #load_all ⇒ Object
- #load_configuration_paths ⇒ Object
Class Method Details
.convert_markdown(text) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/services/plain/ai_docs.rb', line 112 def self.convert_markdown(text) # rndr = Markdownray.new(filter_html: true, hard_wrap: true) = { fenced_code_blocks: true, no_intra_emphasis: true, autolink: true, lax_html_blocks: true } markdown_to_html = Redcarpet::Markdown.new(Redcarpet::Render::HTML, ) # markdown_to_html = Redcarpet::Markdown.new(rndr, options) markdown_to_html.render(text) #rescue nil end |
.create_file_with_path(path, content) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/services/plain/ai_docs.rb', line 125 def self.create_file_with_path(path, content) # Sanitize the path by replacing spaces with underscores and removing unsafe characters sanitized_path = path.strip.gsub(/[^0-9A-Za-z\/]/, '').gsub(/\s+/, '_') # Create the full path, joining the Rails root directory, "app/docs", and the sanitized path full_path = Rails.root.join("app/views/docs", sanitized_path) # Create directories for the path if they don't exist FileUtils.mkdir_p(File.dirname(full_path)) # Create the file and write content to it File.write(full_path, content) end |
.functions ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/services/plain/ai_docs.rb', line 139 def self.functions [ { name: "create_rails_controller", description: "gives a command to create a rails controller", parameters: { type: :object, properties: { controller_name: { type: :string, description: "the controller name, e.g. users_controller" }, unit: { type: "string", enum: %w[celsius fahrenheit] } }, required: ["controller_name"] } } ] end |
.set_conversation_title(conversation) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'app/services/plain/ai_docs.rb', line 70 def self.set_conversation_title(conversation) chat = Plain::AiDocs.new.conversation_client chat.add_examples(conversation.) title = chat.("from the whole conversation please summarize it 4 words") if title.is_a?(Hash) title = title["choices"].map{|o| o["message"]["content"]}.join("") rescue "no subject" end puts "TITLE: #{title}" conversation.update(subject: title) end |
Instance Method Details
#client ⇒ Object
66 67 68 |
# File 'app/services/plain/ai_docs.rb', line 66 def client @client ||= Plain.configuration.vector_search end |
#conversation_client(&block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/services/plain/ai_docs.rb', line 51 def conversation_client(&block) llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"], default_options: { #chat_completion_model_name: "gpt-3.5-turbo-16k" chat_completion_model_name: "gpt-4" }) # refactor this if block_given? @conversation_client = Langchain::Conversation.new(llm: llm) do |chunk| yield chunk end else @conversation_client = Langchain::Conversation.new(llm: llm) end end |
#load_all ⇒ Object
81 82 83 84 |
# File 'app/services/plain/ai_docs.rb', line 81 def load_all client.create_default_schema load_configuration_paths end |
#load_configuration_paths ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/services/plain/ai_docs.rb', line 86 def load_configuration_paths # Distinguishing files from directories files_without_extension, directories = Plain.configuration.paths.partition { |path| File.file?(path) } # Getting files from directories based on the allowed extensions files_with_extension = directories.flat_map do |dir| Dir[File.join("#{dir}/*/**", "*.{#{Plain.configuration.extensions.join(',')}}")] end puts "FILES WITH EXTENSIONS" puts files_with_extension # Add files with extensions to the client client.add_data(paths: files_with_extension) puts "FILES WITHOUT EXTENSIONS" puts files_with_extension # Process files without extensions files_without_extension.each do |file| content = File.read(file) #texts = Langchain::Chunker::Text.new(content).chunks client.add_texts(texts: content) end end |