CharacterAI icon indicating copy to clipboard operation
CharacterAI copied to clipboard

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

Open tschesnok opened this issue 2 years ago • 6 comments

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?

tschesnok avatar Jul 30 '23 17:07 tschesnok

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

KubaPro010 avatar Jul 30 '23 20:07 KubaPro010

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.

SeoulSKY avatar Jul 30 '23 20:07 SeoulSKY

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)

tschesnok avatar Jul 31 '23 18:07 tschesnok

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.

SeoulSKY avatar Aug 01 '23 15:08 SeoulSKY

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

KubaPro010 avatar Aug 09 '23 10:08 KubaPro010

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

kramcat avatar Aug 10 '23 15:08 kramcat