Chat Stateful Streaming

ARK Platform Example / Chat Stateful Streaming

  • Copy
    
    import openai
    import uuid
    
    ark_api_key = "API_KEY"
    ark_base_url = "https://api.ark-labs.cloud/api/v1"
    model_name = "gpt-4o"
    
    client = openai.OpenAI(api_key=ark_api_key, base_url=ark_base_url)
    
    # generate a conversation/session UUID once and reuse it for all requests
    ark_session_id = str(uuid.uuid4())
    
    def stream_completion(messages):
        response = client.chat.completions.create(
            model=model_name,
            messages=messages,
            extra_body={"ark_stateful": ark_session_id},  # enable stateful mode, using the same UUID for all requests
            stream=True,
            max_tokens=8000,
        )
        for chunk in response:
            delta = chunk.choices[0].delta
            if delta and delta.content:
                print(delta.content, end="", flush=True)
        print()
    
    first_messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user",
            "content": (
                "Tell me a story about a brave knight traversing space in a small rocket "
                "who's lost because GPS only works on Earth. 200 words."
            ),
        },
    ]
    print("\nFirst response:")
    stream_completion(first_messages)
    
    second_messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        # no need to resend history, ark_stateful mode remembers it
        {"role": "user", "content": "Translate the story to German, please."},
    ]
    print("\nSecond response:")
    stream_completion(second_messages)