Chat Streaming

ARK Platform Example / Chat Streaming

  • Copy
    
    import openai
    
    ark_api_key = "API_KEY"
    ark_base_url = "https://api.ark-labs.cloud/api/v1"
    
    client = openai.OpenAI(api_key=ark_api_key, base_url=ark_base_url)
    
    print("Waiting for response to start streaming...")
    
    response = client.chat.completions.create(
        model="gpt-4o",
        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."}
        ],
        stream=True
    )
    
    print("Response:")
    for chunk in response:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="", flush=True)