ahyatt / llm

A package abstracting llm capabilities for emacs.
GNU General Public License v3.0
142 stars 19 forks source link

Error handling & JSON parsing #35

Closed r0man closed 3 months ago

r0man commented 3 months ago

Hi @ahyatt,

I tried out the Vertex and OpenAI providers and run into 2 issues:

The errors are now handled in the llm library, but as a user you see this message:

error in process filter: Error calling the LLM: Problem calling GCloud Vertex AI: status: 400 message: Please ensure that multiturn requests alternate between user and model.

The error is not important here. They come from a request failing an me trying again and Vertex getting upset the something is not in order. But the error in process filter: is kind of bad. I'm wondering what I should do in the cases when a function running in the the process filter raises an error, and if this is even something the plz-media-type request function can/should do. From the point of view of the plz-media-type function everything is ok, it got an error response, parsed it correctly, delivered the events to the the handler. The handler decided to raise an exception which plz-media-type is unlikely able to handle. So, maybe the hanlder should not raise a user-error here? Or we should catch it in the llm library and just inform the user via a message?

Wdty?

ahyatt commented 3 months ago

Thank you for catching the error on on-success. The fact that the tests didn't catch this is a bit disturbing. I'll look into why that was. For these media-types, it isn't clear to me as a client what I should be expecting on the :then call when using streaming media-types.

About errors, I think ideally you could have a new param to plz-media-type-request, something like :with-media-signal-handler, in which you can supply a function to handle a signal and it's error message (probably just using the standard conventions for signal handling). It can print a message. Or, it can rethrow the error, which is the default. If it rethrows the error, the connection should be closed to avoid a situation which I had repeatedly with this, which is that I get hundreds of error messages causing emacs to beep repeatedly.

This probably is worth checking with @alphapapa as well, since it should be that this matches the design philosophy of plz.

r0man commented 3 months ago

Your suggestion sounds good. I will give it a try.

r0man commented 3 months ago

@ahyatt I just added a bit more documentation. I hope it clarifies some of your doubts. Please see: https://github.com/ahyatt/llm/pull/35/commits/ec7d38ae1e677519094de8679038df32d330c993

r0man commented 3 months ago

The :then and :else callbacks always will be passed the following:

r0man commented 3 months ago

Hi @ahyatt,

I updated this PR. The OpenAI, Ollama and Vertex providers run fine with the tester.

I had to change the Ollama provider a bit, because:

I left the error handling for errors that happen in the process buffer as a todo, since I'm not sure how you want to deal with this. I think it would be the same for all providers.

I did this slightly different than what you suggested. I catch the error in the process filter, and it gets passed to the ELSE callback instead of adding an additional callback to handle errors. So the :else callback has now 3 error cases:

ahyatt commented 3 months ago

Thanks for the work on this, I'll merge this and test it out on my side.

r0man commented 3 months ago

Ok cool. Thank you!

ahyatt commented 3 months ago

I've changed how errors work, but otherwise all this looks great, thank you!