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

.Net: PromptRendered event does not use changed RenderedPrompt #4190

Closed
dviry opened this issue Dec 12, 2023 · 0 comments · Fixed by #4194
Closed

.Net: PromptRendered event does not use changed RenderedPrompt #4190

dviry opened this issue Dec 12, 2023 · 0 comments · Fixed by #4194
Assignees
Labels
.NET Issue or Pull requests regarding .NET code

Comments

@dviry
Copy link

dviry commented Dec 12, 2023

When attaching the PromptRendered event to the kernel (in order to avoid going over the token limits), the updated .RenderedPrompt is not taken into consideration (i.e. I still get a context_length_exceeded error).

Here is a sample event handler:

private void Kernel_PromptRendered(object? sender, Microsoft.SemanticKernel.Events.PromptRenderedEventArgs e)
{
    var tokenEncoding = GptEncoding.GetEncodingForModel(options.CurrentValue.ChatCompletionModelId);  // usually "cl100k_base"
    var encoded = tokenEncoding.Encode(e.RenderedPrompt);
    if (encoded.Count > options.CurrentValue.MaxTokenTotal)
    {
        e.RenderedPrompt = tokenEncoding.Decode(encoded.GetRange(0, options.CurrentValue.MaxTokenTotal));
    }
}

This applies to the C# SemanticKernel 1.0.0-rc3.

@shawncal shawncal added .NET Issue or Pull requests regarding .NET code triage labels Dec 12, 2023
@github-actions github-actions bot changed the title PromptRendered event does not use changed RenderedPrompt .Net: PromptRendered event does not use changed RenderedPrompt Dec 12, 2023
@markwallace-microsoft markwallace-microsoft self-assigned this Dec 12, 2023
github-merge-queue bot pushed a commit that referenced this issue Dec 12, 2023
…4194)

### Motivation and Context

Closes #4190 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Issue or Pull requests regarding .NET code
Projects
Archived in project
3 participants