Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

make this instead send a post to https://kairo-api.kys.gay/api/gpt with…

  1. User

    PROMPT: user: make this instead send a post to https://kairo-api.kys.gay/api/gpt with payload: 'prompt' 'MESSAGEHERE', 'model': 'gpt-4', 'cvid:': 'CVIDHERE': CVID_FILE = "user_cvids.json" # Function to generate a random string of given length def generate_random_string(length=10): return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length)) # Function to get or create a CVID for a user def get_or_create_cvid(user_id: int): if not os.path.exists(CVID_FILE): cvid_data = {} else: with open(CVID_FILE, 'r') as f: cvid_data = json.load(f) # Check if the CVID for this user exists if str(user_id) in cvid_data: return cvid_data[str(user_id)] # If not, create a new CVID new_cvid = f"DISCORD_{user_id}_{generate_random_string()}" cvid_data[str(user_id)] = new_cvid # Save the CVID to the file with open(CVID_FILE, 'w') as f: json.dump(cvid_data, f) return new_cvid @bot.slash_command(name="chat", description="Chat with AI!") async def chat(interaction: nextcord.Interaction, message: str): user_id = interaction.user.id # Get the user's Discord ID cvid = get_or_create_cvid(user_id) # Get or generate the CVID for the user # Encode the user message to make it URL safe encoded_message = urllib.parse.quote(message) # Construct the URL with the encoded message url = f"https://kairo-api.kys.gay/api/gpt?msg={encoded_message}&cvid={cvid}" async with aiohttp.ClientSession() as session: async with session.get(url) as response: if response.status == 200: data = await response.json() await interaction.response.send_message(data.get('message', "No response from the AI.")) else: await interaction.response.send_message("Failed to get a response from the API.")

  2. Assistant

    To modify your code to send a POST request instead of a GET request to the URL https://kairo-api.kys.gay/api/gpt, you need to adjust the way you're handling your HTTP request. The aiohttp library supports both GET and POST methods easily. Below is an updated version of your slash command to send a POST request:

    import aiohttp
    import json
    import os
    import random
    import string
    import urllib.parse
    import nextcord
    
    CVID_FILE = "user_cvids.json"
    
    # Function to generate a random string of given length
    def generate_random_string(length=10):
        return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
    
    # Function to get or create a CVID for a user
    def get_or_create_cvid(user_id: int):
        if not os.path.exists(CVID_FILE):
            cvid_data = {}
        else:
            with open(CVID_FILE, 'r') as f:
                cvid_data = json.load(f)
    
        # Check if the CVID for this user exists
        if str(user_id) in cvid_data:
            return cvid_data[str(user_id)]
    
        # If not, create a new CVID
        new_cvid = f"DISCORD_{user_id}_{generate_random_string()}"
        cvid_data[str(user_id)] = new_cvid
    
        # Save the CVID to the file
        with open(CVID_FILE, 'w') as f:
            json.dump(cvid_data, f)
    
        return new_cvid
    
    @bot.slash_command(name="chat", description="Chat with AI!")
    async def chat(interaction: nextcord.Interaction, message: str):
        user_id = interaction.user.id  # Get the user's Discord ID
        cvid = get_or_create_cvid(user_id)  # Get or generate the CVID for the user
    
        # Construct the payload for the POST request
        payload = {
            'prompt': message,
            'model': 'gpt-4',
            'cvid': cvid
        }
    
        async with aiohttp.ClientSession() as session:
            async with session.post("https://kairo-api.kys.gay/api/gpt", json=payload) as response:
                if response.status == 200:
                    data = await response.json()
                    await interaction.response.send_message(data.get('message', "No response from the AI."))
                else:
                    await interaction.response.send_message("Failed to get a response from the API.")
    
    

    Key Changes

    1. HTTP Method: Changed from session.get(url) to session.post(url, json=payload), where payload is the data being sent to the server.
    2. Payload: Constructed a JSON payload with the required fields: 'prompt', 'model', and 'cvid'.
    3. URL: Since you're using a POST request, you do not need to encode the message in the URL, so the URL construction with query parameters was removed.

    With these changes, the bot will send a POST request with the user's message, model, and CVID to the specified API endpoint.