sampler/runner: enable gpt-oss structured outputs

This commit is contained in:
ParthSareen 2025-09-03 15:04:20 -07:00
parent 40d3436cd1
commit 1fe7e07f63
3 changed files with 23 additions and 7 deletions

View File

@ -52,6 +52,7 @@ type HarmonyParser struct {
MessageStartTag string
MessageEndTag string
HeaderEndTag string
ConstrainAllowed bool
acc strings.Builder
lifetimeAcc strings.Builder
}
@ -328,6 +329,7 @@ func (h *HarmonyMessageHandler) AddContent(content string, toolParser *HarmonyTo
}
case "final":
h.state = harmonyMessageState_Normal
h.HarmonyParser.ConstrainAllowed = true
}
case HarmonyEventContentEmitted:
logutil.Trace("harmony event content", "content", event.Content, "state", h.state)

View File

@ -814,7 +814,7 @@ func (s *Server) completion(w http.ResponseWriter, r *http.Request) {
req.Options.TopP,
req.Options.MinP,
req.Options.Seed,
grammar,
nil,
)
seq, err := s.NewSequence(req.Prompt, req.Images, NewSequenceParams{
@ -865,6 +865,12 @@ func (s *Server) completion(w http.ResponseWriter, r *http.Request) {
return
}
// TODO(parthsareen): generalize grammar enablement on the fly for all thinking models
if harmonyMessageHandler == nil {
seq.sampler.SetGrammar(grammar)
}
grammarSet := false
for {
select {
case <-r.Context().Done():
@ -877,6 +883,10 @@ func (s *Server) completion(w http.ResponseWriter, r *http.Request) {
var toolContent string
content, thinking, toolContent = harmonyMessageHandler.AddContent(content, harmonyToolParser)
harmonyToolParser.Add(toolContent)
if harmonyMessageHandler.HarmonyParser.ConstrainAllowed && !grammarSet {
seq.sampler.SetGrammar(grammar)
grammarSet = true
}
}
if err := json.NewEncoder(w).Encode(&llm.CompletionResponse{

View File

@ -25,6 +25,10 @@ type Sampler struct {
grammar *GrammarSampler
}
func (s *Sampler) SetGrammar(grammar *GrammarSampler) {
s.grammar = grammar
}
func (s *Sampler) Sample(logits []float32) (int32, error) {
if len(logits) == 0 {
return -1, errors.New("sample: no logits provided to sample")