-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
194 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,7 @@ testem.log | |
.DS_Store | ||
Thumbs.db | ||
|
||
.nx/cache | ||
.nx/cache | ||
|
||
.cache | ||
unittests/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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={})) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.