Skip to content

Commit

Permalink
Refactor Prompt (OWASP#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r authored Jan 7, 2025
1 parent 07ed2a9 commit 63ef7f1
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 29 deletions.
26 changes: 19 additions & 7 deletions backend/apps/core/models/prompt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Core app prompt model."""

import logging

from django.db import models
from django.template.defaultfilters import slugify

from apps.common.models import TimestampedModel

logger = logging.getLogger(__name__)


class Prompt(TimestampedModel):
"""Prompt model."""
Expand All @@ -27,37 +31,45 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

@staticmethod
def get_text(key):
"""Return prompt by key."""
try:
return Prompt.objects.get(key=key).text
except Prompt.DoesNotExist:
logger.exception("Prompt with key '%s' does not exist.", key)

@staticmethod
def get_github_issue_hint():
"""Return GitHub issue hint prompt."""
return Prompt.objects.get(key="github-issue-hint").text
return Prompt.get_text("github-issue-hint")

@staticmethod
def get_github_issue_documentation_project_summary():
"""Return GitHub issue documentation project summary prompt."""
return Prompt.objects.get(key="github-issue-documentation-project-summary").text
return Prompt.get_text("github-issue-documentation-project-summary")

@staticmethod
def get_github_issue_project_summary():
"""Return GitHub issue project summary prompt."""
return Prompt.objects.get(key="github-issue-project-summary").text
return Prompt.get_text("github-issue-project-summary")

@staticmethod
def get_owasp_chapter_suggested_location():
"""Return OWASP chapter suggested location prompt."""
return Prompt.objects.get(key="owasp-chapter-suggested-location").text
return Prompt.get_text("owasp-chapter-suggested-location")

@staticmethod
def get_owasp_chapter_summary():
"""Return OWASP chapter summary prompt."""
return Prompt.objects.get(key="owasp-chapter-summary").text
return Prompt.get_text("owasp-chapter-summary")

@staticmethod
def get_owasp_committee_summary():
"""Return OWASP committee summary prompt."""
return Prompt.objects.get(key="owasp-committee-summary").text
return Prompt.get_text("owasp-committee-summary")

@staticmethod
def get_owasp_project_summary():
"""Return OWASP project summary prompt."""
return Prompt.objects.get(key="owasp-project-summary").text
return Prompt.get_text("owasp-project-summary")
15 changes: 11 additions & 4 deletions backend/apps/github/models/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,30 @@ def generate_hint(self, open_ai=None, max_tokens=1000):
if self.id and not self.is_indexable:
return

if not (prompt := Prompt.get_github_issue_hint()):
return

open_ai = open_ai or OpenAi()
open_ai.set_input(f"{self.title}\r\n{self.body}")
open_ai.set_max_tokens(max_tokens).set_prompt(Prompt.get_github_issue_hint())
open_ai.set_max_tokens(max_tokens).set_prompt(prompt)
self.hint = open_ai.complete() or ""

def generate_summary(self, open_ai=None, max_tokens=500):
"""Generate issue summary."""
if self.id and not self.is_indexable:
return

open_ai = open_ai or OpenAi()
open_ai.set_input(f"{self.title}\r\n{self.body}")
open_ai.set_max_tokens(max_tokens).set_prompt(
prompt = (
Prompt.get_github_issue_documentation_project_summary()
if self.project.is_documentation_type
else Prompt.get_github_issue_project_summary()
)
if not prompt:
return

open_ai = open_ai or OpenAi()
open_ai.set_input(f"{self.title}\r\n{self.body}")
open_ai.set_max_tokens(max_tokens).set_prompt(prompt)
self.summary = open_ai.complete() or ""

def save(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def handle(self, *args, **options):
print(f"{prefix:<10} {chapter.owasp_url}")

# Summary.
if not chapter.summary:
chapter.generate_summary(prompt=Prompt.get_owasp_chapter_summary())
if not chapter.summary and (prompt := Prompt.get_owasp_chapter_summary()):
chapter.generate_summary(prompt=prompt)

# Suggested location.
if not chapter.suggested_location:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ def handle(self, *args, **options):
print(f"{prefix:<10} {committee.owasp_url}")

# Generate summary
if update_summary:
committee.generate_summary(
prompt=Prompt.get_owasp_committee_summary(), open_ai=open_ai
)
if update_summary and (prompt := Prompt.get_owasp_committee_summary()):
committee.generate_summary(prompt=prompt, open_ai=open_ai)

committees.append(committee)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ def handle(self, *args, **options):
print(f"{prefix:<10} {project.owasp_url}")

# Generate summary
if update_summary:
project.generate_summary(
prompt=Prompt.get_owasp_project_summary(), open_ai=open_ai
)
if update_summary and (prompt := Prompt.get_owasp_project_summary()):
project.generate_summary(prompt=prompt, open_ai=open_ai)

projects.append(project)

Expand Down
7 changes: 4 additions & 3 deletions backend/apps/owasp/models/chapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ def generate_suggested_location(self, open_ai=None, max_tokens=100):
if not self.is_active:
return

if not (prompt := Prompt.get_owasp_chapter_suggested_location()):
return

open_ai = open_ai or OpenAi()
open_ai.set_input(self.get_geo_string())
open_ai.set_max_tokens(max_tokens).set_prompt(
Prompt.get_owasp_chapter_suggested_location()
)
open_ai.set_max_tokens(max_tokens).set_prompt(prompt)
self.suggested_location = open_ai.complete() or ""

def get_geo_string(self, include_name=True):
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/owasp/models/committee.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def from_github(self, repository):

def save(self, *args, **kwargs):
"""Save committee."""
if not self.summary:
self.generate_summary(prompt=Prompt.get_owasp_committee_summary())
if not self.summary and (prompt := Prompt.get_owasp_committee_summary()):
self.generate_summary(prompt=prompt)

super().save(*args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion backend/apps/owasp/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def from_github(self, field_mapping, repository):

def generate_summary(self, prompt, open_ai=None, max_tokens=500):
"""Generate entity summary."""
if not self.is_active:
if not self.is_active or not prompt:
return

open_ai = open_ai or OpenAi()
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/owasp/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def from_github(self, repository):

def save(self, *args, **kwargs):
"""Save project."""
if not self.summary:
self.generate_summary(prompt=Prompt.get_owasp_project_summary())
if not self.summary and (prompt := Prompt.get_owasp_project_summary()):
self.generate_summary(prompt=prompt)

super().save(*args, **kwargs)

Expand Down

0 comments on commit 63ef7f1

Please sign in to comment.