15,076
edits
(How to Fix "Unauthorized" Error) |
|||
| Line 23: | Line 23: | ||
* '''200''' — the API key is valid | * '''200''' — the API key is valid | ||
* '''401''' — the API key is invalid or incorrect | * '''401''' — the API key is invalid or incorrect | ||
== How to fix "prompt too long; exceeded max context length" when sending files == | |||
'''Error message:''' | |||
<pre> | |||
HTTP Error 400: {"error":"prompt too long; exceeded max context length by 311181 tokens"} | |||
</pre> | |||
'''Applicable Model:''' | |||
* {{kbd | key=gpt-oss:120b}} | |||
'''Solution''' | |||
Root Cause: The entire file content was concatenated into <code>content</code> as raw text. Ollama's <code>/api/chat</code> has no dedicated "file" field — text/document content must be extracted client-side and embedded directly into the message string, which counts fully against <code>num_ctx</code>. | |||
Incorrect Usage: Whole file dumped straight into content | |||
<pre> | |||
'messages' => [[ | |||
'role' => 'user', | |||
'content' => file''get''contents($path) . "\n\nPlease summary this document" | |||
]] | |||
</pre> | |||
Correct Usage: Truncate / chunk / summarize before sending, and raise <code>num_ctx</code> to match | |||
<pre> | |||
'messages' => [[ | |||
'role' => 'user', | |||
'content' => $chunkedText . "\n\nPlease summary this document" | |||
]], | |||
'options' => [ | |||
'num_ctx' => 32768 | |||
] | |||
</pre> | |||
'''Explanation:''' | |||
Ollama's chat endpoint only has a native <code>images</code> array (base64-encoded, vision models only). There is no <code>files</code> or <code>document</code> parameter. Any non-image file — PDF, txt, docx, log — must be converted to plain text on the client and inserted into <code>content</code>, so its size is not free; it consumes tokens exactly like typed text. | |||
If the resulting text exceeds the model's context window (<code>num_ctx</code>, default is often small, e.g. 2048–4096), the request fails with <code>"prompt too long"</code>. Fix by combining: | |||
* Explicitly setting a larger <code>num_ctx</code> in <code>options</code> (bounded by what the model/hardware can actually support) | |||
* Chunking the file and sending it in multiple requests | |||
* Pre-summarizing or using RAG-style retrieval so only relevant excerpts are sent, instead of the full file | |||
This fix resolves the <code>"prompt too long; exceeded max context length"</code> error when large files are pasted into chat. | |||
== How to fix "json: cannot unmarshal object ..." == | == How to fix "json: cannot unmarshal object ..." == | ||