Skip to content

Commit 5a38090

Browse files
committed
Tidy up tests and integrations.
1 parent 9da6205 commit 5a38090

File tree

18 files changed

+215
-51
lines changed

18 files changed

+215
-51
lines changed

.github/workflows/documentation-coverage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- uses: actions/checkout@v4
1818
- uses: ruby/setup-ruby@v1
1919
with:
20-
ruby-version: "3.3"
20+
ruby-version: "3.4"
2121
bundler-cache: true
2222

2323
- name: Validate coverage

.github/workflows/documentation.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929

3030
- uses: ruby/setup-ruby@v1
3131
with:
32-
ruby-version: "3.3"
32+
ruby-version: "3.4"
3333
bundler-cache: true
3434

3535
- name: Installing packages

.github/workflows/test-coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- ubuntu
2121

2222
ruby:
23-
- "3.3"
23+
- "3.4"
2424

2525
steps:
2626
- uses: actions/checkout@v4
@@ -54,7 +54,7 @@ jobs:
5454
- uses: actions/checkout@v4
5555
- uses: ruby/setup-ruby@v1
5656
with:
57-
ruby-version: "3.3"
57+
ruby-version: "3.4"
5858
bundler-cache: true
5959

6060
- uses: actions/download-artifact@v4

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/.bundle/
2-
/pkg/
1+
/.bundle
2+
/.context
3+
/pkg
34
/gems.locked
45
/.covered.db
56
/external

.rubocop.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
plugins:
2+
- rubocop-socketry
3+
14
AllCops:
25
DisabledByDefault: true
36

7+
Layout/ConsistentBlankLineIndentation:
8+
Enabled: true
9+
410
Layout/IndentationStyle:
511
Enabled: true
612
EnforcedStyle: tabs
@@ -45,6 +51,18 @@ Layout/EmptyLinesAroundClassBody:
4551
Layout/EmptyLinesAroundModuleBody:
4652
Enabled: true
4753

54+
Layout/EmptyLineAfterMagicComment:
55+
Enabled: true
56+
57+
Layout/SpaceInsideBlockBraces:
58+
Enabled: true
59+
EnforcedStyle: no_space
60+
SpaceBeforeBlockParameters: false
61+
62+
Layout/SpaceAroundBlockParameters:
63+
Enabled: true
64+
EnforcedStyleInsidePipes: no_space
65+
4866
Style/FrozenStringLiteralComment:
4967
Enabled: true
5068

async-ollama.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
2222

2323
spec.files = Dir.glob(["{lib}/**/*", "*.md"], File::FNM_DOTMATCH, base: __dir__)
2424

25-
spec.required_ruby_version = ">= 3.1"
25+
spec.required_ruby_version = ">= 3.2"
2626

2727
spec.add_dependency "async"
2828
spec.add_dependency "async-rest"

examples/chat.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22
# frozen_string_literal: true
33

44
# Released under the MIT License.
5-
# Copyright, 2024, by Samuel Williams.
5+
# Copyright, 2024-2025, by Samuel Williams.
66

7-
require_relative "lib/async/ollama"
7+
require_relative "../lib/async/ollama"
88

99
require "console"
1010

1111
terminal = Console::Terminal.for($stdout)
1212
terminal[:reply] = terminal.style(:blue)
1313
terminal[:reset] = terminal.reset
1414

15+
model = ARGV.shift || "llama3.1:latest"
16+
1517
Async::Ollama::Client.open do |client|
16-
generator = client
18+
conversation = Async::Ollama::Conversation.new(client, model: model)
1719

1820
while input = $stdin.gets
19-
generator = generator.generate(input) do |response|
21+
message = conversation.call(input) do |response|
2022
terminal.write terminal[:reply]
2123

22-
response.body.each do |line|
23-
terminal.write line[:response]
24+
response.body.each do |chunk|
25+
terminal.write chunk
2426
end
2527

2628
terminal.puts terminal[:reset]
2729
end
30+
31+
terminal.puts "Current token count: #{conversation.token_count}"
32+
33+
if conversation.size > 10
34+
conversation.summarize!
35+
terminal.puts "Conversation summarized to #{conversation.size} messages."
36+
conversation.messages.each do |message|
37+
terminal.puts "#{message[:role]}: #{message[:content]}"
38+
end
39+
end
2840
end
2941
end

gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
gem "covered"
2020
gem "decode"
2121
gem "rubocop"
22+
gem "rubocop-socketry"
2223

2324
gem "sus-fixtures-async"
2425

lib/async/ollama/chat.rb

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2024, by Samuel Williams.
4+
# Copyright, 2025, by Samuel Williams.
55

66
require "async/rest/representation"
77
require_relative "wrapper"
@@ -14,6 +14,10 @@ def message
1414
self.value[:message]
1515
end
1616

17+
def error
18+
self.value[:error]
19+
end
20+
1721
def tool_calls
1822
if message = self.message
1923
message[:tool_calls]
@@ -24,6 +28,50 @@ def tool_calls
2428
def model
2529
self.value[:model]
2630
end
31+
32+
# @return [Integer] The time spent generating the response, in nanoseconds.
33+
def total_duration
34+
self.value[:total_duration]
35+
end
36+
37+
# @return [Integer] The time spent loading the model, in nanoseconds.
38+
def load_duration
39+
self.value[:load_duration]
40+
end
41+
42+
# @return [Integer] The number of tokens in the prompt (the token count).
43+
def prompt_eval_count
44+
self.value[:prompt_eval_count]
45+
end
46+
47+
# @return [Integer] The time spent evaluating the prompt, in nanoseconds.
48+
def prompt_eval_duration
49+
self.value[:prompt_eval_duration]
50+
end
51+
52+
# @return [Integer] The number of tokens in the response.
53+
def eval_count
54+
self.value[:eval_count]
55+
end
56+
57+
# @return [Integer] The time spent generating the response, in nanoseconds.
58+
def eval_duration
59+
self.value[:eval_duration]
60+
end
61+
62+
def token_count
63+
count = 0
64+
65+
if prompt_eval_count = self.prompt_eval_count
66+
count += prompt_eval_count
67+
end
68+
69+
if eval_count = self.eval_count
70+
count += eval_count
71+
end
72+
73+
return count
74+
end
2775
end
2876
end
2977
end

lib/async/ollama/client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2024, by Samuel Williams.
4+
# Copyright, 2024-2025, by Samuel Williams.
55

66
require "async/rest/resource"
77

@@ -11,13 +11,13 @@
1111

1212
module Async
1313
module Ollama
14+
MODEL = ENV.fetch("ASYNC_OLLAMA_MODEL", "llama3.1:latest")
15+
1416
# Represents a connection to the Ollama service.
1517
class Client < Async::REST::Resource
1618
# The default endpoint to connect to.
1719
ENDPOINT = Async::HTTP::Endpoint.parse("http://localhost:11434")
1820

19-
MODEL = "llama3.1:latest"
20-
2121
# Generate a response from the given prompt.
2222
# @parameter prompt [String] The prompt to generate a response from.
2323
def generate(prompt, **options, &block)

0 commit comments

Comments
 (0)