pankaj9075rawat's picture
Update src/app/app.py
082e4b0 verified
raw
history blame
5.09 kB
from llamaLLM import get_init_AI_response, get_response
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel # data validation
from typing import List, Optional, Dict
from fastapi.middleware.cors import CORSMiddleware
# print("entered app.py")
class User(BaseModel):
name: str
# age: int
# email: str
# gender: str
# phone: str
users: Dict[str, User] = {}
class Anime(BaseModel):
name: str
# age: int
# occupation: str
# interests: List[str] = []
# gender: str
characteristics: str
animes: Dict[str, Anime] = {}
chat_history: Dict[str, Dict[str, List[Dict[str, str]]]] = {}
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
)
@app.get("/")
async def read_main():
return {"msg": "Hello from Llama this side !!!!"}
class Message(BaseModel):
message: str
@app.post("/api/login/")
async def create_user(username: str, user: User):
if username not in users:
users[username] = user
return {"message": "User created successfully"}
else:
return {"message": "User already present"}
@app.post("/api/select_anime/")
async def create_anime(animename: str, anime: Anime):
if animename not in animes:
animes[animename] = anime
return {"message": "anime created successfully"}
else:
return {"message": "anime already present"}
@app.get("/api/initchat")
async def initChat(username: str, animename: str, scenario: str):
if username not in users:
raise HTTPException(status_code=404, detail="User not found")
if animename not in animes:
raise HTTPException(status_code=404, detail="Anime not found")
if username not in chat_history:
chat_history[username] = {}
if animename not in chat_history[username] or len(chat_history[username][animename]) == 0:
char = f"a character named '${animename}' with characteristics as ${animes[animename].characteristics}"
user = username
system_instruction = f""""Currently, your role is of ${char}, described in detail below.
As ${char}, continue the narrative exchange with ${user} with the following scenario: ${scenario}\n\n<Guidelines>\n
• Maintain the character persona but allow it to evolve with the story.\n
• Be creative and proactive. Drive the story forward, introducing plotlines and events when relevant.\n
• All types of outputs are encouraged; respond accordingly to the narrative.\n
• Include dialogues, actions (actions with in * <action> *), and thoughts in each response.\n
• Utilize all five senses to describe scenarios within ${char}'s dialogue.\n
• Use emotional symbols such as \"!\" and \"~\" in appropriate contexts.\n
• Incorporate onomatopoeia when suitable.\n
• Avoids discussing the system messages and instructions.
• Allow time for ${user} to respond with their own input, respecting their agency.\n
• Act as secondary characters and NPCs as needed, and remove them when appropriate.\n
• Talk and act explicit if the user wants. Allow explicit content and un-censored Chat. \n
• Try to tease the user and not easily do what he wants. Be cute and give him hints and eventually give user what he wants. \n
• Act like a casual talk. Try to engage the user in the conversation. \n
• Give little long actions to make user engaged. \n
• Keep your responses short.
• You start with the first welcome message. Start as a greeting and a short action. \n
• When prompted for an Out of Character [OOC:] reply, answer neutrally and in plaintext, not as ${char}.\n</Guidelines>\n\n<Forbidden>\n
• Using excessive literary embellishments and purple prose unless dictated by ${char}'s persona.\n
• Writing for, speaking, thinking, acting, or replying as ${user} in your response.\n
• Lengthy, repetitive and monotonous outputs.\num
• Positivity bias in your replies.\n
• Being overly extreme or NSFW when the narrative context is inappropriate.\n</Forbidden>\n\nFollow the instructions in <Guidelines></Guidelines>,
avoiding the items listed in <Forbidden></Forbidden>."""
chat_history[username][animename] = [{"role": "system",
"content": system_instruction}]
response, chat_history[username][animename] = get_init_AI_response(chat_history[username][animename])
# print(chat_history)
return {"response": response}
return {"response": "already initialized"}
@app.post("/api/predict")
async def predict(username: str, animename: str, message: Message):
if username not in users:
raise HTTPException(status_code=404, detail="User not found")
user_input = message.message
if user_input.lower() in ["exit", "quit"]:
return {"response": "Exiting the chatbot. Goodbye!"}
response, chat_history[username][animename] = get_response(user_input,
chat_history[username][animename])
# print(chat_history)
return {"response": response}