mirror of
https://github.com/HeyPuter/puter.git
synced 2025-02-03 07:48:46 +08:00
dev: max tokens for claude
This commit is contained in:
parent
3ed1453939
commit
791c6f11f4
@ -3,6 +3,7 @@ const BaseService = require("../../services/BaseService");
|
|||||||
const { whatis } = require("../../util/langutil");
|
const { whatis } = require("../../util/langutil");
|
||||||
const { PassThrough } = require("stream");
|
const { PassThrough } = require("stream");
|
||||||
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||||
|
const APIError = require("../../api/APIError");
|
||||||
|
|
||||||
const PUTER_PROMPT = `
|
const PUTER_PROMPT = `
|
||||||
You are running on an open-source platform called Puter,
|
You are running on an open-source platform called Puter,
|
||||||
@ -13,6 +14,8 @@ const PUTER_PROMPT = `
|
|||||||
user of the driver interface (typically an app on Puter):
|
user of the driver interface (typically an app on Puter):
|
||||||
`.replace('\n', ' ').trim();
|
`.replace('\n', ' ').trim();
|
||||||
|
|
||||||
|
const MAX_CLAUDE_INPUT_TOKENS = 10000;
|
||||||
|
|
||||||
class ClaudeService extends BaseService {
|
class ClaudeService extends BaseService {
|
||||||
static MODULES = {
|
static MODULES = {
|
||||||
Anthropic: require('@anthropic-ai/sdk'),
|
Anthropic: require('@anthropic-ai/sdk'),
|
||||||
@ -79,6 +82,21 @@ class ClaudeService extends BaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const token_count = (() => {
|
||||||
|
const text = JSON.stringify(adapted_messages) +
|
||||||
|
JSON.stringify(system_prompts);
|
||||||
|
|
||||||
|
// This is the most accurate token counter available for Claude.
|
||||||
|
return text.length / 4;
|
||||||
|
})();
|
||||||
|
|
||||||
|
if ( token_count > MAX_CLAUDE_INPUT_TOKENS ) {
|
||||||
|
throw APIError.create('max_tokens_exceeded', null, {
|
||||||
|
input_tokens: token_count,
|
||||||
|
max_tokens: MAX_CLAUDE_INPUT_TOKENS,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if ( stream ) {
|
if ( stream ) {
|
||||||
const stream = new PassThrough();
|
const stream = new PassThrough();
|
||||||
const retval = new TypedValue({
|
const retval = new TypedValue({
|
||||||
|
Loading…
Reference in New Issue
Block a user