Skip to content

Commit abf8084

Browse files
committed
feat: llamacpp chat completion
1 parent d96afbc commit abf8084

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ config :hyper_llm,
2828
],
2929
anthropic: [
3030
api_key: "sk-..."
31-
]
31+
],
32+
# ...
3233
```
3334

3435
## Usage
@@ -142,6 +143,7 @@ end
142143
| Anthropic |||
143144
| Cloudflare |||
144145
| Groq |||
146+
| LlamaCPP |||
145147
| Mistral |||
146148
| Ollama |||
147149
| OpenAI |||

lib/model.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule HyperLLM.Model do
77
"anthropic" => HyperLLM.Provider.Anthropic,
88
"cloudflare" => HyperLLM.Provider.Cloudflare,
99
"groq" => HyperLLM.Provider.Groq,
10+
"llama_cpp" => HyperLLM.Provider.LlamaCPP,
1011
"mistral" => HyperLLM.Provider.Mistral,
1112
"openai" => HyperLLM.Provider.OpenAI,
1213
"ollama" => HyperLLM.Provider.Ollama,
@@ -81,7 +82,7 @@ defmodule HyperLLM.Model do
8182
Example:
8283
8384
iex> HyperLLM.Model.list_providers()
84-
["anthropic", "cloudflare", "groq", "mistral", "ollama", "openai", "x_ai"]
85+
["anthropic", "cloudflare", "groq", "llama_cpp", "mistral", "ollama", "openai", "x_ai"]
8586
"""
8687
def list_providers, do: Map.keys(@providers)
8788
end

lib/providers/llama_cpp.ex

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
defmodule HyperLLM.Provider.LlamaCPP do
2+
@behaviour HyperLLM.Provider
3+
4+
@moduledoc """
5+
Provider implementation for LlamaCPP.
6+
7+
LlamaCPP server that implements the OpenAI API format.
8+
https://github.com/ggerganov/llama.cpp/tree/master/examples/server
9+
10+
## Configuration
11+
12+
`api_key` - The API key for the LlamaCPP API (optional).
13+
`base_url` - The base URL for the LlamaCPP API.
14+
15+
config :hyper_llm,
16+
llama_cpp: [
17+
api_key: "llamacpp",
18+
base_url: "http://localhost:8080"
19+
]
20+
"""
21+
22+
@impl true
23+
def completion(messages, config) do
24+
model = Keyword.get(config, :model)
25+
26+
{_request, response} =
27+
request("/chat/completions",
28+
method: :post,
29+
receive_timeout: 30_000,
30+
json: %{
31+
model: model,
32+
messages: messages
33+
}
34+
)
35+
36+
case response do
37+
%{status: 200, body: body} ->
38+
{:ok, body}
39+
40+
%{status: 400, body: body} ->
41+
{:error, body.error.message}
42+
43+
%{status: 401} ->
44+
{:error, "LlamaCPP API key is invalid"}
45+
46+
%{status: 404} ->
47+
{:error, "LlamaCPP API not found"}
48+
49+
%{status: 500} ->
50+
{:error, "LlamaCPP Server error"}
51+
52+
_ ->
53+
{:error, "Unknown error"}
54+
end
55+
end
56+
57+
@impl true
58+
@doc """
59+
Check if a model is supported by the provider.
60+
61+
All models are supported as they are loaded into the server directly.
62+
"""
63+
def model_supported?(_), do: true
64+
65+
defp request(url, opts) do
66+
api_key = HyperLLM.config!(:llama_cpp, :api_key)
67+
base_url = HyperLLM.config(:llama_cpp, :base_url, "http://localhost:8080")
68+
69+
req =
70+
Req.new(
71+
auth: {:bearer, api_key},
72+
base_url: base_url,
73+
url: url
74+
)
75+
76+
Req.run(req, opts)
77+
end
78+
end

test/model_test.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ defmodule HyperLLM.ModelTest do
5454

5555
describe "list_providers/0" do
5656
test "returns a list of providers" do
57-
assert ["anthropic", "cloudflare", "groq", "mistral", "ollama", "openai", "x_ai"] =
57+
assert [
58+
"anthropic",
59+
"cloudflare",
60+
"groq",
61+
"llama_cpp",
62+
"mistral",
63+
"ollama",
64+
"openai",
65+
"x_ai"
66+
] =
5867
HyperLLM.Model.list_providers()
5968
end
6069
end

0 commit comments

Comments
 (0)