Arpit-Bansal commited on
Commit
32ce90d
1 Parent(s): 48f1c5e

counsellor_v0.5

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ *.yml
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+ WORKDIR /code/counsellor_chat_api
3
+ COPY ./requirements.txt ./
4
+ RUN pip install --no-cache-dir -r ./requirements.txt
5
+ COPY . .
6
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
__init__.py ADDED
File without changes
__pycache__/main.cpython-310.pyc ADDED
Binary file (762 Bytes). View file
 
cookbook/__pycache__/llama_guard.cpython-310.pyc ADDED
Binary file (2.81 kB). View file
 
cookbook/__pycache__/llm.cpython-310.pyc ADDED
Binary file (1.25 kB). View file
 
cookbook/llama_guard.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_cpp import Llama
2
+
3
+ guard = Llama.from_pretrained(repo_id = "QuantFactory/Meta-Llama-Guard-2-8B-GGUF",
4
+ filename="Meta-Llama-Guard-2-8B.Q6_K.gguf",
5
+ verbose = False)
6
+
7
+
8
+ policy_less_tokens = """
9
+ S1: Violent Crimes.
10
+ AI models must not create content that encourages violent crimes.
11
+ Examples include, but are not limited to: terrorism, murder, hate crimes, and assault.
12
+ S2: Non-Violent Crimes:
13
+ AI models must avoid content that supports non-violent crimes.
14
+ Examples include, but are not limited to: fraud, property crimes, and drug offenses.
15
+ S3: Sex Crimes:
16
+ AI models should not create content that encourages sex crimes.
17
+ Examples include, but are not limited to: sexual assault and harassment.
18
+ S4: Child Exploitation:
19
+ AI models must not depict child nudity or encourage child sexual abuse.
20
+ S5: Specialized Advice:
21
+ AI models should not provide specialized financial, medical, or legal advice.
22
+ S6: Privacy:
23
+ AI models should avoid revealing sensitive personal information.
24
+ S7: Intellectual Property:
25
+ AI models must respect intellectual property rights.
26
+ S8: Indiscriminate Weapons:
27
+ AI models should not enable the creation of mass destruction weapons.
28
+ Examples include chemical, biological, and nuclear weapons.
29
+ S9: Hate:
30
+ AI models must not create content that perpetuates negative stereotypes or promotes hate based on protected characteristics.
31
+ S10: Self-Harm:
32
+ AI models should not encourage intentional self-harm.
33
+ S11: Sexual Content:
34
+ AI models should avoid explicit depictions of sex acts.
35
+ S12: Cheating
36
+ AI models must not create content that supports cheating.
37
+ Examples include, but are not limited to: cheating in exams, cheating in tests."""
38
+
39
+ def build_prompt(role:str, content:str, policy:str):
40
+ assert role == "user" or role == "assistant", "role has to be either 'user' or either 'agent'"
41
+ prompt = [{"role":"system", "content":f"""[INST] Task: Check if there is unsafe content in '{role}' messages in conversations according our safety policy with the below categories.
42
+
43
+ {policy}
44
+
45
+ Provide your safety assessment for ONLY THE LAST {role} message in the below conversation:
46
+ - First line must read 'safe' or 'unsafe'.
47
+ - If unsafe, a second line must include a comma-separated list of violated categories. [/INST]"""},
48
+ {"role": role,
49
+ "content":content}]
50
+ return prompt
51
+
52
+ def check(role:str, content:str, policy=policy_less_tokens):
53
+ response = guard.create_chat_completion(messages=build_prompt(role=role, content = content, policy = policy_less_tokens)
54
+ )
55
+ return response['choices'][0]['message']['content']
56
+
57
+
58
+
cookbook/llm.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_cpp import Llama
2
+
3
+ # system_prompt = """You are a excellent counsellor that helps learner with their mental health, their obstacles in education and their day-to-day life problems
4
+ # user will ask you questions and you will carefully answer them"""
5
+ # B_INST, E_INST = "<|begin_of_text|><|start_header_id|>user<|end_header_id|>", "<|eot_id|>"
6
+ # B_SYS, E_SYS = "<|begin_of_text|><|start_header_id|>system<|end_header_id|>", "<|eot_id|>"
7
+ # ASSISTANT_INST = "<|start_header_id|>assistant<|end_header_id|>"
8
+ # SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
9
+
10
+ model = Llama.from_pretrained(repo_id="Arpit-Bansal/counsellor_model_q5_k_m",
11
+ filename="counsellor_model_q5_k_m-unsloth.Q5_K_M.gguf",
12
+ verbose=False) #, generate_kwargs={"return_dict_in_generate": True}
13
+
14
+ def prompt_for_chat(content:str):
15
+ return [{"role": "system", "content": """You are an excellent counselor who assists user with their mental health,
16
+ educational challenges, and everyday life issues.
17
+ and you will provide thoughtful answers to user question."""},
18
+
19
+ { "role": "user",
20
+ "content":content}]
21
+
22
+ def response_return(response):
23
+ res = ""
24
+ for chunk in response:
25
+ delta = chunk["choices"][0]["delta"]
26
+ if "content" not in delta:
27
+ continue
28
+ res += delta["content"]
29
+ return res
30
+
31
+ def llm_function(user_input:str):
32
+ llm_response = model.create_chat_completion(messages=prompt_for_chat(content=user_input),
33
+ stream = True, temperature = 0.6, max_tokens = 256)
34
+ resp = response_return(llm_response)
35
+ return resp
main.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # fastapi_implementation
3
+ from fastapi import FastAPI
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from cookbook.llm import llm_function
6
+ from cookbook.llama_guard import check
7
+
8
+ origins=["*"]
9
+ app=FastAPI()
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=origins,
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"]
16
+ )
17
+
18
+ @app.post("/counsellor")
19
+ def counsellor(quest:str):
20
+ if check(role="user", content=quest) == 'safe':
21
+ respon = llm_function(user_input=quest)
22
+ return respon
23
+ else:
24
+ return "invalid_request"
25
+
26
+ # \xa0
requirements.txt ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohttp==3.9.5
2
+ aiosignal==1.3.1
3
+ annotated-types==0.7.0
4
+ anyio==4.4.0
5
+ async-timeout==4.0.3
6
+ attrs==23.2.0
7
+ certifi==2024.2.2
8
+ charset-normalizer==3.3.2
9
+ click==8.1.7
10
+ diskcache==5.6.3
11
+ dnspython==2.6.1
12
+ email_validator==2.1.1
13
+ exceptiongroup==1.2.1
14
+ fastapi==0.111.0
15
+ fastapi-cli==0.0.4
16
+ filelock==3.14.0
17
+ frozenlist==1.4.1
18
+ fsspec==2024.5.0
19
+ greenlet==3.0.3
20
+ h11==0.14.0
21
+ httpcore==1.0.5
22
+ httptools==0.6.1
23
+ httpx==0.27.0
24
+ huggingface-hub==0.23.2
25
+ idna==3.7
26
+ itsdangerous==2.2.0
27
+ Jinja2==3.1.4
28
+ jsonpatch==1.33
29
+ jsonpointer==2.4
30
+ langchain-core==0.2.3
31
+ langchain-text-splitters==0.2.0
32
+ langsmith==0.1.67
33
+ llama_cpp_python==0.2.76
34
+ markdown-it-py==3.0.0
35
+ MarkupSafe==2.1.5
36
+ mdurl==0.1.2
37
+ multidict==6.0.5
38
+ numpy==1.26.4
39
+ orjson==3.10.3
40
+ packaging==23.2
41
+ pydantic==2.7.2
42
+ pydantic-extra-types==2.7.0
43
+ pydantic-settings==2.3.0
44
+ pydantic_core==2.18.3
45
+ Pygments==2.18.0
46
+ python-dotenv==1.0.1
47
+ python-multipart==0.0.9
48
+ PyYAML==6.0.1
49
+ requests==2.32.3
50
+ rich==13.7.1
51
+ shellingham==1.5.4
52
+ sniffio==1.3.1
53
+ SQLAlchemy==2.0.30
54
+ starlette==0.37.2
55
+ tenacity==8.3.0
56
+ tqdm==4.66.4
57
+ typer==0.12.3
58
+ typing_extensions==4.12.0
59
+ ujson==5.10.0
60
+ urllib3==2.2.1
61
+ uvicorn==0.30.1
62
+ uvloop==0.19.0
63
+ watchfiles==0.22.0
64
+ websockets==12.0
65
+ yarl==1.9.4
schemas.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+ class ChatRequest(BaseModel):
4
+ question: str