Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Stop sequence support #44

Open
jacoblee93 opened this issue Oct 5, 2024 · 1 comment
Open

Feature request: Stop sequence support #44

jacoblee93 opened this issue Oct 5, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@jacoblee93
Copy link

jacoblee93 commented Oct 5, 2024

It would be nice in certain situations to be able to pass a list of stop sequences as input:

For example, see: https://platform.openai.com/docs/api-reference/chat/create#chat-create-stop

When the model would generate a sequence like this, it would instead stop and return the current generation to the user. This more easily allows for more advanced prompting techniques like LangChain's original text-based ReAct agent loop, where we want the model to not generate an Observation: and instead have it populated by an external tool call.

We can get around this by streaming a generation and cancelling the request when we detect a stop sequence in the accumulated output, but this is a bit less nice.

@tomayac
Copy link
Contributor

tomayac commented Oct 7, 2024

People discovering this issue via search, the client-side workaround is as follows:

const abortController = new AbortController();
const signal = abortController.signal;

const STOP_SEQUENCES = ['world'];

const assistant = await self.ai.assistant.create();
const stream = await assistant.promptStreaming('Say "Hello, world!"', {
  signal,
});

let previousLength = 0;
streamingLoop: for await (const chunk of stream) {
  const newContent = chunk.slice(previousLength);
  console.log(`Chunk: "${newContent}"`);
  for (const stopSequence of STOP_SEQUENCES) {
    if (newContent.toLowerCase().includes(stopSequence.toLowerCase())) {
      console.log(
        `Stop sequence "${stopSequence}" found in chunk "${newContent}". Aborting.`
      );
      abortController.abort();
      break streamingLoop;
    }
  }
  document.body.insertAdjacentText('beforeEnd', newContent);
  previousLength = chunk.length;
}

You can see this pattern in action in this demo. I have also added this tip to the documentation.

@domenic domenic added the enhancement New feature or request label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants