Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyang628 committed May 19, 2024
1 parent dd3929d commit 09bc326
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
8 changes: 5 additions & 3 deletions app/control/post/summariser.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}")
27 changes: 18 additions & 9 deletions app/llm/open_ai.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import logging
from typing import Any
from typing import Any, Optional
from openai import OpenAI
import os

Expand Down Expand Up @@ -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:
Expand Down
54 changes: 21 additions & 33 deletions app/prompts/summariser/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = [
Expand All @@ -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]
}
}
},
Expand Down

0 comments on commit 09bc326

Please sign in to comment.