SciSharp / LLamaSharp

A C#/.NET library to run LLM (šŸ¦™LLaMA/LLaVA) on your local device efficiently.
https://scisharp.github.io/LLamaSharp
MIT License
2.7k stars 349 forks source link

[DOCS] "Example of LLaMa chat session" is broken #955

Closed easis closed 1 month ago

easis commented 1 month ago

Description

Hey! The example in the README isn't working. I copied the whole code into a new console project, and I got an "Object reference is not set to an instance of an object" error. After debugging, I found the issue:

https://github.com/SciSharp/LLamaSharp/blob/624c8704ddd8d75224f63cb499dace07cddc165e/LLama/LLamaInteractExecutor.cs#L309

It seems that inferenceParams.SamplingPipeline is null.

This could be fixed by either setting an instance of DefaultSamplingPipeline to inferenceParams in the example, or by setting an instance of SamplingPipeline if it is null inside the InferInternal method, like this:

InferenceParams inferenceParams = new()
{
    MaxTokens = 256, // No more than 256 tokens should appear in answer. Remove it if antiprompt is enough for control.
    AntiPrompts = new List<string> { "User:" }, // Stop generation once antiprompts appear.

+    SamplingPipeline = new DefaultSamplingPipeline(),
};
protected override async Task InferInternal(IInferenceParams inferenceParams, InferStateArgs args)
{
    // ...

    // Sample with the pipeline
+   inferenceParams.SamplingPipeline ??= new DefaultSamplingPipeline();
    var id = inferenceParams.SamplingPipeline.Sample(Context.NativeHandle, batch.TokenCount - 1);

    // ...
}

I can make a PR if this sounds good to you.

martindevans commented 1 month ago

Ah that's a good catch, the sampling pipeline changes are very new (just the latest version) and so the readme is out of date. A PR to fix it would be very welcome! Of your two options, the first one is the better fix imo.

easis commented 1 month ago

Ah that's a good catch, the sampling pipeline changes are very new (just the latest version) and so the readme is out of date. A PR to fix it would be very welcome! Of your two options, the first one is the better fix imo.

Done!