Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper method for sampling Conversation with SafeLLamaSamplerChainHandle #994

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions LLama/Batched/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,20 @@ public Conversation Fork()

#region sample
/// <summary>
/// Get the logits from this conversation, ready for sampling
/// Get the index in the context which each token can be sampled from, the return value of this function get be used to retrieve logits
/// (<see cref="SafeLLamaContextHandle.GetLogitsIth"/>) or to sample a token (<see cref="SafeLLamaSamplerChainHandle.Sample"/>.
/// </summary>
/// <param name="offset">How far from the <b>end</b> of the previous prompt should logits be sampled. Any value other than 0 requires allLogits to have been set during prompting</param>
/// <param name="offset">How far from the <b>end</b> of the previous prompt should logits be sampled. Any value other than 0 requires
/// allLogits to have been set during prompting.<br />
/// For example if 5 tokens were supplied in the last prompt call:
/// <list type="bullet">
/// <item>The logits of the first token can be accessed with 4</item>
/// <item>The logits of the second token can be accessed with 3</item>
/// <item>The logits of the third token can be accessed with 2</item>
/// <item>The logits of the fourth token can be accessed with 1</item>
/// <item>The logits of the fifth token can be accessed with 0</item>
/// </list>
/// </param>
/// <returns></returns>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="CannotSampleRequiresPromptException">Thrown if this conversation was not prompted before the previous call to infer</exception>
Expand Down
15 changes: 14 additions & 1 deletion LLama/Batched/ConversationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using LLama.Native;

namespace LLama.Batched;

Expand All @@ -7,6 +8,18 @@ namespace LLama.Batched;
/// </summary>
public static class ConversationExtensions
{
/// <summary>
/// Sample a token from this conversation using the given sampler chain
/// </summary>
/// <param name="conversation"><see cref="Conversation"/> to sample from</param>
/// <param name="sampler"></param>
/// <param name="offset">Offset from the end of the conversation to the logits to sample, see <see cref="Conversation.GetSampleIndex"/> for more details</param>
/// <returns></returns>
public static LLamaToken Sample(this Conversation conversation, SafeLLamaSamplerChainHandle sampler, int offset = 0)
{
return sampler.Sample(conversation.Executor.Context.NativeHandle, conversation.GetSampleIndex(offset));
}

/// <summary>
/// Rewind a <see cref="Conversation"/> back to an earlier state by removing tokens from the end
/// </summary>
Expand Down
Loading