import openai
from openai import OpenAI

# 需要补充一下属性
baseUrl="https://wishub.ctyun.cn:1443/v1" #api前缀地址
# 从环境变量获取API密钥，如果没有设置，也可以直接终端执行export XIRANG_app_key="xxx"
appKey=os.getenv("XIRANG_app_key")
modelId="xxxxx" #模型id
question="你好，3.01+103.1等于多少" #对话问题

def main():
    client = OpenAI(base_url=baseUrl, api_key=appKey)
    messages = [
        {"role": "user", "content": question}
    ]
    try:
        res = client.chat.completions.create(
            model=modelId,
            messages=messages,
            stream=False
        )
        print(res.choices[0].message.content or "", end="", flush=True)

    except openai.APIStatusError as e:
        print(f"APIStatusError: {e.status_code}, {e.message}, {e.body}")
    except openai.APIError as e:
        print(f"APIError: {e.body}")
    except Exception as e:
        print(f"Exception: {e}")

if __name__ == "__main__":
    main()
