Skip to main content
You can use OpenAI Python SDK to interact with FriendliAI. This makes migration of existing applications already using OpenAI particularly easy.

How to Use

Before you start, ensure the base_url and api_key refer to FriendliAI. FriendliAI is fully compatible with the OpenAI SDK, so you can follow the examples below. Choose one of the available models for the model parameter.
pip install -qU openai

Chat Completion

Chat completion API that generates a response from a given conversation. Choose the example that best fits your needs:
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.friendli.ai/serverless/v1",
    api_key=os.environ.get("API_KEY")
)

completion = client.chat.completions.create(
    model="zai-org/GLM-5.2",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

print(completion.choices[0].message)
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.friendli.ai/serverless/v1",
    api_key=os.environ.get("API_KEY")
)

completion = client.chat.completions.create(
    model="zai-org/GLM-5.2",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    stream=True
)

for chunk in completion:
    print(chunk.choices[0].delta)
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.friendli.ai/serverless/v1",
    api_key=os.environ.get("API_KEY")
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        }
    }
]

completion = client.chat.completions.create(
    model="zai-org/GLM-5.2",
    messages=[
        {"role": "user", "content": "What's the weather like in Boston today?"}
    ],
    tools=tools,
    tool_choice="auto"
)

print(completion)
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.friendli.ai/serverless/v1",
    api_key=os.environ.get("API_KEY")
)

completion = client.chat.completions.create(
    model="zai-org/GLM-5.2",
    messages=[
        {"role": "user", "content": "Hello!"}
    ],
    logprobs=True,
    top_logprobs=2
)

print(completion.choices[0].message)
print(completion.choices[0].logprobs)
Last modified on July 6, 2026