Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions effectful/handlers/llm/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from effectful.handlers.llm import Template
from effectful.internals.runtime import get_interpretation, interpreter
from effectful.ops.semantics import fwd
from effectful.ops.syntax import ObjectInterpretation, implements
from effectful.ops.semantics import fwd, handler
from effectful.ops.syntax import ObjectInterpretation, defop, implements


class KAheadSampler[**P, T](ObjectInterpretation):
Expand Down Expand Up @@ -45,3 +45,22 @@ def n_votes_ahead():
tasks.append(executor.submit(interpreter(intp)(fwd), *args, **kwargs))
executor.shutdown()
return self.votes.most_common(1)[0][0]


def sample(template, n):
Comment thread
jfeser marked this conversation as resolved.
Outdated
@defop
def in_nested_call() -> bool:
return False

def _template_call(template, *args, **kwargs):
if in_nested_call():
return fwd()

with handler({in_nested_call: lambda: True}):
with ThreadPoolExecutor() as executor:
intp = get_interpretation()
tasks = [executor.submit(interpreter(intp)(fwd)) for _ in range(n)]
completed = futures.wait(tasks, return_when=futures.ALL_COMPLETED)
return [t.result() for t in completed.done]

return handler({Template.__call__: _template_call})(template)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the benefit of sample using the handler machinery here rather than directly retrieving the information from the template passed in?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then recursively calling Template.__call__ to reuse the existing machinery? also should sample be a defop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Loading