File size: 5,566 Bytes
87c96d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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()