From 09bc326d94a75973657641310de1607304310f1d Mon Sep 17 00:00:00 2001 From: jinyang628 Date: Sun, 19 May 2024 10:53:41 -0700 Subject: [PATCH] Update --- app/control/post/summariser.py | 8 +++-- app/llm/open_ai.py | 27 ++++++++++----- app/prompts/summariser/functions.py | 54 +++++++++++------------------ 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/app/control/post/summariser.py b/app/control/post/summariser.py index c72b99a..0ca52ca 100644 --- a/app/control/post/summariser.py +++ b/app/control/post/summariser.py @@ -1,5 +1,5 @@ import logging -from typing import Any +from typing import Any, Optional from app.exceptions.exception import LogicError from app.prompts.summariser.functions import SummaryFunctions @@ -69,7 +69,9 @@ def _enforce_code_language_presence(key_concepts_lst: list[dict[str, str]]): Args: key_concepts_lst (list[dict[str, str]]): the list of key concepts to be checked. """ - for key_concept in key_concepts_lst: - if key_concept.get(SummaryFunctions.KEY_CONCEPT_CODE_EXAMPLE.value) and not key_concept.get(SummaryFunctions.KEY_CONCEPT_CODE_LANGUAGE.value): + code_example: Optional[dict[str, str]] = key_concept.get(SummaryFunctions.CODE_EXAMPLE.value) + if not code_example: + continue + if code_example.get(SummaryFunctions.CODE.value) and not code_example.get(SummaryFunctions.LANGUAGE.value): raise ValueError(f"Code example present but code language not specified for key concept: {key_concept}") \ No newline at end of file diff --git a/app/llm/open_ai.py b/app/llm/open_ai.py index 27d68bd..7c6c342 100644 --- a/app/llm/open_ai.py +++ b/app/llm/open_ai.py @@ -1,6 +1,6 @@ import json import logging -from typing import Any +from typing import Any, Optional from openai import OpenAI import os @@ -41,19 +41,28 @@ async def send_message(self, system_message: str, user_message: str, config: Pro ) try: json_response: dict[str, str] = json.loads(response.choices[0].message.function_call.arguments) + print("llm_response") + print(json_response) topic: str = json_response[SummaryFunctions.TOPIC] goal: str = json_response[SummaryFunctions.GOAL] overview: str = json_response[SummaryFunctions.OVERVIEW] key_concepts_lst: list = [] for key_concept in json_response[SummaryFunctions.KEY_CONCEPTS]: - key_concepts_lst.append({ - SummaryFunctions.KEY_CONCEPT_HEADER.value: key_concept[SummaryFunctions.KEY_CONCEPT_HEADER], - SummaryFunctions.KEY_CONCEPT_CONTENT.value: key_concept[SummaryFunctions.KEY_CONCEPT_CONTENT], - SummaryFunctions.KEY_CONCEPT_CODE_EXAMPLE.value: key_concept.get(SummaryFunctions.KEY_CONCEPT_CODE_EXAMPLE), - SummaryFunctions.KEY_CONCEPT_CODE_LANGUAGE.value: key_concept.get(SummaryFunctions.KEY_CONCEPT_CODE_LANGUAGE) - }) - - + code_example: Optional[dict[str, str]] = key_concept.get(SummaryFunctions.CODE_EXAMPLE) + if code_example: + key_concepts_lst.append({ + SummaryFunctions.TITLE.value: key_concept[SummaryFunctions.TITLE], + SummaryFunctions.EXPLANATION.value: key_concept[SummaryFunctions.EXPLANATION], + SummaryFunctions.CODE_EXAMPLE.value: { + SummaryFunctions.CODE.value: code_example[SummaryFunctions.CODE], + SummaryFunctions.LANGUAGE.value: code_example[SummaryFunctions.LANGUAGE] + } + }) + else: + key_concepts_lst.append({ + SummaryFunctions.TITLE.value: key_concept[SummaryFunctions.TITLE], + SummaryFunctions.EXPLANATION.value: key_concept[SummaryFunctions.EXPLANATION], + }) log.info(f"Topic: {topic}, Goal: {goal} Overview: {overview}, Key concepts: {key_concepts_lst}") return (topic, goal, overview, key_concepts_lst) except Exception as e: diff --git a/app/prompts/summariser/functions.py b/app/prompts/summariser/functions.py index 3afdb0f..9704449 100644 --- a/app/prompts/summariser/functions.py +++ b/app/prompts/summariser/functions.py @@ -11,10 +11,12 @@ class SummaryFunctions(StrEnum): KEY_CONCEPTS = "key_concepts" # List of tuples containing these 3 elements - KEY_CONCEPT_HEADER = "key_concept_header" - KEY_CONCEPT_CONTENT = "key_concept_content" - KEY_CONCEPT_CODE_EXAMPLE = "key_concept_code_example" - KEY_CONCEPT_CODE_LANGUAGE = "key_concept_code_language" + TITLE = "title" # Compulsory + EXPLANATION = "explanation" # Compulsory + CODE_EXAMPLE = "code_example" # Optional + # CODE_EXAMPLE contains these 2 compulsory elements + CODE = "code" + LANGUAGE = "language" def get_summary_functions() -> list[dict[str, Any]]: summary_functions: list[dict[str, Any]] = [ @@ -41,44 +43,30 @@ def get_summary_functions() -> list[dict[str, Any]]: "items": { "type": "object", "properties": { - SummaryFunctions.KEY_CONCEPT_HEADER: { + SummaryFunctions.TITLE: { "type": "string", "description": "The title of the key concept." }, - SummaryFunctions.KEY_CONCEPT_CONTENT: { + SummaryFunctions.EXPLANATION: { "type": "string", "description": "State the key concept in one or two sentences." }, - SummaryFunctions.KEY_CONCEPT_CODE_EXAMPLE: { - "type": "string", - "description": "A short code example illustrating the key concept if necessary." - }, - SummaryFunctions.KEY_CONCEPT_CODE_LANGUAGE: { - "type": "string", - "description": "The programming language of the code example." - } - }, - "required": [SummaryFunctions.KEY_CONCEPT_HEADER, SummaryFunctions.KEY_CONCEPT_CONTENT], - "allOf": [ - { - "if": { - "properties": { - SummaryFunctions.KEY_CONCEPT_CODE_EXAMPLE: { - "type": "string" - } + SummaryFunctions.CODE_EXAMPLE: { + "type": "object", + "properties": { + SummaryFunctions.CODE: { + "type": "string", + "description": "The code example illustrating the key concept." }, - "required": [SummaryFunctions.KEY_CONCEPT_CODE_EXAMPLE] - }, - "then": { - "required": [SummaryFunctions.KEY_CONCEPT_CODE_LANGUAGE] - }, - "else": { - "not": { - "required": [SummaryFunctions.KEY_CONCEPT_CODE_LANGUAGE] + SummaryFunctions.LANGUAGE: { + "type": "string", + "description": "The programming language of the code example." } - } + }, + "required": [SummaryFunctions.CODE, SummaryFunctions.LANGUAGE], } - ] + }, + "required": [SummaryFunctions.TITLE, SummaryFunctions.EXPLANATION] } } },