Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion and chat completion functions #45

Closed
mishalhossin opened this issue Jun 15, 2023 · 2 comments
Closed

Completion and chat completion functions #45

mishalhossin opened this issue Jun 15, 2023 · 2 comments

Comments

@mishalhossin
Copy link
Contributor

mishalhossin commented Jun 15, 2023

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 mishalhossin added the enhancement New feature or request label Jun 15, 2023
@mak448a mak448a changed the title Some functions to help you with completion and chat completion Completion and chat completion functions Jun 18, 2023
@mak448a mak448a added code snippet and removed enhancement New feature or request labels Jun 19, 2023
@mishalhossin
Copy link
Contributor Author

completion dosent work anymore

@mak448a
Copy link
Owner

mak448a commented Jul 11, 2023

Closing since it's broken

@mak448a mak448a closed this as completed Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants