implement TemplateConstraints in class transformers.Constraint
Author: MrzEsma
Pull request: no
Labels: name: Feature request

name: Good Difficult Issue

### Feature request

Hello,

I recently came across your [blog post](https://huggingface.co/blog/constrained-beam-search) on constrained beam search and I am thrilled to see such implementations being made available. They are quite impressive!

Currently, I find myself in need of the feature `TemplateConstraints` It would greatly benefit my ongoing projects. Could you please provide an estimate on when this feature might be implemented, or if it is already in the pipeline?

Thank you for your hard work and for providing the community with such valuable tools. I look forward to your response.

Best regards,



### Motivation

My motivation is that I want to implement something like function calling in ChatGPT and this feature opens many doors.

### Your contribution

i dont know about it.

Comments:
Hey! This might be implemented if the community is found needing this feature, and if it makes sens in the overall design of the API so I can't give you any estimate of the time this might take!
Leaving this issue open and pinging @gante ! 

Hello @gante,

I saw that @ArthurZucker brought you into the conversation about adding `TemplateConstraints`. I'm keen on this feature for its potential to enable function calling in open source LLMs. Could we discuss its feasibility?



Hi @MrzEsma 👋 

It is my understanding that constrained beam search is not very used, being limited mostly to experimental settings. It is also very expensive to maintain, so I want to avoid expanding it unless there is a significant demand. As such, I'm going to decline the offer for now :)

I'll do my usual bargain: if this comment reaches 10 reactions, then it means other users have been looking for similar features. In that case, I'd be happy to revisit my decision!

Still experimenting but had a similar need recently but I need to experiment a bit more to organize the thoughts.

> Hi @MrzEsma 👋
> 
> It is my understanding that constrained beam search is not very used, being limited mostly to experimental settings. It is also very expensive to maintain, so I want to avoid expanding it unless there is a significant demand. As such, I'm going to decline the offer for now :)
> 
> I'll do my usual bargain: if this comment reaches 10 reactions, then it means other users have been looking for similar features. In that case, I'd be happy to revisit my decision!

Looks like we've hit the magic number 10 - time for our feature to take flight? :)

Alright, I'll keep up my word -- happy to accept a PR if someone is willing to work on it :)

I like it

How is this looking? 

Since no PR is linked I don't think @gante had the time to dive, but any PR is welcome 🤗 

@ArthurZucker 
@NPranitha, @jychen630, and I were thinking of taking this up. We’re planning to use DBA (Dynamic Beam Allocation) [ref: [DisjunctiveConstraint PR](https://github.com/huggingface/transformers/issues/14081#issuecomment-1004479944)], treating each constraint in the template constraint (assuming it's a list of lists) as a single token. This would allow for interleaved generation and continuation while extending a particular constraint keeping the bins same as DBA. Let us know if this makes sense!