Skip to content

Commit 03afa37

Browse files
committed
build prompt with builder function
1 parent f904e46 commit 03afa37

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/rag/generation.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Rag.Generation do
1010
@type response_function :: (String.t(), keyword() -> String.t())
1111
@type context_builder_function :: (Generation.t(), keyword() -> String.t())
1212
@type context_sources_builder_function :: (Generation.t(), keyword() -> list(String.t()))
13+
@type prompt_builder_function :: (Generation.t(), keyword() -> String.t())
1314

1415
@typedoc """
1516
Represents a generation, the main datastructure in `rag`.
@@ -184,4 +185,16 @@ defmodule Rag.Generation do
184185

185186
Generation.put_context_sources(generation, context_sources)
186187
end
188+
189+
@doc """
190+
Passes `generation` and `opts` to `prompt_builder_function` to determine the prompt.
191+
Then, puts the prompt in `generation.prompt`.
192+
"""
193+
@spec build_prompt(t(), prompt_builder_function(), keyword()) :: t()
194+
def build_prompt(%Generation{} = generation, prompt_builder_function, opts \\ [])
195+
when is_function(prompt_builder_function, 2) do
196+
prompt = prompt_builder_function.(generation, opts)
197+
198+
Generation.put_prompt(generation, prompt)
199+
end
187200
end

test/rag/generation_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@ defmodule Rag.GenerationTest do
9090
Generation.build_context_sources(generation, builder_fn, [])
9191
end
9292
end
93+
94+
describe "build_prompt/3" do
95+
test "builds prompt according to builder function" do
96+
generation = %Generation{query: "query"}
97+
98+
builder_fn = fn _generation, _opts -> "this is the prompt" end
99+
100+
assert %{prompt: "this is the prompt"} =
101+
Generation.build_prompt(generation, builder_fn, [])
102+
end
103+
end
93104
end

0 commit comments

Comments
 (0)