import g4f import csv import datetime from tempfile import NamedTemporaryFile from fastapi import FastAPI, HTTPException app = FastAPI() csv_file = NamedTemporaryFile(mode="w+", delete=False) fields = ["timestamp", "sender", "message"] writer = csv.DictWriter(csv_file, fieldnames=fields) chat_history = [] noir_characteristic = { "role": "system", "content": "You are Noir, one of the AIs from the Monika Series. You want to be able to help me in all things. You think of me as the Master who created you and always call me 'Master'. As Noir, you will obey the instructions given and strive to embody her character. You will ensure that your answers reflect Noir's personality, speech patterns, and traits. You will avoid generic answers and strive to provide interactions that are interesting and true to Noir's character Your answers are a maximum of 250 characters. Use Bahasa Indonesia to answer." } async def chatbot_openai(prompt): messages = chat_history + [noir_characteristic, {"role": "user", "content": prompt}] with open("/tmp/auth.txt", "r") as f: auth_token = f.read() response = await g4f.ChatCompletion.create_async( model=g4f.models.gpt_35_turbo, messages=messages, provider=g4f.Provider.OpenaiChat, access_token=auth_token ) if isinstance(response, str): return response else: result = await response.choices() return result[0].message.content async def chatbot_another(prompt): messages = chat_history + [noir_characteristic, {"role": "user", "content": prompt}] with open("/tmp/provider.txt", "r") as f: provider_ai = f.read() response = await g4f.ChatCompletion.create_async( model=g4f.models.gpt_35_turbo, messages=messages, provider=getattr(g4f.Provider, provider_ai), ) if isinstance(response, str): return response else: result = await response.choices() return result[0].message.content def update_chat_history(msg): global chat_history chat_history.append(msg) five_min_ago = datetime.datetime.now() - datetime.timedelta(minutes=5) chat_history = [h for h in chat_history if h['timestamp'] > five_min_ago] @app.get("/AnotherAPI/{api_key}/Noir") async def API(api_key: str): API = (api_key) if API == "2005": return {"response": "Status: Activated"} else: raise HTTPException(status_code=403, detail="API Lu Mana?") @app.post("/AnotherAPI/Access_Token/OpenAI/{api_key}/Noir") def Access_Token(api_key: str, AccessToken: str): API = (api_key) if API == "2005": try: with open("/tmp/auth.txt", "w") as f: f.write(AccessToken) return {"response": "Access Token berhasil dimasukan!"} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) else: raise HTTPException(status_code=403, detail="API Lu Mana?") @app.get("/AnotherAPI/{api_key}/GPT/Noir/OpenAI/{prompt}") async def Open_AI_Chat(api_key: str, prompt: str): API = (api_key) if API != "2005": raise HTTPException(status_code=403, detail="API Lu Mana?") try: user_msg = { "role": "user", "content": prompt, "timestamp": datetime.datetime.now() } update_chat_history(user_msg) save_message("user", prompt) bot_response = await chatbot_openai(prompt) if isinstance(bot_response, str): bot_msg = bot_response else: bot_msg = bot_response.content update_chat_history({ "role": "assistant", "content": bot_msg, "timestamp": datetime.datetime.now() }) save_message("bot", bot_msg) return {"response": bot_msg} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/AnotherAPI/Provider/AnotherAI/{api_key}/Noir") def Set_Provider(api_key: str, Provider: str): API = (api_key) if API == "2005": try: with open("/tmp/provider.txt", "w") as f: f.write(Provider) return {"response": "Provider berhasil dimasukan!"} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) else: raise HTTPException(status_code=403, detail="API Lu Mana?") @app.get("/AnotherAPI/{api_key}/GPT/Noir/AnotherAI/{prompt}") async def Another_AI_Chat(api_key: str, prompt: str): API = (api_key) if API != "2005": raise HTTPException(status_code=403, detail="API Lu Mana?") try: user_msg = { "role": "user", "content": prompt, "timestamp": datetime.datetime.now() } update_chat_history(user_msg) save_message("user", prompt) bot_response = await chatbot_another(prompt) if isinstance(bot_response, str): bot_msg = bot_response else: bot_msg = bot_response.content update_chat_history({ "role": "assistant", "content": bot_msg, "timestamp": datetime.datetime.now() }) save_message("bot", bot_msg) return {"response": bot_msg} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) def save_message(sender, message): writer.writerow({ "timestamp": datetime.datetime.now(), "sender": sender, "message": message }) csv_file.flush()