kramcat / CharacterAI

Unofficial Python API for character.ai
https://docs.kram.cat
MIT License
378 stars 51 forks source link

Async? The CAI website streams character data to the screen. Async function does not? #43

Open tschesnok opened 11 months ago

tschesnok commented 11 months ago

Sorry if I'm being a bit dumb here. What is the purpose of the Async functions? I thought they would get me a character stream as the server returns it.

Is it possible to get a stream like I see on the website?

KubaPro010 commented 11 months ago

I think the async functions don't do streaming, but they're just to use within other async functions, for example discord.py

SeoulSKY commented 11 months ago

Async function allows you do a asynchronous programming (aka non-blocking). When you send a request, it blocks the current thread, wasting the resources. With async, while you're waiting for the response from the server, your thread can do other works.

tschesnok commented 11 months ago

Understood. Thanks. Defensively useful and the way to go. Any hope that we can get streaming data from CAI? Latency on a response ends up being much slower than their website since every character needs to arrive before the function returns.

Any tips for me if I build it? (I'm a C++ guy.. but my python skills are growing)

SeoulSKY commented 11 months ago

Understood. Thanks. Defensively useful and the way to go. Any hope that we can get streaming data from CAI? Latency on a response ends up being much slower than their website since every character needs to arrive before the function returns.

Any tips for me if I build it? (I'm a C++ guy.. but my python skills are growing)

Somebody needs to reverse-engineer the API to figure out how to use stream for getting response. I think there is a way since the website uses stream.

KubaPro010 commented 11 months ago

We don't need to reverse engineer the API, the streaming endpoint that is used in this lib sis the endpoint that does streaming, the lib just doesn't do streaming anyways (open dev tools when chatting with characters)

Lines 298 - 304 in characterai.py prove it:

if response.split('\n')[-1].startswith('{"abort"'):
                if filtering:
                    raise errors.FilterError('No eligible candidates')
                else:
                    return json.loads(response.split('\n')[-3])
            else:
                return json.loads(response.split('\n')[-2])

It does a split with newlines, http event streams usually do arrive line by line, and the -1 just means the last one

kramcat commented 11 months ago

and the -1 just means the last one

It shouldn't be -1, there is an empty value at the end of the list, so I wrote -2