Python example

Updated at:

Call the OpenSearch large model answering service from Python using the OpenAI SDK and a custom base URL.

Prerequisites

Before you begin, ensure that you have:

  • The OpenAI Python SDK installed: pip install openai

  • An API key created on the OpenSearch platform

Send a request

All requests use client.chat.completions.create with the ops-qwen-turbo model and the OpenSearch-compatible base URL.

The messages array defines the conversation. Each message has a role:

  • system — sets the assistant's behavior for the session

  • user — a message from the end user

  • assistant — a previous reply injected to provide conversation history for multi-turn chat

from openai import OpenAI

def get_response():
    client = OpenAI(
        api_key="OS_API_KEY",  # Replace OS_API_KEY with your API Key created on the platform
        base_url="http://xxxx-hangzhou.opensearch.aliyuncs.com/compatible-mode/v1",
    )

    completion = client.chat.completions.create(
        model="ops-qwen-turbo",
        messages=[
            {"role": "system", "content": "You are a robot assistant"},
            {"role": "user", "content": "What is the capital of Henan"},
            {"role": "assistant", "content": "Zhengzhou"},
            {"role": "user", "content": "What are some interesting places there"}
        ]
    )

    print(completion.model_dump_json())

if __name__ == '__main__':
    get_response()
Note

Replace xxxx-hangzhou in the base URL with your actual OpenSearch instance endpoint.

Response structure

A successful response returns a Chat Completions object. The model's answer is in choices[0].message.content.

{
    "id": "48657EBF-2B81-4E3E-96B4-27FE7D6E780A",
    "choices": [
        {
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null,
            "message": {
                "content": "Zhengzhou offers a variety of tourist attractions and cultural activities, including the Yellow Emperor's Ancestral Worship Ceremony, Zhengzhou Museum, Zhengzhou Science and Technology Museum, and Zhengzhou Zoo. Visitors can also enjoy scenic leisure spots like Bishagang Park and Zijingshan Park. With its rich cultural heritage and modern amenities, Zhengzhou is well-suited for both tourism and entertainment.",
                "role": "assistant",
                "function_call": null,
                "tool_calls": null
            }
        }
    ],
    "created": 1719567872,
    "model": "ops-qwen-turbo",
    "object": "chat.completion",
    "service_tier": null,
    "system_fingerprint": null,
    "usage": {
        "completion_tokens": 77,
        "prompt_tokens": 38,
        "total_tokens": 115
    }
}