Dragdev commited on
Commit
068c61e
·
verified ·
1 Parent(s): b2869ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -1,29 +1,30 @@
1
- from fastapi import FastAPI, Request
2
  from pydantic import BaseModel
3
- import openai
4
- import os
5
-
6
- openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  app = FastAPI()
9
 
10
- class ChatRequest(BaseModel):
11
  message: str
12
- history: list = []
 
13
 
14
- @app.post("/chat")
15
- async def chat(data: ChatRequest):
16
- messages = [{"role": "system", "content": "You are a helpful assistant."}]
17
- messages += data.history
18
- messages.append({"role": "user", "content": data.message})
19
 
20
- response = openai.ChatCompletion.create(
21
- model="gpt-3.5-turbo",
22
- messages=messages
23
- )
 
 
 
 
 
 
 
 
24
 
25
- reply = response['choices'][0]['message']['content']
26
- return {
27
- "reply": reply,
28
- "history": messages + [{"role": "assistant", "content": reply}]
29
- }
 
1
+ from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+ import re
 
 
 
4
 
5
  app = FastAPI()
6
 
7
+ class CommandInput(BaseModel):
8
  message: str
9
+ user_id: int # the person who sent the command
10
+ chat_id: int # the chat where the command was used
11
 
12
+ @app.post("/command")
13
+ def handle_command(data: CommandInput):
14
+ message = data.message.strip().lower()
15
+ response = "Unknown command."
 
16
 
17
+ if message.startswith("/ban"):
18
+ match = re.search(r"@?(\w+)", message)
19
+ if match:
20
+ target = match.group(1)
21
+ response = f"User @{target} has been banned in chat {data.chat_id}."
22
+ elif message.startswith("/mute"):
23
+ match = re.search(r"@?(\w+)", message)
24
+ if match:
25
+ target = match.group(1)
26
+ response = f"User @{target} has been muted in chat {data.chat_id}."
27
+ elif message.startswith("/help"):
28
+ response = "Available commands: /ban @user, /mute @user, /help"
29
 
30
+ return {"response": response}