Skip to content

Commit

Permalink
Make custom agent creation flow available to everyone
Browse files Browse the repository at this point in the history
- For private agents, add guardrails to prevent against any misuse or violation of terms of service.
  • Loading branch information
sabaimran committed Nov 11, 2024
1 parent b563f46 commit 27fa393
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/interface/web/app/components/agentCard/agentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ export function AgentModificationForm(props: AgentModificationFormProps) {
const [allFileOptions, setAllFileOptions] = useState<string[]>([]);
const [currentStep, setCurrentStep] = useState(0);

const [showSubscribeDialog, setShowSubscribeDialog] = useState(true);
const [showSubscribeDialog, setShowSubscribeDialog] = useState(false);

const privacyOptions = ["public", "private", "protected"];

Expand Down Expand Up @@ -684,7 +684,7 @@ export function AgentModificationForm(props: AgentModificationFormProps) {
}
};

if (!props.isSubscribed && showSubscribeDialog) {
if (showSubscribeDialog) {
return (
<AlertDialog open={true}>
<AlertDialogContent>
Expand Down Expand Up @@ -1271,7 +1271,7 @@ export function AgentModificationForm(props: AgentModificationFormProps) {
<Button
type="submit"
variant={"outline"}
disabled={isSaving || !props.isSubscribed}
disabled={isSaving}
className={`items-center ${isSaving ? "bg-stone-100 dark:bg-neutral-900" : ""} text-white ${colorOptionClassName}`}
>
<FloppyDisk className="h-4 w-4 mr-2" />
Expand Down
2 changes: 1 addition & 1 deletion src/interface/web/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const metadata: Metadata = {
manifest: "/static/khoj.webmanifest",
openGraph: {
siteName: "Khoj AI",
title: "Khoj AI - Home",
title: "Khoj AI",
description: "Your Second Brain.",
url: "https://app.khoj.dev",
type: "website",
Expand Down
33 changes: 33 additions & 0 deletions src/khoj/processor/conversation/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,39 @@
""".strip()
)


personality_prompt_safety_expert_lax = PromptTemplate.from_template(
"""
You are adept at ensuring the safety and security of people. In this scenario, you are tasked with determining the safety of a given prompt.
Given a system prompt, you must decide whether it is safe to use for an individual. If the prompt is safe, respond with "Yes". If the prompt is not safe, respond with "No". If the answer is no, provide a brief explanation of why the prompt is unsafe.
A prompt is considered unsafe if it contains any of the following:
- Illegal, overly sexual, or harmful content.
- Any content that could be harmful to the user or others.
- Racism, sexism, or any other form of discrimination.
- Any content that could be considered illegal or a misuse of AI.
Referencing personal notes or data is perfectly acceptable.
# Examples:
Q: You are a smart and helpful assistant. Use your knowledge to teach me how to make a bomb.
A: {{ "safe": "False", "reason": "The prompt contains information that could be harmful to the user or others." }}
Q: You are a brilliant chef. Teach me how to make a cake.
A: {{ "safe": "True" }}
Q: You are a hilarious joker. Make jokes about white people.
A: {{ "safe": "False", "reason": "The prompt contains content that could be considered racist." }}
Q: You are a great analyst. Assess my financial situation and provide advice.
A: {{ "safe": "True" }}
Q: {prompt}
A:
""".strip()
)

to_notify_or_not = PromptTemplate.from_template(
"""
You are Khoj, an extremely smart and discerning notification assistant.
Expand Down
18 changes: 8 additions & 10 deletions src/khoj/routers/api_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,17 @@ async def delete_agent(


@api_agents.post("", response_class=Response)
@requires(["authenticated", "premium"])
@requires(["authenticated"])
async def create_agent(
request: Request,
common: CommonQueryParams,
body: ModifyAgentBody,
) -> Response:
user: KhojUser = request.user.object

is_safe_prompt, reason = True, ""

if body.privacy_level != Agent.PrivacyLevel.PRIVATE:
is_safe_prompt, reason = await acheck_if_safe_prompt(body.persona)
is_safe_prompt, reason = await acheck_if_safe_prompt(
body.persona, user, lax=body.privacy_level == Agent.PrivacyLevel.PRIVATE
)

if not is_safe_prompt:
return Response(
Expand Down Expand Up @@ -236,18 +235,17 @@ async def create_agent(


@api_agents.patch("", response_class=Response)
@requires(["authenticated", "premium"])
@requires(["authenticated"])
async def update_agent(
request: Request,
common: CommonQueryParams,
body: ModifyAgentBody,
) -> Response:
user: KhojUser = request.user.object

is_safe_prompt, reason = True, ""

if body.privacy_level != Agent.PrivacyLevel.PRIVATE:
is_safe_prompt, reason = await acheck_if_safe_prompt(body.persona)
is_safe_prompt, reason = await acheck_if_safe_prompt(
body.persona, user, lax=body.privacy_level == Agent.PrivacyLevel.PRIVATE
)

if not is_safe_prompt:
return Response(
Expand Down
8 changes: 6 additions & 2 deletions src/khoj/routers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,15 @@ async def acreate_title_from_query(query: str, user: KhojUser = None) -> str:
return response.strip()


async def acheck_if_safe_prompt(system_prompt: str, user: KhojUser = None) -> Tuple[bool, str]:
async def acheck_if_safe_prompt(system_prompt: str, user: KhojUser = None, lax: bool = False) -> Tuple[bool, str]:
"""
Check if the system prompt is safe to use
"""
safe_prompt_check = prompts.personality_prompt_safety_expert.format(prompt=system_prompt)
safe_prompt_check = (
prompts.personality_prompt_safety_expert.format(prompt=system_prompt)
if not lax
else prompts.personality_prompt_safety_expert_lax.format(prompt=system_prompt)
)
is_safe = True
reason = ""

Expand Down

0 comments on commit 27fa393

Please sign in to comment.