mak448a / AI-Drawing-Chatbot

An image generator Discord bot written in Python with no GPU requirement! It can be easily deployed to your favorite VPS for hosting.
MIT License
27 stars 5 forks source link

Completion and chat completion functions #45

Closed mishalhossin closed 1 year ago

mishalhossin commented 1 year ago
import aiohttp

base_urls = ['https://a.z-pt.com', 'http://chat.darkflow.top']

async def generate_response(prompt):
    endpoint = 'https://gpt4.gravityengine.cc/api/openai/v1/chat/completions'
    headers = {
        'Content-Type': 'application/json',
    }
    data = {
        'model': 'gpt-3.5-turbo-16k-0613',
        'temperature': 0.7,
        'messages': [
            {"role": "system", "content": "INSTRUCTIONS HEREEEEEEEEEEEE"},
            {"role": "user", "content": prompt},
        ]
    }
    for attempt in range(2):
        try:
            async with aiohttp.ClientSession() as session:
                async with session.post(endpoint, headers=headers, json=data) as response:
                    response_data = await response.json()
                    choices = response_data['choices']
                    if choices:
                        return choices[0]['message']['content']
        except aiohttp.ClientError as error:
            print(f'Error making the request with {endpoint}: {error}')
            if attempt < 1:
                print('Retrying with a different base URL.')
                break

    print('All base URLs failed to provide a response.')
    return None

async def generate_completion(prompt, max_token=None, temp=None):
    endpoint = '/api/openai/v1/engines/text-davinci-003/completions'
    headers = {'Content-Type': 'application/json'}

    async with aiohttp.ClientSession() as session:
        for base_url in base_urls:
            url = base_url + endpoint
            async with session.post(url, headers=headers, json={'prompt': prompt, 'max_tokens': max_token or 2048, 'temperature': temp or 0.7}) as response:
                if response.status != 200:
                    continue
                response_data = await response.json()
                response = response_data['choices'][0]['text']
                return response

    return None
mishalhossin commented 1 year ago

completion dosent work anymore

mak448a commented 1 year ago

Closing since it's broken