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

[BUG]: Answer stop abruptly after contextsize, even with limiting prompt size #722

Open
kikipoulet opened this issue May 7, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@kikipoulet
Copy link

Description

Hi,

I have no problem using the chat, until the prompt + answer token size exceed the context size, then the answer interrupts while answering, and without leaving the "session.ChatAsync" method. Which is expected at this point as I don't check my prompt size.

So I simply check the history in my code to remove some old message if the total prompt size exceed the context size. I use "session.History.Messages.Remove()" to do that. But while I have the confirmation that the token size is now okay using "context.Tokenize()", the answer still interrupt whil answering, as if what I did was useless.

So I don't really know what's wrong.

Reproduction Steps

  private static uint contextSize = 260 ;
    
    public void InitChat(string modelPath)
    {
      
        Task.Run((async () =>
        {
            var parameters = new ModelParams(modelPath)
            {
                ContextSize = 260, // limiting context to check the bug quickly, but it happens no matter the context size
                
                GpuLayerCount = 5 // How many layers to offload to GPU. Please adjust it according to your GPU memory.
            };
            using var model = LLamaWeights.LoadFromFile(parameters);
            using var context = model.CreateContext(parameters);
            
            var executor = new InteractiveExecutor(context);

            var chatHistory = new ChatHistory(); 
            
            chatHistory.AddMessage(AuthorRole.System, "You are a coding assistant.");

            ChatSession session = new(executor, chatHistory);

            var bannedwords = new List<string>() {"User:" };
            
            while (true)
            {
               
                signalEvent.Wait(token);  

                InferenceParams inferenceParams = new InferenceParams()
                {
                    MaxTokens = 56,
                };
              
                var x = new ChatHistory.Message(AuthorRole.User, currentMessage).ToString();


               // Check if future context won't be > contextsize (history + answer maxtokens + currentmessagelength + 20 security)
                while (context.Tokenize(session.HistoryTransform.HistoryToText(session.History)).Length  
                       + AdvancedSettings.MaxTokens
                       + context.Tokenize(currentMessage).Length
                       + 20 > contextSize){
                    
                                 // Remove first message that is not system to free some context
                                  session.History.Messages.Remove(session.History.Messages.FirstOrDefault(m => m.AuthorRole != 
                                  AuthorRole.System));

                }
                
                string buffer = "";
                
                await foreach (var text in session.ChatAsync(new ChatHistory.Message(AuthorRole.User, currentMessage), inferenceParams))
                {
                    
                   buffer += text;
                    
                }
 
                signalEvent.Reset();
               
            }
        }));
    }

Environment & Configuration

  • Operating system: Windows 11
  • .NET runtime version: .net 8
  • LLamaSharp version: 0.11.2
  • CUDA version (if you are using cuda backend): CPU Bakcend, 0.11.2
  • CPU & GPU device: AMD Ryzen 7 4800HS

Known Workarounds

I tried the same code using 0.10 version and weirdly, it works and continue the conversation as expected, removing old messages to keep context size below the limit.

@AsakusaRinne AsakusaRinne added the bug Something isn't working label May 10, 2024
@AsakusaRinne AsakusaRinne self-assigned this May 10, 2024
@AsakusaRinne
Copy link
Collaborator

Hi, thank you for reporting us this BUG! I'll look into this problem later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants