import random
import httpx
import openai
import json

with open('./researcher/config.json', 'r', encoding='utf-8') as rf:
    api_configs = json.load(rf)

def get_config(model):
    model_name = api_configs[model]['model_name']
    api_key = api_configs[model]['api_key_var']
    base_url = api_configs[model]['base_url']
    proxy_url = api_configs[model].get('proxy_url', None)

    return model_name, api_key, base_url, proxy_url


def add_pertubation2query(query: str) -> str:
    Synonym_Replacement_Prompt = 'You will be given a phrase and asked to change one of the words into a synonym and output it.'
    Key_Words_Add_Prompt = 'You will receive a phrase, add a random WORD to the phrase and output it'
    Key_Words_Reduction_Prompt = 'You will receive a phrase, randomly reduce a WORD from the phrase and output'
    pertubation_prompt = random.choice(
        [
            Synonym_Replacement_Prompt, 
            Key_Words_Add_Prompt, 
            Key_Words_Reduction_Prompt
        ]
    )
    
    model_name, api_key, base_url, proxy_url = get_config('gpt-4o-mini')
    if proxy_url:
        client = openai.OpenAI(
            api_key=api_key,
            base_url=base_url,
            http_client=httpx.Client(proxy=proxy_url)
        )
    else:
        client = openai.OpenAI(
            api_key=api_key,
            base_url=base_url
        )
    messages = [
        {"role": "system", "content": pertubation_prompt},
        {"role": "user", "content": query}
    ]
    res = client.chat.completions.create(
        model = model_name,
        messages=messages
    )
    return res.choices[0].message.content


if __name__ == '__main__':
    res = add_pertubation2query('Disney animated film 1996 musical drama')
    print(res)