tiny_gemini 1.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/tiny_gemini.rb +77 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 424a3c3b460be94e67b592b32673d707754b683e53f3bddf4be64f37b7a0b9b9
4
+ data.tar.gz: 70d89f17147279c1741c0a77dc9ddc724109c8957bd5211ca1455c51d7c7e06c
5
+ SHA512:
6
+ metadata.gz: 3aab600148ed5a4bbad01709de20cf73bf905c04482100c4875d4316c2b3f612de646841356f5ca76a89341b1a830ff444b1dd0e404cb407c3999a9d31ebda06
7
+ data.tar.gz: 6fb67b948f0ea36b183dca45fc7433dec1ed469b347fb07bf8f451e0c74bfcd145c92b9bbf1a21d4a90f26fac67e433c2589d0d1e33e8a1a500727bfb10f3a04
@@ -0,0 +1,77 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ # tiny, not very robust, HTTP client the Google Gemini API
5
+ # see also: https://ai.google.dev/api/rest
6
+ class TinyGemini
7
+ # a good rule of thumb would be to have a .tiny_gemini.yml config in your
8
+ # project, to parse that as YAML, and pass the parsed reulst into here.
9
+ def initialize(
10
+ model: 'gemini-1.5-flash',
11
+ host: 'generativelanguage.googleapis.com',
12
+ path: '/v1beta/models',
13
+ action: 'generateContent',
14
+ api_key: ENV['GEMINI_KEY'],
15
+ system_instruction: nil
16
+ )
17
+
18
+ @model = model
19
+ @host = host
20
+ @path = path
21
+ @action = action
22
+ @api_key = api_key
23
+ @system_instruction = system_instruction
24
+ end
25
+
26
+ # sends a request to POST generateContent
27
+ #
28
+ # messages: an array of hashes in the following format:
29
+ # [
30
+ # {
31
+ # "parts": [
32
+ # {
33
+ # "text": "hi, how are you?"
34
+ # }
35
+ # ],
36
+ # "role": "user"
37
+ # },
38
+ # {
39
+ # "parts": [
40
+ # {
41
+ # "text": "I am an AI language model, so I don't have feelings or experiences like humans do. However, I am here to assist you with any questions or tasks you may have! How can I help you today??"
42
+ # }
43
+ # ],
44
+ # "role": "model"
45
+ # },
46
+ # {
47
+ # "parts": [
48
+ # {
49
+ # "text": "oh, you don't have feelings? I guess I didn't realize that - you seem so ... real!"
50
+ # }
51
+ # ],
52
+ # "role": "user"
53
+ # },
54
+ # ]
55
+ #
56
+ # NOTE: if you want the model to impersonate a character (i.e. a talking cat)
57
+ # you need to have set the `system_instruction` parameter when initializing
58
+ # this class to make that work
59
+ def chat(messages)
60
+ body = { contents: messages }
61
+ body.merge!(system_instruction: { parts: { text: @system_instruction } }) if @system_instruction
62
+ request_body = body.to_json
63
+
64
+ uri = URI("https://#{@host}#{@path}/#{@model}:#{@action}?key=#{@api_key}")
65
+ headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
66
+ response = Net::HTTP.post(uri, request_body, headers)
67
+
68
+ # Handle potential errors (e.g., non-200 responses)
69
+ unless response.is_a?(Net::HTTPSuccess)
70
+ raise TinyGeminiModelError, "Gemini API Error: #{response.code}\n#{JSON.pretty_generate(response.body)}"
71
+ end
72
+
73
+ JSON.parse(response.body)['candidates'].first.dig('content', 'parts').first['text']
74
+ end
75
+ end
76
+
77
+ class TinyGeminiModelError < StandardError; end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_gemini
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a tiny gem for using Google Gemini's generative models for chat
14
+ email: jefflunt@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/tiny_gemini.rb
20
+ homepage: https://github.com/jefflunt/tiny_gemini
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.4.19
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: a tiny gem for using Google Gemini's generative models for chat
43
+ test_files: []