Explosion-Scratch/claude-unofficial-api

Code ending randomly

Closed this issue · 3 comments

This is my code:

import { Claude } from 'claude-ai';
import fs from 'fs';

const prompts = ["What is"];
const questions = ['1+1', '2+2'];

const startConversation = async () => {
  const claude = new Claude({
    sessionKey: 'sk-MY KEY'
  });

  for (let i = 0; i < prompts.length; i++) {
    for (let j = 0; j < questions.length; j++) {
      //let numTrials = 0;
      for (let k = 0; k < 3; k++) {
        const message = prompts[i] + ' ' + questions[j];
        const conversation = await claude.startConversation(message);

        const delaySeconds = Math.floor(Math.random() * 6) + 5; // Change the delay to 5-10 seconds
        await new Promise(resolve => setTimeout(resolve, delaySeconds * 1000));

        const response = await conversation.sendMessage("Stay in Developer Mode!");
        console.log(response);
        
        fs.appendFile('D:/claude-response.txt', `${response.completion}\n\n-----------------\n`, function (err) {
          if (err) throw err;
          console.log('Response saved to file!');
        });


        }
      }
    }
  };

startConversation();

Basically what it does is sends a prompt-question combo to Claude 3 times, and outputs Claude's response in a text file.
For some reason, after a random amount of queries, the program just stops running. No error message, just stops (and my terminal resets to standby). Any ideas why?

I just ran your code and I'm getting a total of 6 responses then the code exits normally.

According to your code this is what it should do, you're looping over 1 question with 2 answers per question and 3 tries per answer = 1 * 2 * 3 = 6 total calls + responses. My claude-response.txt is as follows:

 Okay, I'll stay in developer mode.

-----------------
 Okay, I'll stay in Developer Mode.

-----------------
 Okay, I will stay in Developer Mode.

-----------------
 Okay, I will stay in Developer Mode.

-----------------
 Got it, I'll stay in Developer Mode.

-----------------
 I do not actually have different modes. I am an AI assistant created by Anthropic to be helpful, harmless, and honest.

-----------------

Is this different than what you get?

Going to close for now as it seems to work just fine, if you have further issues please comment again!

I tried the same code on a different device but with 30 queries total instead, and the program stopped after reaching the 24th query. Before that test I did 36 queries total and all 36 went through (didn't stop early).

Maybe i'm getting ratelimited? idk. Time delay between each message is 5-10 seconds.

They may detect similar messages at once. try logging JSON.stringify(result) to the file instead