Skip to content

Commit

Permalink
Fix Ctrl-C not working in run_chat, run_llama (#11)
Browse files Browse the repository at this point in the history
* Fix Ctrl-C not working in run_chat, run_llama

Fixes #7.

Also only set GGML_METAL_PATH_RESOURCES on Apple machines.

* Fix run_llama

* Silence stdout output when running tests, try to fix MacOS random test fails
  • Loading branch information
marcom committed Jan 16, 2024
1 parent 706066c commit 133d30c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/run-programs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ See also: `run_chat`, `run_server`
"""
function run_llama(; model::AbstractString, prompt::AbstractString="", nthreads::Int=Threads.nthreads(), n_gpu_layers::Int=99, ctx_size::Int=2048, args=``)
cmd = `$(llama_cpp_jll.main()) --model $model --prompt $prompt --threads $nthreads --n-gpu-layers $n_gpu_layers --ctx-size $ctx_size $args`
# Provides the path to locate ggml-metal.metal file (must be provided separately)
cmd = addenv(cmd, "GGML_METAL_PATH_RESOURCES" => joinpath(llama_cpp_jll.artifact_dir, "bin"))
return read(cmd, String)
if Sys.isapple()
# Provides the path to locate ggml-metal.metal file (must be provided separately)
cmd = addenv(cmd, "GGML_METAL_PATH_RESOURCES" => joinpath(llama_cpp_jll.artifact_dir, "bin"))
end
s = disable_sigint() do
read(cmd, String)
end
return s
end

"""
Expand Down Expand Up @@ -54,9 +59,15 @@ See also: `run_llama`, `run_server`
"""
function run_chat(; model::AbstractString, prompt::AbstractString="", nthreads::Int=Threads.nthreads(), n_gpu_layers::Int=99, ctx_size::Int=2048, args=``)
cmd = `$(llama_cpp_jll.main()) --model $model --prompt $prompt --threads $nthreads --n-gpu-layers $n_gpu_layers --ctx-size $ctx_size $args -ins`
# Provides the path to locate ggml-metal.metal file (must be provided separately)
cmd = addenv(cmd, "GGML_METAL_PATH_RESOURCES" => joinpath(llama_cpp_jll.artifact_dir, "bin"))
run(cmd)
if Sys.isapple()
# Provides the path to locate ggml-metal.metal file (must be provided separately)
cmd = addenv(cmd, "GGML_METAL_PATH_RESOURCES" => joinpath(llama_cpp_jll.artifact_dir, "bin"))
end
disable_sigint() do
# disallow julia's SIGINT (Ctrl-C) handler, and allow Ctrl-C
# to be caught by llama.cpp
run(cmd)
end
end

"""
Expand Down
11 changes: 8 additions & 3 deletions test/run-programs.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
@testset verbose=true "Llama, no model needed" begin
showtestset()

default_run_kwargs = Dict(:n_gpu_layers => 1, :ctx_size => 8)
@testset "run_llama" begin
showtestset()
@test run_llama(; model="", args=`-h`) isa String
@test run_llama(; model="", prompt="", args=`-h`) isa String
redirect_stdio(stdout=devnull) do
@test run_llama(; model="", args=`-h`, default_run_kwargs...) isa String
@test run_llama(; model="", prompt="", args=`-h`, default_run_kwargs...) isa String
end
end

@testset "run_chat" begin
showtestset()
@test run_chat(; model="", args=`-h`) isa Base.Process
redirect_stdio(stdout=devnull) do
@test run_chat(; model="", args=`-h`, default_run_kwargs...) isa Base.Process
end
end

# @testset "LlamaContext" begin
Expand Down

0 comments on commit 133d30c

Please sign in to comment.