backend: extract parseOllamaContextLength, 100% coverage
This commit is contained in:
parent
6a60f5014b
commit
13b17cd777
|
|
@ -84,18 +84,26 @@ func (b *OllamaBackend) ContextLength(ctx context.Context) int {
|
|||
return 0
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
cl := parseOllamaContextLength(resp.Body)
|
||||
if cl > 0 {
|
||||
b.ctxLength = cl
|
||||
b.ctxModel = b.model
|
||||
}
|
||||
return cl
|
||||
}
|
||||
|
||||
// parseOllamaContextLength extracts the context_length from an /api/show response body.
|
||||
func parseOllamaContextLength(r io.Reader) int {
|
||||
var result struct {
|
||||
ModelInfo map[string]any `json:"model_info"`
|
||||
}
|
||||
if json.NewDecoder(resp.Body).Decode(&result) != nil {
|
||||
if json.NewDecoder(r).Decode(&result) != nil {
|
||||
return 0
|
||||
}
|
||||
for k, v := range result.ModelInfo {
|
||||
if strings.HasSuffix(k, ".context_length") {
|
||||
if f, ok := v.(float64); ok && f > 0 {
|
||||
b.ctxLength = int(f)
|
||||
b.ctxModel = b.model
|
||||
return b.ctxLength
|
||||
return int(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,3 +140,53 @@ func TestOllamaNDJSON_ScannerError(t *testing.T) {
|
|||
t.Errorf("final = %+v", fin)
|
||||
}
|
||||
}
|
||||
|
||||
// --- parseOllamaContextLength ---
|
||||
|
||||
func TestParseOllamaContextLength_Found(t *testing.T) {
|
||||
body := `{"model_info":{"general.context_length":8192}}`
|
||||
if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 8192 {
|
||||
t.Errorf("cl = %d; want 8192", cl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOllamaContextLength_DifferentPrefix(t *testing.T) {
|
||||
body := `{"model_info":{"llama.context_length":4096}}`
|
||||
if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 4096 {
|
||||
t.Errorf("cl = %d; want 4096", cl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOllamaContextLength_NoKey(t *testing.T) {
|
||||
body := `{"model_info":{"general.embedding_length":768}}`
|
||||
if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 {
|
||||
t.Errorf("cl = %d; want 0", cl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOllamaContextLength_EmptyModelInfo(t *testing.T) {
|
||||
body := `{"model_info":{}}`
|
||||
if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 {
|
||||
t.Errorf("cl = %d; want 0", cl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOllamaContextLength_MalformedJSON(t *testing.T) {
|
||||
if cl := parseOllamaContextLength(strings.NewReader("not json")); cl != 0 {
|
||||
t.Errorf("cl = %d; want 0", cl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOllamaContextLength_ZeroValue(t *testing.T) {
|
||||
body := `{"model_info":{"general.context_length":0}}`
|
||||
if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 {
|
||||
t.Errorf("cl = %d; want 0", cl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOllamaContextLength_NonNumeric(t *testing.T) {
|
||||
body := `{"model_info":{"general.context_length":"not a number"}}`
|
||||
if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 {
|
||||
t.Errorf("cl = %d; want 0", cl)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue