rails_ai 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rails_ai/version.rb +1 -1
- data/lib/rails_ai.rb +32 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79cd8a7525dc1139d6d697801f1e24f41d72157d97b1e42a049debce931146c1
|
4
|
+
data.tar.gz: abf698a755293a28d98756a5979f69806ea91c5c3fbc25edab0d4ad349cdfb17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28f77635e2609dc5aec01c34ee9a3a22e856ac3ef47733f913cc81c993426de7fc7f14e4a3ece60783387b98594122d611306f03ac1d38f085e7a4b1deaff610
|
7
|
+
data.tar.gz: 5adcc56daa2903a3cd8969fe1d8be971a41e2beaf277f247b9feda2a25d6c194dd9719cfb1e1a8ac12e13ff06bc519d50494084052776895db0c6fbd69c914c4
|
data/lib/rails_ai/version.rb
CHANGED
data/lib/rails_ai.rb
CHANGED
@@ -500,3 +500,35 @@ end
|
|
500
500
|
def self.handle_security_error(error, context = {})
|
501
501
|
Security::ErrorHandler.handle_security_error(error, context)
|
502
502
|
end
|
503
|
+
require_relative "rails_ai/web_search"
|
504
|
+
|
505
|
+
# Web-enhanced chat with real-time information
|
506
|
+
def self.chat_with_web_search(prompt, model: config.default_model, **opts)
|
507
|
+
# Check if the prompt needs web search
|
508
|
+
web_keywords = ['current', 'latest', 'today', 'now', 'recent', 'weather', 'news', 'stock', 'price']
|
509
|
+
needs_web_search = web_keywords.any? { |keyword| prompt.downcase.include?(keyword) }
|
510
|
+
|
511
|
+
if needs_web_search
|
512
|
+
begin
|
513
|
+
# Perform web search
|
514
|
+
search_results = WebSearch.search(prompt, num_results: 3)
|
515
|
+
|
516
|
+
# Enhance the prompt with web results
|
517
|
+
web_context = "\n\nRecent web search results:\n"
|
518
|
+
search_results.each_with_index do |result, index|
|
519
|
+
web_context += "#{index + 1}. #{result[:title]}\n #{result[:snippet]}\n Source: #{result[:link]}\n\n"
|
520
|
+
end
|
521
|
+
|
522
|
+
enhanced_prompt = "#{prompt}\n\nPlease use the following web search results to provide current, up-to-date information:#{web_context}"
|
523
|
+
|
524
|
+
# Get AI response with web context
|
525
|
+
chat(enhanced_prompt, model: model, **opts)
|
526
|
+
rescue WebSearch::SearchError => e
|
527
|
+
# Fallback to regular chat if web search fails
|
528
|
+
chat(prompt, model: model, **opts)
|
529
|
+
end
|
530
|
+
else
|
531
|
+
# Regular chat for non-time-sensitive queries
|
532
|
+
chat(prompt, model: model, **opts)
|
533
|
+
end
|
534
|
+
end
|