[Python] How does streaming work in OpenAI's APIs?

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I have a basic understanding of how event streams work. I am currently converting langchain code to directly use OpenAI's API and I have a piece of code I am a bit confused about.

    def stream_message(
    messages
    ):
    client = OpenAI(api_key=settings.OPENAI_API_KEY)
    response = client.chat.completions.create(messages=messages, model="gpt-4o", temperature=0.5, stream=True, stop=None)
    return response

    response = stream_message(messages,)

    for sse in response:
    content = sse_chunk.choices[0].delta.content
    # some other stuff


    What I'm confused about is that the python function stream_message is executed once and then is iterated. When I put a breakpoint under the initial API call and inspect it, it gives me the entire response from GPT. Is this really streaming data back? How does this work if I call the function once and I iterate the response value underneath? How is the response getting updated as I iterate the object if it really is streaming a response back?

    Continue reading...

Compartilhe esta Página