Skip to content

Commit

Permalink
censorship message
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Nov 19, 2023
1 parent 5081f42 commit db54b30
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 13 deletions.
20 changes: 17 additions & 3 deletions app/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self):
self.projects = []
self.llmCache = {}
self.embeddingCache = {}
self.defaultCensorship = "This question is outside of my scope. Please ask another question."

self.text_splitter = RecursiveCharacterTextSplitter(
separators=[" "], chunk_size=1024, chunk_overlap=30)
Expand Down Expand Up @@ -69,7 +70,10 @@ def createProject(self, projectModel, db):
projectModel.name,
projectModel.embeddings,
projectModel.llm,
projectModel.system)
projectModel.system,
projectModel.sandboxed,
projectModel.censorship
)
project = Project()
project.boot(projectModel)
self.initializeEmbeddings(project)
Expand Down Expand Up @@ -102,6 +106,10 @@ def editProject(self, name, projectModel: ProjectModelUpdate, db):
if proj_db.system != projectModel.system:
proj_db.system = projectModel.system
changed = True

if proj_db.censorship != projectModel.censorship:
proj_db.censorship = projectModel.censorship
changed = True

if changed:
dbc.update_project(db)
Expand Down Expand Up @@ -136,7 +144,7 @@ def question(self, project, questionModel):
output = qa(questionModel.question)

if project.model.sandboxed and len(output["source_documents"]) == 0:
return "This question is outside of my scope. Please ask another question.", []
return project.model.censorship or self.defaultCensorship, []

return output["result"].strip(), output["source_documents"]

Expand All @@ -157,6 +165,12 @@ def chat(self, project, chatModel):
result = conversationalChain(
{"question": chatModel.message, "chat_history": chat.history}
)

if project.model.sandboxed and len(result["source_documents"]) == 0:
chat.history.append(
(project.model.censorship or self.defaultCensorship))
return project.model.censorship or self.defaultCensorship, []

chat.history.append((chatModel.message, result["answer"]))
return chat, result

Expand Down Expand Up @@ -208,7 +222,7 @@ def questionContext(self, project, questionModel):

if len(docs) == 0:
if project.model.sandboxed:
return "This question is outside of my scope. Please ask another question.", []
return project.model.censorship or self.defaultCensorship, []
else:
inputs = [{"context": "",
"question": questionModel.question}]
Expand Down
4 changes: 2 additions & 2 deletions app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def get_project_by_name(self, db, name):
ProjectDatabase.name == name).first()
return project

def create_project(self, db, name, embeddings, llm, system):
def create_project(self, db, name, embeddings, llm, system, sandboxed, censorship):
db_project = ProjectDatabase(
name=name, embeddings=embeddings, llm=llm, system=system)
name=name, embeddings=embeddings, llm=llm, system=system, sandboxed=sandboxed, censorship=censorship)
db.add(db_project)
db.commit()
db.refresh(db_project)
Expand Down
1 change: 1 addition & 0 deletions app/databasemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ class ProjectDatabase(Base):
llm = Column(String)
system = Column(String)
sandboxed = Column(Boolean, default=False)
censorship = Column(String)
5 changes: 3 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
app = FastAPI(
title="RestAI",
description="Modular REST API bootstrap on top of LangChain. Create embeddings associated with a project tenant and interact using a LLM. RAG as a service.",
version="3.1.1",
version="3.1.2",
contact={
"name": "Pedro Dias",
"url": "https://github.com/apocas/restai",
Expand Down Expand Up @@ -227,7 +227,8 @@ async def get_project(projectName: str, user: User = Depends(get_current_usernam
embeddings=project.model.embeddings,
llm=project.model.llm,
system=project.model.system,
sandboxed=project.model.sandboxed)
sandboxed=project.model.sandboxed,
censorship=project.model.censorship)
output.documents = len(dbInfo["documents"])
output.metadatas = len(dbInfo["metadatas"])

Expand Down
2 changes: 2 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ProjectModel(BaseModel):
llm: Union[str, None] = None
system: Union[str, None] = None
sandboxed: Union[bool, None] = None
censorship: Union[str, None] = None
model_config = ConfigDict(from_attributes=True)


Expand Down Expand Up @@ -73,6 +74,7 @@ class ProjectModelUpdate(BaseModel):
llm: Union[str, None] = None
system: Union[str, None] = None
sandboxed: Union[bool, None] = None
censorship: Union[str, None] = None


class HardwareInfo(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions frontend/src/components/pages/Projects/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function Edit() {
const [info, setInfo] = useState({ "version": "", "embeddings": [], "llms": [], "loaders": [] });
const [error, setError] = useState([]);
const systemForm = useRef(null)
const censorshipForm = useRef(null)
const llmForm = useRef(null)
const sandboxedForm = useRef(null)
var { projectName } = useParams();
Expand Down Expand Up @@ -48,7 +49,8 @@ function Edit() {
"name": projectName,
"llm": llmForm.current.value,
"system": systemForm.current.value,
"sandboxed": sandboxedForm.current.checked
"sandboxed": sandboxedForm.current.checked,
"censorship": censorshipForm.current.value,
}),
})
.then(response => response.json())
Expand Down Expand Up @@ -97,8 +99,11 @@ function Edit() {
<Form.Label>System Message</Form.Label>
<Form.Control rows="2" as="textarea" ref={systemForm} defaultValue={data.system ? data.system : ""} />
</Form.Group>
<Form.Group as={Col} controlId="formGridAdmin">
<Form.Check ref={sandboxedForm} type="checkbox" label="Sandboxed" />
</Row>
<Row className="mb-3">
<Form.Group as={Col} controlId="formGridCensorship">
<Form.Check ref={sandboxedForm} type="checkbox" label="Sandboxed" /> <Form.Label>Censorship Message</Form.Label>
<Form.Control rows="2" as="textarea" ref={censorshipForm} defaultValue={data.censorship ? data.censorship : ""} />
</Form.Group>
</Row>
<Button variant="dark" type="submit" className="mb-2">
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/pages/Projects/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ function Project() {
<ListGroup.Item>Documents: {data.documents}</ListGroup.Item>
<ListGroup.Item>Metadatas: {data.metadatas}</ListGroup.Item>
<ListGroup.Item>System: {data.system}</ListGroup.Item>
<ListGroup.Item>Sandboxed: {data.sandboxed ? (<span></span>) : (<span></span>)}</ListGroup.Item>
<ListGroup.Item>Censorship: {data.censorship}</ListGroup.Item>
</ListGroup>
</Col>
<Col sm={6}>
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/components/pages/Projects/Projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Projects() {
const projectNameForm = useRef(null)
const sandboxedForm = useRef(null)
const systemForm = useRef(null)
const censorshipForm = useRef(null)
const embbeddingForm = useRef(null)
const llmForm = useRef(null)
const { getBasicAuth } = useContext(AuthContext);
Expand Down Expand Up @@ -81,7 +82,8 @@ function Projects() {
"embeddings": embbeddingForm.current.value,
"llm": llmForm.current.value,
"system": systemForm.current.value,
"sandboxed": sandboxedForm.current.checked
"sandboxed": sandboxedForm.current.checked,
"censorship": censorshipForm.current.value
}),
})
.then(response => response.json())
Expand Down Expand Up @@ -220,8 +222,10 @@ function Projects() {
</Form.Group>
</Row>
<Row className="mb-3">
<Form.Group as={Col} controlId="formGridSandboxed">
<Form.Group as={Col} controlId="formGridCensorship">
<Form.Check ref={sandboxedForm} type="checkbox" label="Sandboxed" />
<Form.Label>Censorship Message</Form.Label>
<Form.Control ref={censorshipForm} rows="2" as="textarea" />
</Form.Group>
</Row>
<Button variant="dark" type="submit" className="mb-2">
Expand Down

0 comments on commit db54b30

Please sign in to comment.