import g4f import asyncio from datetime import datetime, timedelta from typing import List from characterai import PyCAI as AnotherAPI from fastapi import FastAPI, HTTPException, Response, Query, Request from typing import Optional from pydantic import BaseModel app = FastAPI() #GPT---------------------------------------------------------------------------- _providers = [g4f.Provider.FakeGpt] 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 run_provider(provider: g4f.Provider.BaseProvider, messages): try: response = await g4f.ChatCompletion.create_async( model='text-davinci-003', messages=messages, provider=provider, ) return response except Exception as e: return str(e) async def run_all(messages): calls = [ run_provider(provider, messages) for provider in _providers ] responses = await asyncio.gather(*calls) return responses async def clean_chat_history(): while True: current_time = datetime.now() for session_id, history in list(chat_history.items()): if history[-1]['timestamp'] + timedelta(minutes=5) < current_time: del chat_history[session_id] await asyncio.sleep(60) #------------------------------------------------------------------------------- @app.get("/AnotherAPI/{api_key}/GPT/Noir/{prompt}") async def chat(request: Request, api_key: str, prompt: str): try: API = AnotherAPI(api_key) API.chat.new_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs') session_id = request.client.host if session_id not in chat_history: chat_history[session_id] = [noir_characteristic] messages = chat_history[session_id] messages.append({"role": "user", "content": prompt}) responses = await run_all(messages) if responses[0]: asyncio.create_task(clean_chat_history()) messages.append({"role": "assistant", "content": responses[0]}) return {"response": responses[0]} else: asyncio.create_task(clean_chat_history()) raise HTTPException(status_code=500, detail="Provider failed") except Exception as e: raise HTTPException(status_code=401, detail=f"Invalid API key: {str(e)}")