Skip to content

Commit

Permalink
Implementing python processors.
Browse files Browse the repository at this point in the history
  • Loading branch information
yghokim committed Mar 13, 2024
1 parent 325b151 commit 0574ed4
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
indent_size =

insert_final_newline = true
trim_trailing_whitespace = true

Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ testem.log
.DS_Store
Thumbs.db

.nx/cache
.nx/cache

.cache
unittests/
16 changes: 9 additions & 7 deletions apps/backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion apps/backend/tests/test_hello.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
"""Hello unit test module."""

import pytest
from libs.py_core.py_core.hello import hello
from libs.py_core.py_core.system.processor import ChildCardRecommendationGenerator


def test_hello():
"""Test the hello function."""
assert hello() == "Hello aacesstalk-backend"


@pytest.mark.asyncio
async def test_processor():
child_card_recommender = ChildCardRecommendationGenerator()

card_recommendation_result = await child_card_recommender.generate()

print(card_recommendation_result)
31 changes: 16 additions & 15 deletions libs/py_core/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
47 changes: 47 additions & 0 deletions libs/py_core/py_core/system/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from enum import StrEnum
from typing import Literal, TypeAlias
from nanoid import generate

from pydantic import BaseModel, ConfigDict, TypeAdapter, Field


class ChildCardRecommendationResult(BaseModel):
model_config = ConfigDict(frozen=True)

nouns: list[str]
emotions: list[str]
actions: list[str]


class ParentGuidanceElement(BaseModel):
model_config = ConfigDict(frozen=True)

example: str
guidance: str


class ParentRecommendationResult(BaseModel):
model_config = ConfigDict(frozen=True)

recommendations: list[ParentGuidanceElement]


class DialogueRole(StrEnum):
model_config = ConfigDict(frozen=True)

Parent = "parent"
Child = "child"


class DialogueMessage(BaseModel):
model_config = ConfigDict(frozen=True)

id: str = Field(default_factory=lambda: generate(size=20))
speaker: Literal["parent", "child"]
content: str | list[str]
recommendation_id: str


Dialogue: TypeAlias = list[DialogueMessage]

DialogueTypeAdapter = TypeAdapter(Dialogue)
37 changes: 37 additions & 0 deletions libs/py_core/py_core/system/processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import Any

from chatlib.tool.versatile_mapper import ChatCompletionFewShotMapper, ChatCompletionFewShotMapperParams
from chatlib.tool.converter import generate_pydantic_converter
from chatlib.llm.integration.openai_api import GPTChatCompletionAPI, ChatGPTModel

from libs.py_core.py_core.system.model import ChildCardRecommendationResult, Dialogue, DialogueMessage, DialogueRole


class ChildCardRecommendationGenerator:

def __init__(self):
str_output_converter, output_str_converter = generate_pydantic_converter(ChildCardRecommendationResult)

self.__mapper: ChatCompletionFewShotMapper[Dialogue, ChildCardRecommendationResult, Any] = (
ChatCompletionFewShotMapper(GPTChatCompletionAPI(),
instruction_generator=f"""
You are a helpful assistant that serves as an Alternative Augmented Communication tool.
Suppose that you are helping a communication with a child and parents in Korean. The autistic child has the language proficiency of a 6 to 8-year-old in Korean, so recommendations should consider their cognitive level.
Given the last message of the parents, suggest a list of keywords that can help the child pick to create a sentence as an answer.
Proceed in the following order.
1. [nouns] : Provide nouns that reflect detailed context based on your parents' questions.
2. [actions] : Provide words for the action that can be matched with the nouns suggested in [nouns].
3. [emotions] : Suggest emotions that the child might want to express in the situation, including both positive and negative emotions and needs.
Note that the output must be JSON, formatted like the following:
{ChildCardRecommendationResult.schema_json()}
The result should be in Korean and please provide up to four options for each element.
""",
input_str_converter=lambda dialogue, params: dialogue.json(),
output_str_converter=output_str_converter,
str_output_converter=str_output_converter
))

async def generate(self) -> ChildCardRecommendationResult:
return await self.__mapper.run(None, input=[DialogueMessage(speaker=DialogueRole.Parent, content="오늘 학교에서 뭐 했어?")],
params=ChatCompletionFewShotMapperParams(model = ChatGPTModel.GPT_4_0125, api_params={}))

3 changes: 2 additions & 1 deletion libs/py_core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ readme = 'README.md'
[tool.poetry.dependencies]
python = ">=3.11.8,<3.12"
pendulum = "^3.0.0"
chatlib = {git = "https://github.com/yghokim/chatlib.git", tag="0.6.8"}
chatlib = {git = "https://github.com/yghokim/chatlib.git", tag="0.7.0"}
pydantic = "^2.6.4"

[tool.poetry.group.dev.dependencies]
autopep8 = "2.0.2"
Expand Down
15 changes: 15 additions & 0 deletions libs/py_core/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from chatlib.llm.integration.openai_api import GPTChatCompletionAPI

from libs.py_core.py_core.system.processor import ChildCardRecommendationGenerator


async def test_processor():

GPTChatCompletionAPI.authorize()

child_card_recommender = ChildCardRecommendationGenerator()

card_recommendation_result = await child_card_recommender.generate()

print(card_recommendation_result)

3 changes: 3 additions & 0 deletions libs/py_core/tests/test_hello.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Hello unit test module."""

import pytest
from libs.py_core.py_core.hello import hello
from libs.py_core.py_core.system.processor import ChildCardRecommendationGenerator


def test_hello():
"""Test the hello function."""
assert hello() == "Hello py-core"

59 changes: 47 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0574ed4

Please sign in to comment.