Skip to content

Commit cde1347

Browse files
authored
Merge pull request #994 from martindevans/Conversation_sampler_chain_helper
Helper method for sampling `Conversation` with `SafeLLamaSamplerChainHandle`
2 parents a431792 + 032ae40 commit cde1347

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

LLama/Batched/Conversation.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,20 @@ public Conversation Fork()
135135

136136
#region sample
137137
/// <summary>
138-
/// Get the logits from this conversation, ready for sampling
138+
/// 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
139+
/// (<see cref="SafeLLamaContextHandle.GetLogitsIth"/>) or to sample a token (<see cref="SafeLLamaSamplerChainHandle.Sample"/>.
139140
/// </summary>
140-
/// <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>
141+
/// <param name="offset">How far from the <b>end</b> of the previous prompt should logits be sampled. Any value other than 0 requires
142+
/// allLogits to have been set during prompting.<br />
143+
/// For example if 5 tokens were supplied in the last prompt call:
144+
/// <list type="bullet">
145+
/// <item>The logits of the first token can be accessed with 4</item>
146+
/// <item>The logits of the second token can be accessed with 3</item>
147+
/// <item>The logits of the third token can be accessed with 2</item>
148+
/// <item>The logits of the fourth token can be accessed with 1</item>
149+
/// <item>The logits of the fifth token can be accessed with 0</item>
150+
/// </list>
151+
/// </param>
141152
/// <returns></returns>
142153
/// <exception cref="ObjectDisposedException"></exception>
143154
/// <exception cref="CannotSampleRequiresPromptException">Thrown if this conversation was not prompted before the previous call to infer</exception>

LLama/Batched/ConversationExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using LLama.Native;
23

34
namespace LLama.Batched;
45

@@ -7,6 +8,18 @@ namespace LLama.Batched;
78
/// </summary>
89
public static class ConversationExtensions
910
{
11+
/// <summary>
12+
/// Sample a token from this conversation using the given sampler chain
13+
/// </summary>
14+
/// <param name="conversation"><see cref="Conversation"/> to sample from</param>
15+
/// <param name="sampler"></param>
16+
/// <param name="offset">Offset from the end of the conversation to the logits to sample, see <see cref="Conversation.GetSampleIndex"/> for more details</param>
17+
/// <returns></returns>
18+
public static LLamaToken Sample(this Conversation conversation, SafeLLamaSamplerChainHandle sampler, int offset = 0)
19+
{
20+
return sampler.Sample(conversation.Executor.Context.NativeHandle, conversation.GetSampleIndex(offset));
21+
}
22+
1023
/// <summary>
1124
/// Rewind a <see cref="Conversation"/> back to an earlier state by removing tokens from the end
1225
/// </summary>

0 commit comments

Comments
 (0)