Skip to content

Conversation

@dave-gray101
Copy link
Collaborator

@dave-gray101 dave-gray101 commented Jul 20, 2023

Description
Lays some of the groundwork for LLAMA2 compatibility as well as other future models with complex prompting schemes.

  • Started small refactoring in pkg/model/loader.go regarding template loading. Currently still a part of ModelLoader, but should be easy to add template loading for situations other than overall prompt templates and the new chat-specific per-message templates
  • Adds support for new chat-endpoint-specific, per-message templates as an alternative to the existing Role: XYZ sprintf method.
  • Includes a temporary prompt template as an example, since I have a few questions before we merge in the model-gallery side changes (see )
  • Minor debug logging changes.

Big thanks to @tmm1 who helped work out the proper LLAMA2 prompt text as well as helping with testing!

Notes for Reviewers
This obviously isn't 100% compatibility with LLAMA2. We'll need some upstream changes for the larger models, as well as the proper BOS/EOS support... but we might as well get started now.

Signed commits

  • Yes, I signed my commits.

@dave-gray101 dave-gray101 changed the title WIP: Prompt Template Improvements Prompt Template Improvements Jul 21, 2023
@dave-gray101 dave-gray101 marked this pull request as ready for review July 21, 2023 04:22
@dave-gray101 dave-gray101 enabled auto-merge (squash) July 21, 2023 04:23
if err := ml.loadTemplateIfExists(modelName, modelFile); err != nil {
return "", err
}
func evaluateTemplate[T any](templateName string, in T, modelPath string, templateMap *(map[string]*template.Template), mutex *sync.Mutex) (string, error) {
Copy link
Owner

@mudler mudler Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any specific need for the generics here? I'd keep them only when strictly needed otherwise it is just confusing (what's the catch instead of using an interface?)

Copy link
Collaborator Author

@dave-gray101 dave-gray101 Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mudler ! I'm still catching on to go style, but I'll admit I'm a big fan of generics in most cases... probably more than I should be!

I suppose we could rewrite this to interface{} instead of T - personally, I think the T approach is clearer to read, as we could point at line 119 for example to see that TemplateForChatMessage expects and only works with ChatMesageTemplate, and not interface{}. Is there another way to express this type of constraint in go that I've overlooked?

Theoretically, we could just drop the generic parameter and pass it in as interface{} if that convention is already clear in most cases to go programmers :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine either ways. I'm just a bit not super convinced in this case because raises code complexity (see e.g. pointer of maps) for no real catch

}

func (ml *ModelLoader) TemplateForChatMessage(templateName string, messageData ChatMessageTemplateData) (string, error) {
return evaluateTemplate[ChatMessageTemplateData](templateName, messageData, ml.ModelPath, &(ml.chatMessageTemplates), &(ml.mu))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering, why passing the pointer to the map around?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the pointer to the map is because evaluateTemplate[T] was generic - See other comment for style choice on why this line in general is generic.

If that function is generic, it can't accept an *ModelLoader receiver, so I just have it accept a pointer to the relevant lock and map... admittedly, a hack, but it's "contained" to the private method here in this file at least.

If we de-generic that method, I would presumably remove it.

Copy link
Owner

@mudler mudler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall here! great job @dave-gray101 and @tmm1! just few nits from my side but that shouldn't block this

content = fmt.Sprint(*i.Content)
if templatedChatMessage == "" {
log.Warn().Msgf("template \"%s\" produced blank output for %+v. Skipping!", config.TemplateConfig.ChatMessage, chatMessageData)
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe instead of skipping here would be better to skip templating and using the message as-is? making it disappear would be probably confusing

Copy link
Collaborator Author

@dave-gray101 dave-gray101 Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well - I think we'd theoretically want to allow a template to ignore a message and return "" - so the continue here really only avoids appending "" on to a slice of strings, right - would result in one additional \n if we don't skip adding it.

I admit it's probably not super clear that's the only effect of that statement, so I'll see if I can revise it!

The Warn is because I assume rarely the user will intend for a "" to be returned from a template, so we shouldn't call that an outright error, but it also is worth making visible when debug=true since most of the time it's probably a template logic error.

templatedChatMessage, err := o.Loader.TemplateForChatMessage(config.TemplateConfig.ChatMessage, chatMessageData)
if err != nil {
log.Error().Msgf("error processing message %+v using template \"%s\": %v. Skipping!", chatMessageData, config.TemplateConfig.ChatMessage, err)
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto on skipping

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, in this case, I think I see a much stronger argument for falling back on the sprintf formatting, as long as we keep the error message for the template error :)

@Aisuko Aisuko self-requested a review July 22, 2023 02:48
Copy link
Collaborator

@Aisuko Aisuko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. I suggest rebasing 13 commits to several reasonable commits. It will be easy for us to split features.

@dave-gray101 dave-gray101 disabled auto-merge July 22, 2023 06:45
@dave-gray101
Copy link
Collaborator Author

Thanks for your contribution. I suggest rebasing 13 commits to several reasonable commits. It will be easy for us to split features.

I was planning on squash merging the PR to a single commit, but I can rewrite history into a new branch and merge as several if that's the style we prefer.

@mudler
Copy link
Owner

mudler commented Jul 22, 2023

@dave-gray101 that's fine, just squash them during merge

@mudler mudler changed the title Prompt Template Improvements feat(llama2): add template for chat messages Jul 22, 2023
Copy link
Owner

@mudler mudler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@dave-gray101 dave-gray101 merged commit c6bf67f into mudler:master Jul 22, 2023
@mudler mudler added the enhancement New feature or request label Jul 22, 2023
@dave-gray101 dave-gray101 deleted the exp_llama2 branch February 21, 2024 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants