llm.rb-datacircle 0.1.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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +97 -0
- data/lib/llm/datacircle/calls.rb +206 -0
- data/lib/llm/datacircle/client.rb +83 -0
- data/lib/llm/datacircle/tools.rb +73 -0
- data/lib/llm/datacircle/version.rb +7 -0
- data/lib/llm/datacircle.rb +17 -0
- data/lib/llm.rb-datacircle.rb +4 -0
- metadata +68 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 50d3c98451a83546c1870ae242b9a771b34587f59c29692350431ad24a23c6ec
|
|
4
|
+
data.tar.gz: 96030476217814aa8ba98d490cd8b7819007e7c6788986c2ccf867626cbe0156
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 179143079c2cf91f602110429f29b5d7833de200ce9a81c25a4f2f071b9ec8df0d93d220520dc0ce7b81647bb52c8b078b839ca029ef7a3755e75ee2492fd3ad
|
|
7
|
+
data.tar.gz: 95a15ae9b1ab4433625861b6b1be45f84d5225f0885e8ebdcc9a233d84c71d5cff7df470d8a051d71dbb34fdfc62ee8721f3c0d6f320744cfb590af1e4684f94
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Datacircle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Datacircle tools for llm.rb
|
|
2
|
+
|
|
3
|
+
Find the best GTM APIs. We benchmark all the GTM APIs and we put the results on our website.
|
|
4
|
+
|
|
5
|
+
Our website: https://datacircle.dev
|
|
6
|
+
|
|
7
|
+
This gem gives [llm.rb](https://github.com/r-uby-dev/llm) four tools that call Datacircle's open API, https://api.datacircle.dev/v1. Each tool is a class of `LLM::Tool`, for an agent or a context. You need no key and no account.
|
|
8
|
+
|
|
9
|
+
| Tool | What the tool gives | Call |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `datacircle-list-apis` | Every GTM API on Datacircle, with the API's slug, number of plans, number of providers and best plan | `GET /apis` |
|
|
12
|
+
| `datacircle-best-plans` | One API's plans, sorted by score out of 100, best first. Each plan comes with a rank and a price per 1,000 calls | `GET /apis/{slug}/best` |
|
|
13
|
+
| `datacircle-search-plans` | Plans that match a provider or plan name, on all APIs or on one API. You can sort them by score, provider or price | `GET /plans` |
|
|
14
|
+
| `datacircle-provider` | The APIs a provider sells, and the provider's rank on each API | `GET /providers/{slug}` |
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
gem install llm.rb-datacircle
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or add it to your Gemfile:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem "llm.rb-datacircle"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The gem needs llm.rb 16 or later and Ruby 3.4 or later. llm.rb 18 and later need Ruby 4.
|
|
29
|
+
|
|
30
|
+
## Use the tools with an agent
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "llm"
|
|
34
|
+
require "llm/datacircle"
|
|
35
|
+
|
|
36
|
+
llm = LLM.openai(key: ENV["OPENAI_API_KEY"])
|
|
37
|
+
agent = LLM::Agent.new(llm, tools: LLM::Datacircle.tools)
|
|
38
|
+
res = agent.talk "Which email finder API is best for under $5 per 1,000?"
|
|
39
|
+
puts res.content
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The agent runs the tool calls of the model and sends the answers back. Each tool is a class: `LLM::Datacircle::ListApis`, `BestPlans`, `SearchPlans` and `Provider`. Give an agent only the ones it needs, or add them to your own tools:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
class PlanFinder < LLM::Agent
|
|
46
|
+
tools LLM::Datacircle::BestPlans, LLM::Datacircle::SearchPlans
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
res = PlanFinder.new(llm).talk "Who is best for the SERP API?"
|
|
50
|
+
puts res.content
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The tools run on each concurrency strategy of llm.rb: `:sequential` (the default), `:thread`, `:fiber`, `:async`, `:fork` and `:ractor`. `:ractor` needs Ruby 4, as the Net::HTTP of Ruby 3.4 can't run in a ractor. Run the tools on threads like this:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
agent = LLM::Agent.new(llm, tools: LLM::Datacircle.tools, concurrency: :thread)
|
|
57
|
+
puts agent.talk("Rank Bright Data on each API it sells").content
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
With a context, you run the tool calls yourself:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
ctx = LLM::Context.new(llm, tools: LLM::Datacircle.tools)
|
|
64
|
+
ctx.talk "What are the 3 cheapest plans of the email-finder API?"
|
|
65
|
+
ctx.talk(ctx.wait(:thread)) while ctx.pending_functions?
|
|
66
|
+
puts ctx.messages.to_a.select(&:assistant?).last.content
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Call a tool yourself
|
|
70
|
+
|
|
71
|
+
You can call a tool without a model. You get the tool's answer as a Hash:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
LLM::Datacircle::BestPlans.new.call(slug: "email-finder", limit: 3)
|
|
75
|
+
# => {"api" => "Employee email", "slug" => "email-finder", ..., "plans" => [{"api" => "Employee email", "rank" => 1, ...}]}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`LLM::Datacircle::Calls.new` makes the same calls. The methods of `Calls` are `list_apis`, `best_plans`, `search_plans` and `provider`. `LLM::Datacircle::Calls.new(base_url: ...)` calls another address, for example a proxy of yours.
|
|
79
|
+
|
|
80
|
+
## What the model reads
|
|
81
|
+
|
|
82
|
+
Each plan comes with a rank, provider, plan, score, price per 1,000 (`perThousand`, in dollars) and a page on datacircle.dev. That page lists every field. A tool also takes an API's name or a provider's name in place of a slug.
|
|
83
|
+
|
|
84
|
+
For an unknown slug, the model reads `{"error" => "..."}` with the API's message and what to send instead. If the network fails, the tool raises `LLM::Datacircle::Error`. llm.rb gives the model that error and the agent keeps going.
|
|
85
|
+
|
|
86
|
+
## Network
|
|
87
|
+
|
|
88
|
+
Each tool call sends HTTPS GET requests to https://api.datacircle.dev/v1 and nowhere else. Most calls send one request. A tool sends up to four when the model gives a name in place of a slug. The requests hold only the tool's input from the model: no key and nothing from your messages. The API takes up to 60 calls a minute from one IP address. A tool waits once, for a minute at most, when the API asks for a wait. The gem's one dependency is llm.rb. The gem stores nothing.
|
|
89
|
+
|
|
90
|
+
## Links
|
|
91
|
+
|
|
92
|
+
- Datacircle: https://datacircle.dev
|
|
93
|
+
- The API: https://datacircle.dev/api
|
|
94
|
+
- The API's spec: https://api.datacircle.dev/v1/openapi.json
|
|
95
|
+
- What each key means: https://datacircle.dev/fields
|
|
96
|
+
|
|
97
|
+
MIT license.
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LLM
|
|
4
|
+
module Datacircle
|
|
5
|
+
# A plan holds about 90 keys (its terms of use, its aggregators...): these choose a plan, the rest is on its page.
|
|
6
|
+
PLAN_KEYS = ::Ractor.make_shareable(%w[api rank provider plan score perThousand notFoundPerThousand billing pay
|
|
7
|
+
currency units unit strengths latencyMs page pricingPage docs])
|
|
8
|
+
|
|
9
|
+
# The keys of an API on GET /apis the model gets.
|
|
10
|
+
API_KEYS = ::Ractor.make_shareable(%w[api slug plans providers best bestPage sheetPage])
|
|
11
|
+
|
|
12
|
+
# Tells a model that sent a provider's name to an API's tool where a provider's plans are.
|
|
13
|
+
FOR_PROVIDER = "A provider's plans: datacircle-search-plans with its name as query, like bright data, " \
|
|
14
|
+
'or datacircle-provider.'
|
|
15
|
+
|
|
16
|
+
# Tells a model that sent an API's slug to the provider's tool where an API's best providers are.
|
|
17
|
+
FOR_API = "An API's best providers: datacircle-best-plans with the API's slug, like email-finder."
|
|
18
|
+
|
|
19
|
+
# A placeholder a small model copies from a prompt's example, like <api-slug>, sent in place of a slug.
|
|
20
|
+
PLACEHOLDER = /[<>{}]/
|
|
21
|
+
|
|
22
|
+
# Accented Latin letters, lowercase, and the letter each is without its accent (Unicode's own decomposition): a
|
|
23
|
+
# name's slug without String#unicode_normalize, whose tables a ractor can't read.
|
|
24
|
+
ACCENTED = 'àáâãäåçèéêëìíîïñòóôõöùúûüýÿāăąćĉċčďēĕėęěĝğġģĥĩīĭįĵķĺļľńņňōŏőŕŗřś' \
|
|
25
|
+
'ŝşšţťũūŭůűųŵŷźżžſơưǎǐǒǔǖǘǚǜǟǡǧǩǫǭǰǵǹǻȁȃȅȇȉȋȍȏȑȓȕȗșțȟȧȩȫȭȯȱȳḁḃḅḇḉ' \
|
|
26
|
+
'ḋḍḏḑḓḕḗḙḛḝḟḡḣḥḧḩḫḭḯḱḳḵḷḹḻḽḿṁṃṅṇṉṋṍṏṑṓṕṗṙṛṝṟṡṣṥṧṩṫṭṯṱṳṵṷṹṻṽṿẁẃẅẇẉ' \
|
|
27
|
+
'ẋẍẏẑẓẕẖẗẘẙẛạảấầẩẫậắằẳẵặẹẻẽếềểễệỉịọỏốồổỗộớờởỡợụủứừửữựỳỵỷỹ'
|
|
28
|
+
UNACCENTED = 'aaaaaaceeeeiiiinooooouuuuyyaaaccccdeeeeegggghiiiijklllnnnooorrrs' \
|
|
29
|
+
'sssttuuuuuuwyzzzsouaiouuuuuaagkoojgnaaaeeiioorruusthaeooooyabbbc' \
|
|
30
|
+
'dddddeeeeefghhhhhiikkkllllmmmnnnnoooopprrrrsssssttttuuuuuvvwwwww' \
|
|
31
|
+
'xxyzzzhtwysaaaaaaaaaaaaeeeeeeeeiioooooooooooouuuuuuuyyyy'
|
|
32
|
+
|
|
33
|
+
# The letters that are two without their accent, lowercase.
|
|
34
|
+
LIGATURES = ::Ractor.make_shareable({ 'ij' => 'ij', 'dž' => 'dz', 'lj' => 'lj', 'nj' => 'nj', 'dz' => 'dz' })
|
|
35
|
+
|
|
36
|
+
# The four calls of https://api.datacircle.dev/v1 behind the tools, each answering the Hash the model reads: an
|
|
37
|
+
# answer of 4xx (an unknown slug, a wrong argument) as {"error" => the API's words}. Error is raised when the API
|
|
38
|
+
# can't be reached or answers 5xx, and llm.rb gives it to the model as an in-band error.
|
|
39
|
+
class Calls
|
|
40
|
+
def initialize(base_url: Client::BASE)
|
|
41
|
+
@client = Client.new(base_url: base_url)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# GET /apis, the APIs whose name or slug holds the most of the query's words first.
|
|
45
|
+
def list_apis(query: nil, limit: nil)
|
|
46
|
+
apis = objects(@client.get('/apis'))
|
|
47
|
+
words = query.to_s.downcase.tr('-', ' ').split
|
|
48
|
+
found = apis.each_with_index.filter_map do |api, order|
|
|
49
|
+
name = "#{api['api']} #{api['slug'].to_s.tr('-', ' ')}".downcase
|
|
50
|
+
matched = words.count { |word| name.include?(word) }
|
|
51
|
+
[-matched, order, api] if matched.positive? || words.empty?
|
|
52
|
+
end.sort_by { |matched, order, _| [matched, order] }
|
|
53
|
+
answer = { 'total' => found.size,
|
|
54
|
+
'apis' => found.first(number(limit, 200) || 20).map { |_, _, api| brief(api, API_KEYS) } }
|
|
55
|
+
if words.any? && (found.empty? || -found.first.first < words.size)
|
|
56
|
+
# A provider's name sent as an API's, like Bright Data, whose data matches many APIs: where its plans are.
|
|
57
|
+
answer['note'] = "No API's name holds every word of #{query.to_s.to_json}. #{FOR_PROVIDER}"
|
|
58
|
+
end
|
|
59
|
+
answer
|
|
60
|
+
rescue Error => e
|
|
61
|
+
refusal(e)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# GET /apis/{slug}/best
|
|
65
|
+
def best_plans(slug: nil, limit: nil)
|
|
66
|
+
if slug.to_s.match?(PLACEHOLDER)
|
|
67
|
+
return { 'error' => "#{slug.to_s.to_json} is a placeholder. Give the API's own slug, like email-finder: " \
|
|
68
|
+
'datacircle-list-apis gives every slug.' }
|
|
69
|
+
end
|
|
70
|
+
api = slugify(slug)
|
|
71
|
+
if api.empty?
|
|
72
|
+
return { 'error' => "Give an API's slug, like email-finder: datacircle-list-apis gives every slug. #{FOR_PROVIDER}" }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
begin
|
|
76
|
+
best(api, limit)
|
|
77
|
+
rescue Error => e
|
|
78
|
+
return refusal(e) unless e.status == 404
|
|
79
|
+
|
|
80
|
+
# The slug with "api" after it, like serp-api, as the sheet's address reads (/serp-api-pricing): the API's own.
|
|
81
|
+
bare = api.sub(/-api(-pricing)?\z/, '')
|
|
82
|
+
if bare != api && !bare.empty?
|
|
83
|
+
begin
|
|
84
|
+
return best(bare, limit).merge('note' => "No API has the slug #{api}: these are #{bare}'s plans.")
|
|
85
|
+
rescue Error
|
|
86
|
+
# The first answer stands.
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
# An API's name sent as its slug, like Employee email: the API of that name.
|
|
90
|
+
named = objects(@client.get('/apis')).find { |entry| slugify(entry['api']) == api }
|
|
91
|
+
return best(named['slug'], limit) if named
|
|
92
|
+
|
|
93
|
+
# A provider's name sent as the API's slug: the API's words, and where a provider's plans are.
|
|
94
|
+
{ 'error' => "#{e.message}. datacircle-list-apis gives every API's slug. #{FOR_PROVIDER}" }
|
|
95
|
+
end
|
|
96
|
+
rescue Error => e
|
|
97
|
+
refusal(e)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# GET /plans
|
|
101
|
+
def search_plans(api: nil, query: nil, sort: nil, limit: nil, offset: nil)
|
|
102
|
+
api = api.to_s
|
|
103
|
+
query = query.to_s
|
|
104
|
+
sort = sort.to_s.strip.downcase
|
|
105
|
+
notes = []
|
|
106
|
+
search = lambda do |on, words|
|
|
107
|
+
@client.get('/plans', api: on.strip, query: words.strip, sort: (sort if %w[score provider price].include?(sort)),
|
|
108
|
+
limit: number(limit, 50), offset: number(offset, 1_000_000, low: 0))
|
|
109
|
+
end
|
|
110
|
+
begin
|
|
111
|
+
answer = search.call(api, query)
|
|
112
|
+
rescue Error => e
|
|
113
|
+
raise unless e.status == 400 && e.message.include?('api must be') && query.strip.empty?
|
|
114
|
+
|
|
115
|
+
# A provider's name sent as the API: the plans whose provider or plan name holds it.
|
|
116
|
+
notes << "No API is named #{api.to_json}: these are the plans whose provider or plan name holds it."
|
|
117
|
+
api, query = '', api
|
|
118
|
+
answer = search.call(api, query)
|
|
119
|
+
end
|
|
120
|
+
if answer['total'].to_i.zero? && query.include?('-')
|
|
121
|
+
# A slug sent as a name, like bright-data, which no name holds: its words, like bright data. A name with a
|
|
122
|
+
# hyphen, like A-Leads, is found as it is first.
|
|
123
|
+
words = query.tr('-', ' ').split.join(' ')
|
|
124
|
+
begin
|
|
125
|
+
answer = search.call(api, words)
|
|
126
|
+
notes << "No provider's or plan's name holds #{query.to_json}: these are the plans whose name holds #{words.to_json}."
|
|
127
|
+
rescue Error
|
|
128
|
+
# The first answer stands.
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
answer = answer.merge('plans' => objects(answer['plans']).map { |plan| brief(plan) })
|
|
132
|
+
notes.empty? ? answer : answer.merge('note' => notes.join(' '))
|
|
133
|
+
rescue Error => e
|
|
134
|
+
refusal(e)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# GET /providers/{slug}
|
|
138
|
+
def provider(slug: nil)
|
|
139
|
+
if slug.to_s.match?(PLACEHOLDER)
|
|
140
|
+
return { 'error' => "#{slug.to_s.to_json} is a placeholder. Give the provider's own slug or name, like " \
|
|
141
|
+
'bright-data or Bright Data.' }
|
|
142
|
+
end
|
|
143
|
+
name = slugify(slug)
|
|
144
|
+
return { 'error' => "Give a provider's slug or name, like bright-data or Bright Data" } if name.empty?
|
|
145
|
+
|
|
146
|
+
begin
|
|
147
|
+
answer = @client.get("/providers/#{name}")
|
|
148
|
+
rescue Error => e
|
|
149
|
+
# An API's slug sent as a provider's, like email-finder: the API's words, and where an API's best providers are.
|
|
150
|
+
return e.status == 404 ? { 'error' => "#{e.message}. #{FOR_API}" } : refusal(e)
|
|
151
|
+
end
|
|
152
|
+
apis = objects(answer['apis']).map do |entry|
|
|
153
|
+
plans = objects(entry['plans'])
|
|
154
|
+
kept = brief(entry, %w[api slug rank bestPage]).merge('plans' => plans.size)
|
|
155
|
+
top = best_plan(plans)
|
|
156
|
+
top ? kept.merge('best' => brief(top, %w[plan score perThousand page])) : kept
|
|
157
|
+
end
|
|
158
|
+
# Its best rank first, the APIs where it has none last, in the API's order.
|
|
159
|
+
apis = apis.each_with_index.sort_by { |entry, order| [entry['rank'].is_a?(::Numeric) ? entry['rank'] : ::Float::INFINITY, order] }
|
|
160
|
+
.map(&:first)
|
|
161
|
+
answer.merge('apis' => apis, 'total' => apis.size, 'ranked' => apis.count { |entry| entry['rank'].is_a?(::Numeric) })
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
private
|
|
165
|
+
|
|
166
|
+
def best(api, limit)
|
|
167
|
+
answer = @client.get("/apis/#{api}/best", limit: number(limit, 50))
|
|
168
|
+
answer.merge('plans' => objects(answer['plans']).map { |plan| brief(plan) })
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# An answer of 4xx as the model reads it, the API's words; anything else raised.
|
|
172
|
+
def refusal(error)
|
|
173
|
+
raise error unless error.status && error.status >= 400 && error.status < 500
|
|
174
|
+
|
|
175
|
+
{ 'error' => error.message }
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def brief(item, keys = PLAN_KEYS)
|
|
179
|
+
keys.each_with_object({}) { |key, kept| kept[key] = item[key] unless item[key].nil? }
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def objects(value)
|
|
183
|
+
value.is_a?(::Array) ? value.select { |item| item.is_a?(::Hash) } : []
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# A number the model sent, as a number or as its digits, within the API's bounds: nil for the API's default.
|
|
187
|
+
def number(value, high, low: 1)
|
|
188
|
+
given = value.is_a?(::Numeric) ? value.to_i : ::Kernel.Integer(value.to_s.strip, 10)
|
|
189
|
+
given < low ? nil : given.clamp(low, high)
|
|
190
|
+
rescue ::ArgumentError, ::TypeError, ::FloatDomainError
|
|
191
|
+
nil
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# The API's rule for a slug: the name in lowercase letters and digits, the rest a hyphen.
|
|
195
|
+
def slugify(value)
|
|
196
|
+
value.to_s.downcase.tr(ACCENTED, UNACCENTED).gsub(/[ijdžljnjdz]/, LIGATURES).gsub(/\p{Mn}/, '')
|
|
197
|
+
.gsub(/[^a-z0-9]+/, '-').gsub(/\A-+|-+\z/, '')
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# A provider's best plan on an API: its lowest rank, the first when none has a rank.
|
|
201
|
+
def best_plan(plans)
|
|
202
|
+
plans.select { |plan| plan['rank'].is_a?(::Numeric) }.min_by { |plan| plan['rank'] } || plans.first
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
require 'openssl'
|
|
6
|
+
require 'uri'
|
|
7
|
+
|
|
8
|
+
module LLM
|
|
9
|
+
module Datacircle
|
|
10
|
+
# What Datacircle's API answered when it gave no data (its HTTP status in #status), or why it could not be reached.
|
|
11
|
+
class Error < ::StandardError
|
|
12
|
+
attr_reader :status
|
|
13
|
+
|
|
14
|
+
def initialize(message = nil, status: nil)
|
|
15
|
+
super(message)
|
|
16
|
+
@status = status
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# One HTTPS GET to Datacircle's open API, with no key: its answer as a Hash or an Array. Nothing in it is shared
|
|
21
|
+
# between calls, so a tool runs it on any of llm.rb's strategies, a ractor's too.
|
|
22
|
+
class Client
|
|
23
|
+
BASE = 'https://api.datacircle.dev/v1'
|
|
24
|
+
|
|
25
|
+
def initialize(base_url: BASE)
|
|
26
|
+
@base = base_url.to_s.chomp('/')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def get(path, params = {})
|
|
30
|
+
uri = ::URI.parse("#{@base}#{path}")
|
|
31
|
+
query = params.reject { |_, value| value.nil? || value.to_s.strip.empty? }
|
|
32
|
+
uri.query = ::URI.encode_www_form(query) unless query.empty?
|
|
33
|
+
2.times do |attempt|
|
|
34
|
+
response = request(uri)
|
|
35
|
+
return parse(response) if response.is_a?(::Net::HTTPSuccess)
|
|
36
|
+
|
|
37
|
+
if response.code == '429' && attempt.zero?
|
|
38
|
+
sleep(wait(response))
|
|
39
|
+
next
|
|
40
|
+
end
|
|
41
|
+
raise Error.new(words(response), status: response.code.to_i)
|
|
42
|
+
end
|
|
43
|
+
raise Error.new("Datacircle's API answered 429 twice: wait a minute and call again", status: 429)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def request(uri)
|
|
49
|
+
::Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: 15, read_timeout: 30) do |http|
|
|
50
|
+
http.get(uri.request_uri, 'Accept' => 'application/json', 'User-Agent' => "llm.rb-datacircle/#{VERSION}")
|
|
51
|
+
end
|
|
52
|
+
rescue ::SocketError, ::SystemCallError, ::Timeout::Error, ::OpenSSL::SSL::SSLError, ::Net::HTTPBadResponse,
|
|
53
|
+
::IOError => e
|
|
54
|
+
raise Error, "Datacircle's API could not be reached: #{e.message}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def parse(response)
|
|
58
|
+
::JSON.parse(response.body.to_s)
|
|
59
|
+
rescue ::JSON::ParserError
|
|
60
|
+
raise Error.new("Datacircle's API answered #{response.code} with no JSON", status: response.code.to_i)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The API asks to wait in Retry-After: its seconds, a minute at most.
|
|
64
|
+
def wait(response)
|
|
65
|
+
::Kernel.Integer(response['Retry-After'].to_s.strip, 10).clamp(1, 60)
|
|
66
|
+
rescue ::ArgumentError
|
|
67
|
+
60
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The API's own words: its error of a 4xx, else its status.
|
|
71
|
+
def words(response)
|
|
72
|
+
parsed = ::JSON.parse(response.body.to_s)
|
|
73
|
+
if parsed.is_a?(::Hash) && parsed['error'].is_a?(::String) && !parsed['error'].empty?
|
|
74
|
+
parsed['error']
|
|
75
|
+
else
|
|
76
|
+
"Datacircle's API answered #{response.code}"
|
|
77
|
+
end
|
|
78
|
+
rescue ::JSON::ParserError
|
|
79
|
+
"Datacircle's API answered #{response.code}"
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LLM
|
|
4
|
+
module Datacircle
|
|
5
|
+
PRICE = "perThousand is a plan's price of 1,000 items in dollars, pay the plan's whole price in its currency."
|
|
6
|
+
|
|
7
|
+
# GET /apis
|
|
8
|
+
class ListApis < LLM::Tool
|
|
9
|
+
name 'datacircle-list-apis'
|
|
10
|
+
description "Lists the GTM APIs on Datacircle's sheet (https://datacircle.dev). GTM means go-to-market. GTM " \
|
|
11
|
+
"APIs are the APIs sales and marketing teams use. Gives each API's slug, how many plans and " \
|
|
12
|
+
"providers it has and its best plan. Call it first to find an API's slug."
|
|
13
|
+
parameter :query, String, "Words of the API's name or slug, like email or linkedin profile: the APIs that hold " \
|
|
14
|
+
"the most of them come first. Left out, the sheet's order"
|
|
15
|
+
parameter :limit, Integer, 'How many APIs to give, 1 to 200. 20 when left out: the sheet has about 150'
|
|
16
|
+
|
|
17
|
+
def call(query: nil, limit: nil, **)
|
|
18
|
+
Calls.new.list_apis(query: query, limit: limit)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# GET /apis/{slug}/best
|
|
23
|
+
class BestPlans < LLM::Tool
|
|
24
|
+
name 'datacircle-best-plans'
|
|
25
|
+
description "Gives an API's plans best first by their score out of 100 on Datacircle's benchmark " \
|
|
26
|
+
'(https://datacircle.dev), each with its rank, provider, plan, $ per 1,000 calls and its page. ' \
|
|
27
|
+
"#{PRICE}"
|
|
28
|
+
parameter :slug, String, "The API's slug, like email-finder or linkedin-profile, as datacircle-list-apis gives it"
|
|
29
|
+
parameter :limit, Integer, 'How many plans to give, 1 to 50. 10 when left out'
|
|
30
|
+
required %i[slug]
|
|
31
|
+
|
|
32
|
+
def call(slug: nil, limit: nil, **)
|
|
33
|
+
Calls.new.best_plans(slug: slug, limit: limit)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# GET /plans
|
|
38
|
+
class SearchPlans < LLM::Tool
|
|
39
|
+
name 'datacircle-search-plans'
|
|
40
|
+
description "Searches every plan on Datacircle's sheet (https://datacircle.dev), or one API's, by provider or " \
|
|
41
|
+
'plan name, sorted by score, provider or price. Gives how many were found and up to limit of them. ' \
|
|
42
|
+
"#{PRICE}"
|
|
43
|
+
parameter :api, String, "Only this API's plans: its name or slug, like email-finder. A provider's name goes in " \
|
|
44
|
+
'query, never here. Left out, every API'
|
|
45
|
+
parameter :query, String, "Words that must all appear in the provider's or plan's name, like bright data. Left " \
|
|
46
|
+
'out, every plan'
|
|
47
|
+
parameter :sort, Enum['score', 'provider', 'price'],
|
|
48
|
+
'score (best first, the default), provider (A to Z) or price ($ per 1,000, lowest first)'
|
|
49
|
+
parameter :limit, Integer, 'How many plans to give, 1 to 50. 10 when left out'
|
|
50
|
+
parameter :offset, Integer, 'How many found plans to skip first. 0 when left out'
|
|
51
|
+
|
|
52
|
+
def call(api: nil, query: nil, sort: nil, limit: nil, offset: nil, **)
|
|
53
|
+
Calls.new.search_plans(api: api, query: query, sort: sort, limit: limit, offset: offset)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# GET /providers/{slug}
|
|
58
|
+
class Provider < LLM::Tool
|
|
59
|
+
name 'datacircle-provider'
|
|
60
|
+
description "Gives each API one provider sells and its rank on each on Datacircle's benchmark " \
|
|
61
|
+
'(https://datacircle.dev), with its best plan there, its best rank first. total is how many APIs ' \
|
|
62
|
+
"it sells, ranked on how many of them it has a rank. For an API's best providers, call " \
|
|
63
|
+
'datacircle-best-plans.'
|
|
64
|
+
parameter :slug, String, "The provider's slug, like bright-data, or its name, like Bright Data: the name is " \
|
|
65
|
+
'made a slug'
|
|
66
|
+
required %i[slug]
|
|
67
|
+
|
|
68
|
+
def call(slug: nil, **)
|
|
69
|
+
Calls.new.provider(slug: slug)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'llm'
|
|
4
|
+
require_relative 'datacircle/version'
|
|
5
|
+
require_relative 'datacircle/client'
|
|
6
|
+
require_relative 'datacircle/calls'
|
|
7
|
+
require_relative 'datacircle/tools'
|
|
8
|
+
|
|
9
|
+
module LLM
|
|
10
|
+
# Four llm.rb tools on Datacircle's open API (https://api.datacircle.dev/v1), with no key.
|
|
11
|
+
module Datacircle
|
|
12
|
+
# The four tools, for LLM::Agent.new(llm, tools: LLM::Datacircle.tools).
|
|
13
|
+
def self.tools
|
|
14
|
+
[ListApis, BestPlans, SearchPlans, Provider]
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: llm.rb-datacircle
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wayne Hamadi
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: llm.rb
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '16.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '16.0'
|
|
26
|
+
description: 'Four llm.rb tools on Datacircle''s open API, with no key: list the GTM
|
|
27
|
+
APIs, the best plans of one API, a search of every plan and one provider''s ranks.
|
|
28
|
+
We benchmark all the GTM APIs and we put the results on our website: https://datacircle.dev'
|
|
29
|
+
email:
|
|
30
|
+
- wayne@datacircle.dev
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- LICENSE.txt
|
|
36
|
+
- README.md
|
|
37
|
+
- lib/llm.rb-datacircle.rb
|
|
38
|
+
- lib/llm/datacircle.rb
|
|
39
|
+
- lib/llm/datacircle/calls.rb
|
|
40
|
+
- lib/llm/datacircle/client.rb
|
|
41
|
+
- lib/llm/datacircle/tools.rb
|
|
42
|
+
- lib/llm/datacircle/version.rb
|
|
43
|
+
homepage: https://datacircle.dev
|
|
44
|
+
licenses:
|
|
45
|
+
- MIT
|
|
46
|
+
metadata:
|
|
47
|
+
homepage_uri: https://datacircle.dev
|
|
48
|
+
source_code_uri: https://github.com/waynehamadi/llm.rb-datacircle
|
|
49
|
+
documentation_uri: https://github.com/waynehamadi/llm.rb-datacircle#readme
|
|
50
|
+
bug_tracker_uri: https://github.com/waynehamadi/llm.rb-datacircle/issues
|
|
51
|
+
rdoc_options: []
|
|
52
|
+
require_paths:
|
|
53
|
+
- lib
|
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '3.4'
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
requirements: []
|
|
65
|
+
rubygems_version: 4.0.20
|
|
66
|
+
specification_version: 4
|
|
67
|
+
summary: Find the best GTM APIs, in llm.rb.
|
|
68
|
+
test_files: []
|