import openai
import requests

openai.api_base = "https://api3.apifans.com/v1"
openai.api_key = "sk-bu6GTEtgqeTb2UQkD95fD3B04d2a48488f1a8b3395Ff667e"



def send_request(prompt, model, temperature):
    try:
    # Create the prompt as a list of messages
        messages = [
        {'role': 'user', 'content': prompt}
    ]
    
        # Make an API call to OpenAI
        response = openai.ChatCompletion.create(
            api_base=openai.api_base,
            api_key=openai.api_key,
            model=model,
            messages=messages,
            temperature=temperature
        )
    
    # Return the response content
        return response.choices[0].message['content']
    except Exception as e:
        print(e)
        return None
    
def send_request_sb(prompt, model, temperature, api_key='sb-f5f2ee94ccbc8e730a706b1e1325e2dc2f5c2eb133be2212'):
    messages = [{"role": "user", "content": prompt}]
    url = "https://api.openai-sb.com/v1/chat/completions"  # 请确认 API 网址是否正确
    print(f"Sending request to {url} with model {model} and temperature {temperature}")
    parameters = {
        "model": model,
        "messages": messages,
        "temperature": temperature,
    }
    headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
    response = requests.post(url, headers=headers, json=parameters)
    if response.status_code == 200:
        data = response.json()
        message_content = data["choices"][0]["message"]['content']
        return message_content
    else:
        print(f"Error: {response.status_code}")
        print(f"Response: {response.text}")
        return "Sorry, something went wrong."