"doc/git@developer.sourcefind.cn:OpenDAS/ktransformers.git" did not exist on "ed2b971e025245a77ab91f02a769ba0229d01cdf"
Unverified Commit 4918fae5 authored by royjhan's avatar royjhan Committed by GitHub
Browse files

OpenAI v1/completions: allow stop token list (#5551)

* stop token parsing fix

* add stop test
parent 0aff6787
...@@ -338,12 +338,16 @@ func fromCompleteRequest(r CompletionRequest) (api.GenerateRequest, error) { ...@@ -338,12 +338,16 @@ func fromCompleteRequest(r CompletionRequest) (api.GenerateRequest, error) {
switch stop := r.Stop.(type) { switch stop := r.Stop.(type) {
case string: case string:
options["stop"] = []string{stop} options["stop"] = []string{stop}
case []string: case []any:
options["stop"] = stop var stops []string
default: for _, s := range stop {
if r.Stop != nil { if str, ok := s.(string); ok {
return api.GenerateRequest{}, fmt.Errorf("invalid type for 'stop' field: %T", r.Stop) stops = append(stops, str)
} else {
return api.GenerateRequest{}, fmt.Errorf("invalid type for 'stop' field: %T", s)
}
} }
options["stop"] = stops
} }
if r.MaxTokens != nil { if r.MaxTokens != nil {
......
...@@ -79,6 +79,7 @@ func TestMiddlewareRequests(t *testing.T) { ...@@ -79,6 +79,7 @@ func TestMiddlewareRequests(t *testing.T) {
Model: "test-model", Model: "test-model",
Prompt: "Hello", Prompt: "Hello",
Temperature: &temp, Temperature: &temp,
Stop: []string{"\n", "stop"},
} }
bodyBytes, _ := json.Marshal(body) bodyBytes, _ := json.Marshal(body)
...@@ -99,6 +100,16 @@ func TestMiddlewareRequests(t *testing.T) { ...@@ -99,6 +100,16 @@ func TestMiddlewareRequests(t *testing.T) {
if genReq.Options["temperature"] != 1.6 { if genReq.Options["temperature"] != 1.6 {
t.Fatalf("expected 1.6, got %f", genReq.Options["temperature"]) t.Fatalf("expected 1.6, got %f", genReq.Options["temperature"])
} }
stopTokens, ok := genReq.Options["stop"].([]any)
if !ok {
t.Fatalf("expected stop tokens to be a list")
}
if stopTokens[0] != "\n" || stopTokens[1] != "stop" {
t.Fatalf("expected ['\\n', 'stop'], got %v", stopTokens)
}
}, },
}, },
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment