LitServe icon indicating copy to clipboard operation
LitServe copied to clipboard

% character when streaming

Open rasbt opened this issue 9 months ago • 1 comments

The streaming works really nicely now using the latest litgpt version from main. The only little issue is that it creates a % character e.g.,

⚡ ~/streaming python streaming_client.py --prompt "Write a 1000 word poem"
The moon casts its silvery glow
Across the night's dark, velvet flow
The stars above, a twinkling sea
A celestial show, for you and me

The world is hushed, in quiet sleep
The trees, like sent% 
Screenshot 2024-05-23 at 4 42 22 PM

The code looks like this:

def stream_response(response):
    """
    Handle streaming response from the server and print each line received.
    """
    for line in response.iter_lines():
        # decode the byte to string and print
        if line:
            print(json.loads(line)["output"], end="")


def main():
    # Set up command line argument parsing
    parser = argparse.ArgumentParser(description="Send a prompt to the LitServe server.")
    parser.add_argument("--prompt", type=str, required=True, help="The prompt text to generate text from.")

    # Parse command line arguments
    args = parser.parse_args()

    # Use the provided prompt from the command line
    prompt_text = args.prompt

    # Define the server's URL and the endpoint
    server_url = "http://127.0.0.1:8000"
    predict_endpoint = "/predict"

    # Prepare the request data as a dictionary
    request_data = {
        "prompt": prompt_text
    }

    # Send a POST request to the server with stream=True and receive the streamed response

    with requests.post(f"{server_url}{predict_endpoint}", json=request_data, stream=True) as response:
        if response.status_code == 200:
            # Handle streaming response
            stream_response(response)
        else:
            print(f"Error: Received response code {response.status_code}")

Am I missing something/doing something wrong, is this an artifact, or is this even desired?

rasbt avatar May 23 '24 21:05 rasbt