guidance icon indicating copy to clipboard operation
guidance copied to clipboard

Streaming example

Open rawsh opened this issue 1 year ago • 6 comments

Can an example of streaming output as a generator be added? My use case is replacing langchain in production for a QA system.

rawsh avatar May 17 '23 20:05 rawsh

As a follow up - it would be great if the library allowed doing something like the following without blocking the event loop by internally calling openai.ChatCompletion.acreate instead of create.

import asyncio
import guidance


guidance.llm = guidance.llms.OpenAI(model='gpt-3.5-turbo')

program = guidance("""
{{#user~}}
Write A poem about sparkling water
{{~/user}}
{{#assistant~}}
{{gen 'poem'}}
{{~/assistant}}
""")


async def counter():
    for x in range(5):
        print(x, flush=True)
        await asyncio.sleep(1)


async def generate():
    # Currently unsupported
    async for chunk in program(async_mode=True, stream=True):
        print(chunk, flush=True)


async def main():
    await asyncio.gather(generate(), counter())


asyncio.run(main())

vaharoni avatar May 21 '23 13:05 vaharoni

I've got a working workaround here https://github.com/andaag/chattergpt/blob/e199ccc99b7d8476275f4ed5720b268d506490c1/shared.py#L41 - it's a bit ugly as it accesses internal classes, but should work fine until something is implemented.

andaag avatar May 22 '23 06:05 andaag

This is indeed much needed. Will post when this is pushed.

slundberg avatar May 23 '23 19:05 slundberg

I've got a working workaround here https://github.com/andaag/chattergpt/blob/e199ccc99b7d8476275f4ed5720b268d506490c1/shared.py#L41 - it's a bit ugly as it accesses internal classes, but should work fine until something is implemented.

Thanks for the link, using the internal done function is what I was missing to get hacky streaming working with FastAPI

rawsh avatar May 23 '23 19:05 rawsh

This is indeed much needed. Will post when this is pushed.

Thank you @slundberg . The guidance library has tremendous potential for controlling nuanced chatbot behavior vs. langchain and we appreciate your efforts.

yermie avatar May 24 '23 23:05 yermie

0.0.56 now has a first version of streaming support. We will need to also update library calls to update variables more frequently, but any comments or feedback are welcome over in #129

slundberg avatar May 26 '23 23:05 slundberg

Closing for now, feel free to reopen if needed.

marcotcr avatar Jun 06 '23 16:06 marcotcr