Open logancyang opened 2 hours ago
The issue you're encountering with the OllamaEmbeddings
class not respecting the context length is likely related to the handling of input strings that exceed the model's context window. By default, the OllamaEmbeddings
class does not truncate input text to fit within the model's context window unless explicitly configured to do so.
To address this, you can set the truncate
parameter to true
when creating an instance of OllamaEmbeddings
. This will ensure that the input text is truncated to fit within the model's context window, potentially resolving the HTTP 400 errors you're seeing.
Here's how you can modify your code:
const embeddings = new OllamaEmbeddings({
baseUrl: "http://127.0.0.1:11434",
model: "nomic-embed-text",
truncate: true, // Add this line to enable truncation
});
This change should help manage the input length and prevent the 400 errors caused by exceeding the context length [1].
To continue talking to Dosu, mention @dosu.
@dosubot I don't want truncation though, curl works without truncation and makes use of the full context length 8192 (at least I suppose it's doing that without truncating internally).
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Ollama log: [GIN] 2024/11/04 - 22:02:56 | 400 | 5.3135ms | 127.0.0.1 | POST "/api/embed" [GIN] 2024/11/04 - 22:02:57 | 400 | 10.841458ms | 127.0.0.1 | POST "/api/embed" [GIN] 2024/11/04 - 22:03:01 | 400 | 10.250083ms | 127.0.0.1 | POST "/api/embed" [GIN] 2024/11/04 - 22:03:07 | 400 | 8.79875ms | 127.0.0.1 | POST "/api/embed" [GIN] 2024/11/04 - 22:03:20 | 400 | 9.281166ms | 127.0.0.1 | POST "/api/embed" [GIN] 2024/11/04 - 22:03:44 | 400 | 14.08825ms | 127.0.0.1 | POST "/api/embed"
Description
I'm trying to use OllamaEmbedding ("@langchain/ollama": "0.1.1"). I tested
curl
with this same string and it worked. But testing with the test script above it fails with 400. Shortening the string makes it work, so it must be an issue with the OllamaEmbedding client's context length setting. Please help.System Info
{ "name": "nodetest", "type": "module", "dependencies": { "@langchain/ollama": "0.1.1", "node-fetch": "^3.3.2" } }