Chat Stateful

ARK Platform Example / Chat Stateful

  • Copy
    
    import openai
    import uuid
    
    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)
    
    # generate a conversation/session UUID once and reuse it for all requests
    ark_session_id = str(uuid.uuid4())
    
    print("Session ID:", ark_session_id)
    print("Waiting for the first response...")
    
    first = 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."}
        ],
        extra_body={"ark_stateful": ark_session_id},  # enable stateful mode
    )
    
    print()
    print("First response:")
    print(first.choices[0].message.content)
    
    print()
    print("Waiting for the second response...")
    
    second = client.chat.completions.create(
        model="gpt-4o",
        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."}
        ],
        extra_body={"ark_stateful": ark_session_id},  # send the same UUID to continue the conversation in the same context
    )
    
    print()
    print("Second response:")
    print(second.choices[0].message.content)