File size: 5,093 Bytes
b4ada1c
6d64b51
 
 
082e4b0
6d64b51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
082e4b0
 
 
 
 
 
 
6d64b51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}