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

How to cancel a response stream 'mid-stream'? #17

Closed
houstonhaynes opened this issue Jan 7, 2024 · 7 comments · Fixed by #18
Closed

How to cancel a response stream 'mid-stream'? #17

houstonhaynes opened this issue Jan 7, 2024 · 7 comments · Fixed by #18

Comments

@houstonhaynes
Copy link

I've been looking at the code for Ollama and working with its console for a bit and trying to get my head wrapped around how to perform the .NET equivalent of Ctrl+C in the API. Is there such a command?

The goal that I'm seeking is to have the ability to "stop" the current stream of text for a given response so that it can then be ready to take in another request from the API.

This is that use case where the user is like "oh wait this is the wrong way to go" and wants to shut off the response so a new message can be sent. The optimal scenario would be a cancellation token or a specific command that can be sent to shut down the current response. I have thought about simply shutting down the current connection and re-connecting but that seems pretty inelegant.

I like the stream response. I most definitely want to use it. Any thoughts on "shunting" or "stopping" a stream? Thanks!

@awaescher
Copy link
Owner

This is definitely a use case for a CancellationToken. Implementing this to exit the stream is easy to do, however I am not sure if the Ollama API will just continue to stream then. Going to take a look.

awaescher added a commit that referenced this issue Jan 8, 2024
@houstonhaynes
Copy link
Author

houstonhaynes commented Jan 8, 2024

Amazing - I just did the research and "dusted off" the little bit of Go I know - seems you're several steps ahead of me putting in a PR! 🙏 🚀

(FWIW - I had questions about whether the HTTP context 'transfer' would actually trigger the SIGINT)

@awaescher
Copy link
Owner

I just pushed the new version 1.1.0 that allows an optional CancellationToken to every async method.
As far as I could test, cancelling the requests stops the operation on the Ollama host. Tested it with the GPU monitor on the Mac host which went from >80% back to close 0%.

@houstonhaynes
Copy link
Author

I asked around and got the impression that the HTTP context will "handle itself" which always makes me pull back a bit. Someone originally suggested just closing and re-opening the connection and that felt too ham-handed but I'd be willing to do it as an interim step. This feels much more harmonious to the original intent. Thanks again!

@awaescher
Copy link
Owner

awaescher commented Jan 8, 2024

I don't think you'll have to do more than pulling the trigger on a cancellation token. I mean you could simply use a CancellationTokenSource, pass its token through the Ollama calls and cancel it whenever you need, like when the user hits a stop button.

@houstonhaynes
Copy link
Author

Oh yeah - I totally get it now - I was just replaying my hand-wringing when I was first looking at this without having "pierced the veil" of looking at the Go code and making a few educated guesses. 😺

@houstonhaynes
Copy link
Author

Really nice work - passing CT to the Ollama instance running on the server is at the end of this little walk-thorugh. There's a bit of lag in reporting out the server GPU but it's definitely doing what's expected.

Screen.Recording.2024-01-13.at.11.56.48.AM.mov

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants