Class: Tfhelps::GeminiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tfhelps.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ GeminiClient



12
13
14
# File 'lib/tfhelps.rb', line 12

def initialize(api_key)
  @url = URI.parse("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=#{api_key}")
end

Instance Method Details

#format_text(text) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tfhelps.rb', line 16

def format_text(text)
  prompt = "    Format the following text into a professional and readable structure.\n      Follow these guidelines:\n      1. Combine sentences into a single paragraph if they are separated by a single empty line.\n      2. Create a new paragraph whenever there are two or more empty lines separating the text.\n      3. Ensure proper spacing, punctuation, and capitalization throughout the text.\n      4. Maintain a clean and polished format suitable for professional use.\n      5. Replace all curved (smart) double quotation marks (e.g., \u201C \u201D) with straight (normal) double quotation marks (\").\n      6. Do not add a title or any additional text content that is not present in the original text.\n\n      Text to format:\n      \#{text}\n  PRO\n\n  http = Net::HTTP.new(@url.host, @url.port)\n  http.use_ssl = true\n  request = Net::HTTP::Post.new(@url.request_uri)\n  request.content_type = \"application/json\"\n\n  payload = {\n    contents: [{\n      parts: [{\n        text: prompt\n      }]\n    }]\n  }.to_json\n\n  request.body = payload\n\n  response = http.request(request)\n\n  return JSON.parse(response.body)[\"candidates\"][0][\"content\"][\"parts\"][0][\"text\"] if response.is_a?(Net::HTTPOK)\n\n  raise Error, \"Failed to format text: \#{response.code} - \#{response.body}\"\nend\n"