Skip to content

Commit

Permalink
refactor: refactor interface code to use __init__ instead of create
Browse files Browse the repository at this point in the history
  • Loading branch information
nsantacruz committed Feb 22, 2024
1 parent 9ee2f9a commit 56c798d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ class TopicPromptInput:
topic: Topic
sources: List[TopicPromptSource]

@staticmethod
def create(serial) -> 'TopicPromptInput':
return TopicPromptInput(**{
**serial,
"topic": Topic(**serial['topic']),
"sources": [TopicPromptSource.deserialize(raw_source) for raw_source in serial['sources']]
})
def __init__(self, lang: str, topic: dict, sources: List[dict]):
self.lang = lang
self.topic = Topic(**topic)
self.sources = [TopicPromptSource(**raw_source) for raw_source in sources]
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class TopicPromptGenerationOutput:
lang: str
prompts: List[TopicPrompt]

@staticmethod
def create(raw_output):
return TopicPromptGenerationOutput(
**{**raw_output, "prompts": [TopicPrompt(**raw_prompt) for raw_prompt in raw_output['prompts']]}
)
def __init__(self, lang: str, prompts: List[dict]):
self.lang = lang
self.prompts = [TopicPrompt(**raw_prompt) for raw_prompt in prompts]
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ class TopicPromptSource:
commentary: Optional[List[TopicPromptCommentary]] = None # list of commentary in a given language
surrounding_text: Optional[Dict[str, str]] = None # section for Tanakh and sugya for Talmud

@staticmethod
def deserialize(serial):
if serial.get('commentary', []):
commentary = [
TopicPromptCommentary(**comment)
for comment in serial.get('commentary', [])
]
else:
commentary = []
return TopicPromptSource(**{
**serial,
"commentary": commentary
})
def __init__(self, ref: str, categories: List[str], book_description: Dict[str, str], book_title: Dict[str, str],
comp_date: str, author_name: str, context_hint: str, text: Dict[str, str],
commentary: List[dict] = None, surrounding_text: Dict[str, str]=None):
self.ref = ref
self.categories = categories
self.book_description = book_description
self.book_title = book_title
self.comp_date = comp_date
self.author_name = author_name
self.context_hint = context_hint
self.text = text
self.commentary = [
TopicPromptCommentary(**comment)
for comment in (commentary or [])
]
self.surrounding_text = surrounding_text
4 changes: 2 additions & 2 deletions app/topic_prompt/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

@shared_task(name='llm.generate_topic_prompts')
def generate_topic_prompts(raw_topic_prompt_input: dict) -> dict:
tp_input = TopicPromptInput.create(raw_topic_prompt_input)
tp_input = TopicPromptInput(**raw_topic_prompt_input)
toprompt_options_list = get_toprompts(tp_input)
# only return the first option for now
toprompts = [TopicPrompt(**options.toprompts[0].serialize()) for options in toprompt_options_list]
toprompts = [options.toprompts[0].serialize() for options in toprompt_options_list]
output = TopicPromptGenerationOutput(lang=tp_input.lang, prompts=toprompts)
return asdict(output)

0 comments on commit 56c798d

Please sign in to comment.