Skip to content

FunASR-powered video transcription, subtitle generation, and LLM-assisted clipping tool with a local Gradio UI.

4.7 KB
Raw Blame
import os
import logging
from openai import OpenAI

ATLASCLOUD_API_BASE = "https://api.atlascloud.ai/v1"
ATLASCLOUD_MODEL_PREFIX = "atlascloud/"

# MiniMax exposes an OpenAI-compatible chat completions endpoint. The global
# endpoint is used by default; set MINIMAX_API_BASE to the mainland China host
# (https://api.minimaxi.com/v1) to route requests through the cn_zh region.
MINIMAX_API_BASE = "https://api.minimax.io/v1"
MINIMAX_API_BASE_CN = "https://api.minimaxi.com/v1"
MINIMAX_MODEL_PREFIX = "minimax/"

# OrcaRouter is an OpenAI-compatible smart-routing gateway: one chat
# completions endpoint that routes each request to the best model for the
# task. Model IDs carry an `orcarouter/` prefix (e.g. `orcarouter/auto`) and
# must be sent to the gateway verbatim — a bare `auto` is not routable.
ORCAROUTER_API_BASE = "https://api.orcarouter.ai/v1"
ORCAROUTER_MODEL_PREFIX = "orcarouter/"

# Cheaper Inference is an OpenAI-compatible LLM gateway. Its model IDs are
# bare (e.g. `gpt-5.4-mini`), so the `cheaperinference/` prefix is stripped.
CHEAPER_INFERENCE_API_BASE = "https://api.cheaperinference.com/v1"
CHEAPER_INFERENCE_MODEL_PREFIX = "cheaperinference/"


def _resolve_model_config(model):
    base_url = None
    api_key_env = None

    if model.startswith(ATLASCLOUD_MODEL_PREFIX):
        model = model[len(ATLASCLOUD_MODEL_PREFIX):]
        if not model:
            raise ValueError(
                "Model name is empty after stripping atlascloud/ prefix"
            )
        base_url = os.environ.get("ATLASCLOUD_API_BASE", ATLASCLOUD_API_BASE).strip()
        if not base_url:
            base_url = ATLASCLOUD_API_BASE
        api_key_env = "ATLASCLOUD_API_KEY"
    elif model.startswith(MINIMAX_MODEL_PREFIX):
        model = model[len(MINIMAX_MODEL_PREFIX):]
        if not model:
            raise ValueError(
                "Model name is empty after stripping minimax/ prefix"
            )
        base_url = os.environ.get("MINIMAX_API_BASE", MINIMAX_API_BASE).strip()
        if not base_url:
            base_url = MINIMAX_API_BASE
        api_key_env = "MINIMAX_API_KEY"
    elif model.startswith(ORCAROUTER_MODEL_PREFIX):
        if len(model) <= len(ORCAROUTER_MODEL_PREFIX):
            raise ValueError(
                "Model name is empty after stripping orcarouter/ prefix"
            )
        base_url = os.environ.get("ORCAROUTER_API_BASE", ORCAROUTER_API_BASE).strip()
        if not base_url:
            base_url = ORCAROUTER_API_BASE
        api_key_env = "ORCAROUTER_API_KEY"
    elif model.startswith(CHEAPER_INFERENCE_MODEL_PREFIX):
        model = model[len(CHEAPER_INFERENCE_MODEL_PREFIX):]
        if not model:
            raise ValueError(
                "Model name is empty after stripping cheaperinference/ prefix"
            )
        base_url = os.environ.get("CHEAPER_INFERENCE_API_BASE", CHEAPER_INFERENCE_API_BASE).strip()
        if not base_url:
            base_url = CHEAPER_INFERENCE_API_BASE
        api_key_env = "CHEAPER_INFERENCE_API_KEY"
    elif model.startswith("deepseek"):
        base_url = "https://api.deepseek.com"
    elif model.startswith("gpt-3.5-turbo"):
        base_url = "https://api.moonshot.cn/v1"

    return model, base_url, api_key_env


if __name__ == '__main__':
    from llm.demo_prompt import demo_prompt
    client = OpenAI(
        # This is the default and can be omitted
        api_key=os.environ.get("OPENAI_API_KEY"),
    )

    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": demo_prompt,
            }
        ],
        model="gpt-3.5-turbo-0125",
    )
    print(chat_completion.choices[0].message.content)
    
    
def openai_call(apikey, 
                model="gpt-3.5-turbo", 
                user_content="如何做西红柿炖牛腩?", 
                system_content=None):
    model, base_url, api_key_env = _resolve_model_config(model)
    if not apikey and api_key_env:
        apikey = os.environ.get(api_key_env, "").strip()
        if not apikey:
            raise ValueError(
                f"Missing API key: pass apikey or set {api_key_env}"
            )

    client = OpenAI(
        # This is the default and can be omitted
        api_key=apikey,
        base_url=base_url
    )
    if system_content is not None and len(system_content.strip()):
        messages = [
            {'role': 'system', 'content': system_content},
            {'role': 'user', 'content': user_content}
      ]
    else:
        messages = [
            {'role': 'user', 'content': user_content}
      ]
    
    chat_completion = client.chat.completions.create(
        messages=messages,
        model=model,
    )
    logging.info("Openai model inference done.")
    return chat_completion.choices[0].message.content