Spaces:
Sleeping
Sleeping
File size: 4,104 Bytes
42634bb abde79a 494ae6e fb0ccef 4e46113 c2c41a0 6e79d43 abde79a 4e46113 7314a02 abde79a 7314a02 abde79a 4e46113 78e652d 4e46113 abde79a 78e652d abde79a 6e79d43 abde79a 78e652d 4e46113 abde79a 78e652d abde79a f2bafaf 5ec1fd1 abde79a 2cc34b8 3fe56f3 abde79a 7314a02 fb0ccef 699f895 fb0ccef f2bafaf c85e74f 5ec1fd1 c85e74f 5ec1fd1 1e3c2b7 fb0ccef 5ec1fd1 c85e74f 7314a02 5ec1fd1 fb0ccef 7183d3b f2bafaf 5ec1fd1 78e652d f2bafaf d5fbac3 5ec1fd1 78e652d f2bafaf 5ec1fd1 78e652d f2bafaf 699f895 494ae6e 1e3c2b7 fb0ccef 1e3c2b7 fb0ccef 6390ee2 78e652d |
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 |
import g4f
import asyncio
from datetime import datetime, timedelta
from typing import List
from fastapi import FastAPI, HTTPException, Request
app = FastAPI()
# ---------------- GPT 3.5 TURBO NORMAL ----------------------------------------------------------------------------
_providers = [g4f.Provider.Chatgpt4Online]
chat_history = {}
default_characteristic = {
"role": "system",
"content": "you are AI chat gpt-3.5-turbo who will help me and will always answer with cute kaomoji like this (≧▽≦), always answer me cutely like loli in anime"
}
async def run_provider(messages):
try:
response = await g4f.ChatCompletion.create_async(
model=g4f.models.gpt_35_turbo,
messages=messages,
provider=g4f.Provider.Chatgpt4Online,
)
if isinstance(response, str):
return response
else:
result = await response.choices()
return result[0].message.content
except Exception as e:
return str(e)
async def run_all(messages):
calls = [
run_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 and history.get('last_chat') and history['last_chat'] + timedelta(minutes=10) < current_time:
del chat_history[session_id]
await asyncio.sleep(60)
# Schedule the clean_chat_history() function as a background task
asyncio.create_task(clean_chat_history())
#-------------------------------------------------------------------------------
@app.get("/gpt")
async def chat(request: Request, prompt: str, characteristic: str = None, id: str = None):
try:
if not id:
raise HTTPException(status_code=400, detail="ID parameter is required")
if characteristic and id in chat_history:
messages = chat_history[id]['message']
custom_characteristic = {"role": "system", "content": characteristic}
messages.append(custom_characteristic)
elif characteristic and id not in chat_history:
custom_characteristic = {"role": "system", "content": characteristic}
chat_history[id] = {'last_chat': datetime.now(), 'message': [custom_characteristic]}
if id not in chat_history:
chat_history[id] = {'last_chat': datetime.now(), 'message': [default_characteristic]}
messages = chat_history[id]['message']
messages.append({"role": "user", "content": prompt})
else:
messages = chat_history[id]['message']
messages.append({"role": "user", "content": prompt})
while True:
messages = chat_history[id]['message']
bot_response = await run_provider(messages)
if isinstance(bot_response, str):
response = bot_response
else:
response = bot_response.content
if 'Response 403: {"detail":"Unusual activity' not in response:
chat_history[id]['last_chat'] = datetime.now()
messages.append({"role": "assistant", "content": response})
return {"response": response}
else:
print("Handling 'accessToken' case")
messages = chat_history[id]['message']
responses = await run_provider(messages)
except Exception as e:
raise HTTPException(status_code=401, detail=f"Internal Error: {str(e)}")
@app.get("/gpt/listsessions")
async def list_sessions():
return {"sessions": list(chat_history.keys())}
@app.get("/gpt/historychat")
async def history_chat(id: str):
if id not in chat_history:
raise HTTPException(status_code=404, detail="Session ID not found")
return {"history": chat_history[id]['message']}
@app.get("/key")
async def change_key(id_key: str):
key["token"] = id_key
return {"key": id_key}
|