心云AI-xinyun.ai
  1. API接入
心云AI-xinyun.ai
  • 获取key和api地址
  • API接入
    • Cherry studio
    • Chatbox
    • Zotero
    • Omate
    • Vs code+cline
    • Python-openai格式-非流
    • Python-openai格式-流式
    • 生图模型-imagen-3.0-generate-002
  1. API接入

Python-openai格式-非流

import requests
import json

# 设置 API 地址和密钥
api_url = "https://xinyun.ai/v1/chat/completions"
api_key = "sk-xxxxxxx"  # API 密钥

def ask_gpt(prompt: str, model: str = "gemini-2.5-pro") -> str:
    """
    向 Gemini 模型发送请求并获取回复。
    
    参数:
    - prompt: 用户输入的提示词
    - model: 使用的 Gemini 模型
    
    返回:
    - Gmini 的回复
    """
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    data = {
        "model": model,
        "messages": [{"role": "user", "content": prompt}]
    }
    
    try:
        # 发送 POST 请求
        response = requests.post(api_url, headers=headers, json=data)
        
        if response.status_code == 200:
            # 解析响应并提取 Gemini 的回复
            result = response.json()
            return result["choices"][0]["message"]["content"].strip()
        else:
            return f"请求失败,状态码: {response.status_code}, 错误信息: {response.text}"
    except Exception as e:
        return f"请求出错: {str(e)}"

if __name__ == "__main__":
    print("欢迎使用 Gemini API 测试程序!")
    while True:
        user_input = input("请输入提示词(输入 'exit' 退出): ")
        if user_input.lower() == 'exit':
            print("退出程序,谢谢使用!")
            break
        # 调用 Gemini 模型获取回复
        response = ask_gpt(user_input)
        print(f"Gemini 回复: {response}\n")
修改于 2025-07-11 14:29:31
上一页
Vs code+cline
下一页
Python-openai格式-流式
Built with