Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
ceac416e
"examples/distributed/graphsage/node_classification.py" did not exist on "39260242a254375436d5fc903d5545b511d0d981"
Unverified
Commit
ceac416e
authored
Sep 18, 2025
by
Michael Yang
Committed by
GitHub
Sep 18, 2025
Browse files
fix(integration): check truncated length (#12337)
parent
2717dce6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
62 deletions
+109
-62
integration/embed_test.go
integration/embed_test.go
+101
-61
server/routes.go
server/routes.go
+8
-1
No files found.
integration/embed_test.go
View file @
ceac416e
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"testing"
"testing"
"time"
"time"
"github.com/google/go-cmp/cmp"
"github.com/ollama/ollama/api"
"github.com/ollama/ollama/api"
)
)
...
@@ -44,9 +45,8 @@ func TestAllMiniLMEmbeddings(t *testing.T) {
...
@@ -44,9 +45,8 @@ func TestAllMiniLMEmbeddings(t *testing.T) {
}
}
res
,
err
:=
embeddingTestHelper
(
ctx
,
client
,
t
,
req
)
res
,
err
:=
embeddingTestHelper
(
ctx
,
client
,
t
,
req
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"error: %v"
,
err
)
t
.
Fatal
(
err
)
}
}
if
len
(
res
.
Embedding
)
!=
384
{
if
len
(
res
.
Embedding
)
!=
384
{
...
@@ -74,9 +74,8 @@ func TestAllMiniLMEmbed(t *testing.T) {
...
@@ -74,9 +74,8 @@ func TestAllMiniLMEmbed(t *testing.T) {
}
}
res
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
req
)
res
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
req
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"error: %v"
,
err
)
t
.
Fatal
(
err
)
}
}
if
len
(
res
.
Embeddings
)
!=
1
{
if
len
(
res
.
Embeddings
)
!=
1
{
...
@@ -112,9 +111,8 @@ func TestAllMiniLMBatchEmbed(t *testing.T) {
...
@@ -112,9 +111,8 @@ func TestAllMiniLMBatchEmbed(t *testing.T) {
}
}
res
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
req
)
res
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
req
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"error: %v"
,
err
)
t
.
Fatal
(
err
)
}
}
if
len
(
res
.
Embeddings
)
!=
2
{
if
len
(
res
.
Embeddings
)
!=
2
{
...
@@ -156,93 +154,135 @@ func TestAllMiniLMEmbedTruncate(t *testing.T) {
...
@@ -156,93 +154,135 @@ func TestAllMiniLMEmbedTruncate(t *testing.T) {
truncTrue
,
truncFalse
:=
true
,
false
truncTrue
,
truncFalse
:=
true
,
false
type
testReq
struct
{
want
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
api
.
EmbedRequest
{
Name
string
Model
:
"all-minilm"
,
Request
api
.
EmbedRequest
Input
:
"why"
,
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
reqs
:=
[]
testReq
{
cases
:=
[]
struct
{
name
string
request
api
.
EmbedRequest
check
func
(
*
api
.
EmbedResponse
,
error
)
}{
{
{
N
ame
:
"
T
arget
T
runcation"
,
n
ame
:
"
t
arget
t
runcation"
,
R
equest
:
api
.
EmbedRequest
{
r
equest
:
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Model
:
"all-minilm"
,
Input
:
"why"
,
Input
:
"why"
,
},
},
check
:
func
(
got
*
api
.
EmbedResponse
,
err
error
)
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
diff
:=
cmp
.
Diff
(
want
.
Embeddings
[
0
],
got
.
Embeddings
[
0
]);
diff
!=
""
{
t
.
Errorf
(
"embedding mismatch (-want +got):
\n
%s"
,
diff
)
}
},
},
},
{
{
N
ame
:
"
D
efault
T
runcate"
,
n
ame
:
"
d
efault
t
runcate"
,
R
equest
:
api
.
EmbedRequest
{
r
equest
:
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Model
:
"all-minilm"
,
Input
:
"why is the sky blue?"
,
Input
:
"why is the sky blue?"
,
Options
:
map
[
string
]
any
{
"num_ctx"
:
1
},
Options
:
map
[
string
]
any
{
"num_ctx"
:
3
},
},
check
:
func
(
got
*
api
.
EmbedResponse
,
err
error
)
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
diff
:=
cmp
.
Diff
(
want
.
Embeddings
[
0
],
got
.
Embeddings
[
0
]);
diff
!=
""
{
t
.
Errorf
(
"embedding mismatch (-want +got):
\n
%s"
,
diff
)
}
},
},
{
name
:
"explicit truncate"
,
request
:
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Input
:
"why is the sky blue?"
,
Truncate
:
&
truncTrue
,
Options
:
map
[
string
]
any
{
"num_ctx"
:
3
},
},
check
:
func
(
got
*
api
.
EmbedResponse
,
err
error
)
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
diff
:=
cmp
.
Diff
(
want
.
Embeddings
[
0
],
got
.
Embeddings
[
0
]);
diff
!=
""
{
t
.
Errorf
(
"embedding mismatch (-want +got):
\n
%s"
,
diff
)
}
},
},
{
name
:
"truncate error"
,
request
:
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Input
:
"why is the sky blue?"
,
Truncate
:
&
truncFalse
,
Options
:
map
[
string
]
any
{
"num_ctx"
:
3
},
},
check
:
func
(
res
*
api
.
EmbedResponse
,
err
error
)
{
if
err
.
Error
()
!=
"input exceeds maximum context length"
{
t
.
Fatalf
(
"expected truncation error, got: %v"
,
err
)
}
},
},
},
},
{
{
N
ame
:
"
Explicit T
runcate"
,
n
ame
:
"
input after t
runcate
error
"
,
R
equest
:
api
.
EmbedRequest
{
r
equest
:
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Model
:
"all-minilm"
,
Input
:
"why is the sky blue?"
,
Input
:
"why is the sky blue?"
,
Truncate
:
&
truncTrue
,
Truncate
:
&
truncTrue
,
Options
:
map
[
string
]
any
{
"num_ctx"
:
1
},
Options
:
map
[
string
]
any
{
"num_ctx"
:
1
},
},
},
check
:
func
(
res
*
api
.
EmbedResponse
,
err
error
)
{
if
err
.
Error
()
!=
"input after truncation exceeds maximum context length"
{
t
.
Fatalf
(
"expected truncation error, got: %v"
,
err
)
}
},
},
{
name
:
"input after truncate error"
,
request
:
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Input
:
"why is the sky blue?"
,
Truncate
:
&
truncTrue
,
Options
:
map
[
string
]
any
{
"num_ctx"
:
0
},
},
check
:
func
(
res
*
api
.
EmbedResponse
,
err
error
)
{
if
err
.
Error
()
!=
"input after truncation exceeds maximum context length"
{
t
.
Fatalf
(
"expected truncation error, got: %v"
,
err
)
}
},
},
},
}
}
res
:=
make
(
map
[
string
]
*
api
.
EmbedResponse
)
for
_
,
req
:=
range
cases
{
t
.
Run
(
req
.
name
,
func
(
t
*
testing
.
T
)
{
for
_
,
req
:=
range
reqs
{
req
.
check
(
embedTestHelper
(
ctx
,
client
,
t
,
req
.
request
))
response
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
req
.
Request
)
})
if
err
!=
nil
{
t
.
Fatalf
(
"error: %v"
,
err
)
}
res
[
req
.
Name
]
=
response
}
if
res
[
"Target Truncation"
]
.
Embeddings
[
0
][
0
]
!=
res
[
"Default Truncate"
]
.
Embeddings
[
0
][
0
]
{
t
.
Fatal
(
"expected default request to truncate correctly"
)
}
if
res
[
"Default Truncate"
]
.
Embeddings
[
0
][
0
]
!=
res
[
"Explicit Truncate"
]
.
Embeddings
[
0
][
0
]
{
t
.
Fatal
(
"expected default request and truncate true request to be the same"
)
}
// check that truncate set to false returns an error if context length is exceeded
_
,
err
:=
embedTestHelper
(
ctx
,
client
,
t
,
api
.
EmbedRequest
{
Model
:
"all-minilm"
,
Input
:
"why is the sky blue?"
,
Truncate
:
&
truncFalse
,
Options
:
map
[
string
]
any
{
"num_ctx"
:
1
},
})
if
err
==
nil
{
t
.
Fatal
(
"expected error, got nil"
)
}
}
}
}
func
embeddingTestHelper
(
ctx
context
.
Context
,
client
*
api
.
Client
,
t
*
testing
.
T
,
req
api
.
EmbeddingRequest
)
(
*
api
.
EmbeddingResponse
,
error
)
{
func
embeddingTestHelper
(
ctx
context
.
Context
,
client
*
api
.
Client
,
t
*
testing
.
T
,
req
api
.
EmbeddingRequest
)
(
*
api
.
EmbeddingResponse
,
error
)
{
if
err
:=
PullIfMissing
(
ctx
,
client
,
req
.
Model
);
err
!=
nil
{
t
.
Helper
()
t
.
Fatalf
(
"failed to pull model %s: %v"
,
req
.
Model
,
err
)
}
response
,
err
:=
client
.
Embeddings
(
ctx
,
&
req
)
if
err
!=
nil
{
if
err
:=
PullIfMissing
(
ctx
,
client
,
req
.
Model
);
err
!=
nil
{
return
nil
,
err
t
.
Fatal
(
err
)
}
}
return
response
,
nil
return
client
.
Embeddings
(
ctx
,
&
req
)
}
}
func
embedTestHelper
(
ctx
context
.
Context
,
client
*
api
.
Client
,
t
*
testing
.
T
,
req
api
.
EmbedRequest
)
(
*
api
.
EmbedResponse
,
error
)
{
func
embedTestHelper
(
ctx
context
.
Context
,
client
*
api
.
Client
,
t
*
testing
.
T
,
req
api
.
EmbedRequest
)
(
*
api
.
EmbedResponse
,
error
)
{
if
err
:=
PullIfMissing
(
ctx
,
client
,
req
.
Model
);
err
!=
nil
{
t
.
Helper
()
t
.
Fatalf
(
"failed to pull model %s: %v"
,
req
.
Model
,
err
)
}
response
,
err
:=
client
.
Embed
(
ctx
,
&
req
)
if
err
:=
PullIfMissing
(
ctx
,
client
,
req
.
Model
);
err
!=
nil
{
t
.
Fatal
(
err
)
if
err
!=
nil
{
return
nil
,
err
}
}
return
response
,
nil
return
client
.
Embed
(
ctx
,
&
req
)
}
}
server/routes.go
View file @
ceac416e
...
@@ -634,7 +634,7 @@ func (s *Server) EmbedHandler(c *gin.Context) {
...
@@ -634,7 +634,7 @@ func (s *Server) EmbedHandler(c *gin.Context) {
ctxLen
:=
min
(
opts
.
NumCtx
,
int
(
kvData
.
ContextLength
()))
ctxLen
:=
min
(
opts
.
NumCtx
,
int
(
kvData
.
ContextLength
()))
if
len
(
tokens
)
>
ctxLen
{
if
len
(
tokens
)
>
ctxLen
{
if
!
truncate
{
if
!
truncate
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
"input
length
exceeds maximum context length"
})
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
"input exceeds maximum context length"
})
return
return
}
}
...
@@ -646,6 +646,13 @@ func (s *Server) EmbedHandler(c *gin.Context) {
...
@@ -646,6 +646,13 @@ func (s *Server) EmbedHandler(c *gin.Context) {
ctxLen
--
ctxLen
--
}
}
slog
.
Info
(
""
,
"ctxLen"
,
ctxLen
,
"tokenCount"
,
len
(
tokens
))
if
ctxLen
<=
0
{
// return error if the truncated input would be empty or just special tokens
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
"input after truncation exceeds maximum context length"
})
return
}
tokens
=
tokens
[
:
ctxLen
]
tokens
=
tokens
[
:
ctxLen
]
s
,
err
=
r
.
Detokenize
(
c
.
Request
.
Context
(),
tokens
)
s
,
err
=
r
.
Detokenize
(
c
.
Request
.
Context
(),
tokens
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment