Reject empty and truncated answers and expose complete tool calls
This commit is contained in:
1 parent
019a38f111
commit
3ca0994437
14 files changed
+278
-7
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/sausheong/harness/llm"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPreservesOutputLimitAndTrailingUsage(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, `data: {"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"partial","type":"function","function":{"name":"bash","arguments":"{\"command\":"}}]},"finish_reason":"length"}]}`+"\n\n")
|
||||
fmt.Fprint(w, `data: {"choices":[],"usage":{"prompt_tokens":50,"completion_tokens":2048,"total_tokens":2098}}`+"\n\ndata: [DONE]\n\n")
|
||||
}))
|
||||
defer srv.Close()
|
||||
p := NewOpenAIProviderWithKind("fixture", srv.URL, "openai-compatible")
|
||||
events, err := p.ChatStream(context.Background(), llm.ChatRequest{Model: "fixture"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for e := range events {
|
||||
if e.Type == llm.EventToolCallDone {
|
||||
t.Fatal("truncated tool accepted")
|
||||
}
|
||||
if e.Type == llm.EventError {
|
||||
t.Fatal(e.Error)
|
||||
}
|
||||
if e.Type == llm.EventDone {
|
||||
found = true
|
||||
if e.StopReason != "length" || e.Usage == nil || e.Usage.OutputTokens != 2048 {
|
||||
t.Fatalf("%+v", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("missing terminal event")
|
||||
}
|
||||
}
|
||||
@@ -274,6 +274,7 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
toolCalls := make(map[int]*pendingTC)
|
||||
|
||||
var lastUsage *llm.Usage
|
||||
var finishReason string
|
||||
|
||||
for {
|
||||
resp, err := stream.Recv()
|
||||
@@ -337,6 +338,9 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
}
|
||||
|
||||
// Finish reason
|
||||
if choice.FinishReason != "" {
|
||||
finishReason = string(choice.FinishReason)
|
||||
}
|
||||
if choice.FinishReason == openai.FinishReasonToolCalls || choice.FinishReason == openai.FinishReasonStop {
|
||||
emitToolCalls(events, toolCalls)
|
||||
clear(toolCalls)
|
||||
@@ -344,7 +348,7 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
}
|
||||
}
|
||||
|
||||
events <- llm.ChatEvent{Type: llm.EventDone, Usage: lastUsage}
|
||||
events <- llm.ChatEvent{Type: llm.EventDone, Usage: lastUsage, StopReason: finishReason}
|
||||
}()
|
||||
|
||||
return events, nil
|
||||
|
||||
Reference in new issue
Block a user