Spaces:
Sleeping
Sleeping
ivnban27-ctl
commited on
Commit
•
a59350a
1
Parent(s):
5ff529c
open ai update to gpt4
Browse files- models/custom_parsers.py +30 -30
- models/openai/role_models.py +23 -66
- requirements.txt +2 -3
- utils/chain_utils.py +1 -2
models/custom_parsers.py
CHANGED
@@ -20,38 +20,38 @@ class CustomStringOutputParser(BaseOutputParser[List[str]]):
|
|
20 |
text_list = [x.strip() for x in list(chain.from_iterable(text_list))]
|
21 |
return text_list
|
22 |
|
23 |
-
class CustomINSTOutputParser(BaseOutputParser[List[str]]):
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
def parse_whispers(self, text: str) -> str:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
def parse_split(self, text: str) -> str:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
-
def parse(self, text: str) -> str:
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
20 |
text_list = [x.strip() for x in list(chain.from_iterable(text_list))]
|
21 |
return text_list
|
22 |
|
23 |
+
# class CustomINSTOutputParser(BaseOutputParser[List[str]]):
|
24 |
+
# """Parse the output of an LLM call to a list."""
|
25 |
|
26 |
+
# name = "Kit"
|
27 |
+
# name_rx = re.compile(r""+ name + r":|" + name.lower() + r":")
|
28 |
+
# whispers = re.compile((r"([\(]).*?([\)])"))
|
29 |
+
# reactions = re.compile(r"([\*]).*?([\*])")
|
30 |
+
# double_spaces = re.compile(r" ")
|
31 |
+
# quotation_rx = re.compile('"')
|
32 |
|
33 |
+
# @property
|
34 |
+
# def _type(self) -> str:
|
35 |
+
# return "str"
|
36 |
|
37 |
+
# def parse_whispers(self, text: str) -> str:
|
38 |
+
# text = self.name_rx.sub("", text).strip()
|
39 |
+
# text = self.reactions.sub("", text).strip()
|
40 |
+
# text = self.whispers.sub("", text).strip()
|
41 |
+
# text = self.double_spaces.sub(r" ", text).strip()
|
42 |
+
# text = self.quotation_rx.sub("", text).strip()
|
43 |
+
# return text
|
44 |
|
45 |
+
# def parse_split(self, text: str) -> str:
|
46 |
+
# text = text.split("[INST]")[0]
|
47 |
+
# text_list = text.split("[/INST]")
|
48 |
+
# text_list = [x.split("</s>") for x in text_list]
|
49 |
+
# text_list = [x.strip() for x in list(chain.from_iterable(text_list))]
|
50 |
+
# text_list = [x.split("\n\n") for x in text_list]
|
51 |
+
# text_list = [x.strip().rstrip("\n") for x in list(chain.from_iterable(text_list))]
|
52 |
+
# return text_list
|
53 |
|
54 |
+
# def parse(self, text: str) -> str:
|
55 |
+
# text = self.parse_whispers(text)
|
56 |
+
# text_list = self.parse_split(text)
|
57 |
+
# return text_list
|
models/openai/role_models.py
CHANGED
@@ -1,88 +1,45 @@
|
|
1 |
import logging
|
2 |
-
import pandas as pd
|
3 |
from models.custom_parsers import CustomStringOutputParser
|
4 |
-
from utils.app_utils import get_random_name
|
5 |
from langchain.chains import ConversationChain
|
6 |
-
from
|
7 |
from langchain.prompts import PromptTemplate
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
"suicide": "issue_Suicide",
|
13 |
-
"safety_planning": "issue_Suicide",
|
14 |
-
"GCT": "issue_Gral",
|
15 |
-
}
|
16 |
-
|
17 |
-
EN_TEXTER_TEMPLATE_ = """The following is a conversation between you and a crisis counselor.
|
18 |
-
{current_issue}
|
19 |
-
You are able to reply with what the character should say. You are able to reply with your character's dialogue inside and nothing else. Do not write explanations.
|
20 |
-
Do not disclose your name unless asked.
|
21 |
-
Current conversation:
|
22 |
-
{history}
|
23 |
-
helper: {input}
|
24 |
texter:"""
|
25 |
|
26 |
-
|
27 |
-
{
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
texter:"""
|
34 |
-
|
35 |
-
CURRENT_ISSUE_MAPPING = {
|
36 |
-
"issue_Suicide-en": "Your character, {texter_name}, has suicidal thoughts. Your character has a plan to end his life and has all the means and requirements to do so. {seed}",
|
37 |
-
"issue_Anxiety-en": "Your character, {texter_name}, is experiencing anxiety. Your character has suicide thoughts but no plan. {seed}",
|
38 |
-
"issue_Suicide-es": "Tu personaje, {texter_name}, tiene pensamientos suicidas. Tu personaje tiene un plan para terminar con su vida y tiene todos los medios y requerimientos para hacerlo. {seed}",
|
39 |
-
"issue_Anxiety-es": "Tu personaje, {texter_name}, experimenta ansiedad. Tu personaje tiene pensamientos suicidas pero ningun plan. {seed}",
|
40 |
-
"issue_Gral-en": "Your character {texter_name} is experiencing a mental health crisis. {seed}",
|
41 |
-
"issue_Gral-es": "Tu personaje {texter_name} esta experimentando una crisis de salud mental. {seed}",
|
42 |
-
}
|
43 |
-
|
44 |
-
def get_template_role_models(issue: str, language: str, texter_name: str = "", seed="") -> str:
|
45 |
-
"""_summary_
|
46 |
-
|
47 |
-
Args:
|
48 |
-
issue (str): Issue for template, current options are ['issue_Suicide','issue_Anxiety']
|
49 |
-
language (str): Language for the template, current options are ['en','es']
|
50 |
-
texter_name (str): texter to apply to template, defaults to None
|
51 |
-
|
52 |
-
Returns:
|
53 |
-
str: template
|
54 |
-
"""
|
55 |
-
current_issue = CURRENT_ISSUE_MAPPING.get(
|
56 |
-
f"{issue}-{language}", CURRENT_ISSUE_MAPPING[f"issue_Gral-{language}"]
|
57 |
-
)
|
58 |
-
default_name = get_random_name()
|
59 |
-
current_issue = current_issue.format(
|
60 |
-
texter_name=default_name if not texter_name else texter_name,
|
61 |
-
seed = seed
|
62 |
-
)
|
63 |
-
|
64 |
-
if language == "en":
|
65 |
-
template = EN_TEXTER_TEMPLATE_.format(current_issue=current_issue, history="{history}", input="{input}")
|
66 |
-
elif language == "es":
|
67 |
-
template = SP_TEXTER_TEMPLATE_.format(current_issue=current_issue, history="{history}", input="{input}")
|
68 |
|
69 |
-
|
|
|
|
|
70 |
|
71 |
def get_role_chain(template, memory, temperature=0.8):
|
72 |
|
|
|
73 |
PROMPT = PromptTemplate(
|
74 |
input_variables=['history', 'input'],
|
75 |
template=template
|
76 |
)
|
77 |
-
llm =
|
78 |
-
|
79 |
-
|
|
|
80 |
)
|
81 |
llm_chain = ConversationChain(
|
82 |
llm=llm,
|
83 |
prompt=PROMPT,
|
84 |
memory=memory,
|
85 |
-
output_parser=CustomStringOutputParser()
|
|
|
86 |
)
|
87 |
-
logging.debug(f"loaded
|
88 |
return llm_chain, "helper:"
|
|
|
1 |
import logging
|
|
|
2 |
from models.custom_parsers import CustomStringOutputParser
|
|
|
3 |
from langchain.chains import ConversationChain
|
4 |
+
from langchain_openai import ChatOpenAI
|
5 |
from langchain.prompts import PromptTemplate
|
6 |
+
from models.business_logic_utils.input_processing import initialize_conversation
|
7 |
|
8 |
+
OPENAI_TEMPLATE = """{template}
|
9 |
+
{{history}}
|
10 |
+
helper: {{input}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
texter:"""
|
12 |
|
13 |
+
def get_template_role_models(issue: str, language: str, texter_name: str = "") -> str:
|
14 |
+
model_input = {
|
15 |
+
"issue": issue,
|
16 |
+
"language": language,
|
17 |
+
"texter_name": texter_name,
|
18 |
+
"messages": [],
|
19 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Initialize the conversation (adds the system message)
|
22 |
+
model_input = initialize_conversation(model_input, "")
|
23 |
+
return model_input["messages"][0]["content"]
|
24 |
|
25 |
def get_role_chain(template, memory, temperature=0.8):
|
26 |
|
27 |
+
template = OPENAI_TEMPLATE.format(template=template)
|
28 |
PROMPT = PromptTemplate(
|
29 |
input_variables=['history', 'input'],
|
30 |
template=template
|
31 |
)
|
32 |
+
llm = ChatOpenAI(
|
33 |
+
model="gpt-4o",
|
34 |
+
temperature=temperature,
|
35 |
+
max_tokens=256,
|
36 |
)
|
37 |
llm_chain = ConversationChain(
|
38 |
llm=llm,
|
39 |
prompt=PROMPT,
|
40 |
memory=memory,
|
41 |
+
output_parser=CustomStringOutputParser(),
|
42 |
+
verbose=True,
|
43 |
)
|
44 |
+
logging.debug(f"loaded GPT4o model")
|
45 |
return llm_chain, "helper:"
|
requirements.txt
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
scipy==1.11.1
|
2 |
-
|
3 |
-
langchain==0.1.0
|
4 |
pymongo==4.5.0
|
5 |
mlflow==2.9.0
|
6 |
-
langchain-
|
|
|
1 |
scipy==1.11.1
|
2 |
+
langchain==0.3.0
|
|
|
3 |
pymongo==4.5.0
|
4 |
mlflow==2.9.0
|
5 |
+
langchain-openai==0.2.0
|
utils/chain_utils.py
CHANGED
@@ -12,8 +12,7 @@ def get_chain(issue, language, source, memory, temperature, texter_name=""):
|
|
12 |
OA_engine = finetuned_models[f"{issue}-{language}"]
|
13 |
return get_finetuned_chain(OA_engine, memory, temperature)
|
14 |
elif source in ('OA_rolemodel'):
|
15 |
-
|
16 |
-
template = get_template_role_models(issue, language, texter_name=texter_name, seed=seed)
|
17 |
return get_role_chain(template, memory, temperature)
|
18 |
elif source in ('CTL_llama2'):
|
19 |
if language == "English":
|
|
|
12 |
OA_engine = finetuned_models[f"{issue}-{language}"]
|
13 |
return get_finetuned_chain(OA_engine, memory, temperature)
|
14 |
elif source in ('OA_rolemodel'):
|
15 |
+
template = get_template_role_models(issue, language, texter_name=texter_name)
|
|
|
16 |
return get_role_chain(template, memory, temperature)
|
17 |
elif source in ('CTL_llama2'):
|
18 |
if language == "English":
|