Skip to content

Commit 90ef9a3

Browse files
committed
Antripic - Handling the second type of token count exceeded message - "input length and max_tokens exceed context limit..."
1 parent 9982edb commit 90ef9a3

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/service/HandleAnthropicErrorCodes.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import io.cequence.wsclient.service.WSClient
1010
*/
1111
trait HandleAnthropicErrorCodes extends WSClient {
1212

13+
private val TokenCountExceededMessages = Set(
14+
"input length and `max_tokens` exceed context limit",
15+
"prompt is too long"
16+
)
17+
1318
override protected def handleErrorCodes(
1419
httpCode: Int,
1520
message: String
@@ -19,7 +24,7 @@ trait HandleAnthropicErrorCodes extends WSClient {
1924

2025
case 400 => {
2126
// Check if the error message indicates token count exceeded
22-
if (message.toLowerCase.contains("prompt is too long") && message.toLowerCase.contains("tokens")) {
27+
if (TokenCountExceededMessages.exists(message.contains)) {
2328
throw new AnthropicScalaTokenCountExceededException(errorMessage)
2429
} else {
2530
// 400 - invalid_request_error: There was an issue with the format or content of your request.

openai-count-tokens/src/main/scala/io/cequence/openaiscala/service/OpenAICountTokensHelper.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ trait OpenAICountTokensHelper {
166166
* @param modelType
167167
* @return
168168
*/
169-
protected def countTokens(
169+
def countTokens(
170170
text: String,
171171
modelType: Option[ModelType] = None
172172
) = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.cequence.openaiscala.examples.anthropic
2+
3+
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
4+
import io.cequence.openaiscala.domain.{NonOpenAIModelId, SystemMessage, UserMessage}
5+
import io.cequence.openaiscala.examples.{ChatCompletionProvider, ExampleBase}
6+
import io.cequence.openaiscala.service.OpenAIChatCompletionService
7+
8+
import scala.concurrent.Future
9+
10+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
11+
object AnthropicCreateChatCompletionWithOpenAIAdapterTokenCountExceeded2
12+
extends ExampleBase[OpenAIChatCompletionService] {
13+
14+
override val service: OpenAIChatCompletionService = ChatCompletionProvider.anthropic()
15+
16+
private val messages = Seq(
17+
SystemMessage("You are a helpful assistant."),
18+
UserMessage("What is the weather like in Norway?" * 20000)
19+
)
20+
21+
override protected def run: Future[_] =
22+
service
23+
.createChatCompletion(
24+
messages = messages,
25+
settings = CreateChatCompletionSettings(
26+
NonOpenAIModelId.claude_3_7_sonnet_20250219,
27+
// the second type of "token exceeded" error - input + max_tokens > limit (200000)
28+
max_tokens = Some(100000)
29+
)
30+
)
31+
.map { content =>
32+
println(content.contentHead)
33+
}
34+
}

0 commit comments

Comments
 (0)