mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
Set api key when counting tokens
This commit is contained in:
parent
f3f1cc12c8
commit
35058618b1
@ -74,8 +74,8 @@ function maybeParseAnthropicErrorPromptTooLong(e: any) {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function countTokens(messages: MessageParam[], systemPrompt: string): Promise<number> {
|
async function countTokens(state: ChatState, messages: MessageParam[], systemPrompt: string): Promise<number> {
|
||||||
const anthropic = new Anthropic();
|
const anthropic = new Anthropic({ apiKey: state.apiKey });
|
||||||
const response = await anthropic.messages.countTokens({
|
const response = await anthropic.messages.countTokens({
|
||||||
model: Model,
|
model: Model,
|
||||||
messages,
|
messages,
|
||||||
@ -122,7 +122,7 @@ function compressMessages(messages: MessageParam[]): MessageParam[] {
|
|||||||
|
|
||||||
async function reduceMessageSize(state: ChatState, messages: MessageParam[], systemPrompt: string, maximum: number): Promise<MessageParam[]> {
|
async function reduceMessageSize(state: ChatState, messages: MessageParam[], systemPrompt: string, maximum: number): Promise<MessageParam[]> {
|
||||||
for (let iteration = 0; iteration < 5; iteration++) {
|
for (let iteration = 0; iteration < 5; iteration++) {
|
||||||
const tokens = await countTokens(messages, systemPrompt);
|
const tokens = await countTokens(state, messages, systemPrompt);
|
||||||
|
|
||||||
console.log(`AnthropicReduceMessageSize ${JSON.stringify({ iteration, tokens, maximum })}`);
|
console.log(`AnthropicReduceMessageSize ${JSON.stringify({ iteration, tokens, maximum })}`);
|
||||||
state.infos.push(`AnthropicReduceMessageSize ${JSON.stringify({ iteration, tokens, maximum })}`);
|
state.infos.push(`AnthropicReduceMessageSize ${JSON.stringify({ iteration, tokens, maximum })}`);
|
||||||
|
Loading…
Reference in New Issue
Block a user