Team Ai
Apppublic

Israelbliz/User-Modeling-Agent

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
reflection.cpython-313.pyc152 linesDownload Raw Back to __pycache__
1�

2�/jl7��h�SrSSKJr SSKrSSKJrJr SSKJr SSK	J3r4Jr SSKJ
r
 SSKJr \R "\5r"S	S5\65r"SS\75r\"S
S55rSSjrSSjrSSSjjrSSjrSSSjjrg)uSelf-reflection — the act → critique → revise loop.8 9This is what makes NaijaTaste AI an agent rather than a one-shot pipeline.10After a first-pass output, the agent critiques its own work against the11persona and, if the critique finds problems, revises.12 13Two public entry points:14 15  reflect_on_review(...)   — Task A: critique + refine a generated review16  reflect_on_recommendations(...) — Task B: critique + refine a top-N list17 18Each runs at most `max_iterations` revise cycles (default 2). The loop19stops early once the critique passes (no blocking issues). Every cycle is20logged so the paper can report how often refinement triggered and what it21changed.22 23Reference: Madaan et al. 2023, "Self-Refine: Iterative Refinement with24Self-Feedback"; Shinn et al. 2023, "Reflexion".25�)�annotationsN)�	dataclass�field)�Optional)�	BaseModel�Field)�	LLMClient)�UserPersonac��\rSrSr%Sr\"SS9rS\S'\"SS9rS\S'\"S	S9r	S\S26'\"SS9r27S\S
'\SSj5rSr
g)�ReviewCritique�&z=The critique LLM's assessment of a generated review (Task A).zeTrue if the review text matches the star rating (e.g. a 4-star review doesn't read like a 2-star pan)��description�bool�rating_text_consistentu[True if the review sounds like THIS user — their length, register, vocabulary, and quirks�voice_matchz?True if the review is about the actual item, not generic filler�on_topic�jIf any check failed, a specific 1-2 sentence description of what to fix. If all passed, the string 'none'.�str�issuesc�f�UR=(a UR=(a UR$�N)rrr��selfs �)C:\user-modeling-agent\core\reflection.py�passed�ReviewCritique.passed8s"���*�*�Q�t�/?�/?�Q�D�M�M�Q��N��returnr)�__name__�28__module__�__qualname__�__firstlineno__�__doc__rr�__annotations__rrr�propertyr�__static_attributes__rrrrr&sz��G�#(�L�$��D���7��K����U��H�d���@��F�C��29�R��Rrrc��\rSrSr%Sr\"SS9rS\S'\"SS9rS\S'\"S	S9r	S\S30'\"SS9r31S\S'\"S
S9rS\S'\SSj5r
Srg)�RecommendationCritique�=zFThe critique LLM's assessment of a top-N recommendation list (Task B).zTTrue if the recommended items look like real products, not review-headline fragmentsrr�titles_are_realz4True if the picks genuinely fit the persona's tastes�well_matchedzPTrue if each pick's reasoning cites specific persona signals, not generic filler�reasoning_groundedz.True if the list isn't 10 near-identical items�diverse_enoughrrrc��UR=(a2 UR=(a UR=(a UR$r)r-r.r/r0rs rr�RecommendationCritique.passedRs=���$�$�D��):�):�D��+�+�D�04�0C�0C�	ErrNr )r"r#r$r%r&rr-r'r.r/r0rr(rr)rrrr+r+=s���P�!�4��O�T���J��L�$�� %�)� ����!�D��N�D���@��F�C��32�E��Err+c�Z�\rSrSr%SrSrS\S'\"\S9r	S\S'S	r33S34\S'S	rS35\S'S
rg)�ReflectionTrace�\u@Record of what the reflection loop did — useful for the paper.r�int�iterations_run)�default_factoryz	list[str]�	critiquesFr�passed_final�refinedrN)
r"r#r$r%r&r7r'r�listr9r:r;r)rrrr4r4\s2��J��N�C�� ��6�I�y�6��L�$���G�T�rr4c�:�SR/SPSPSPSPSPUR5PSPSPSPSPSPUPS	PUPSPSPS36PSPSPUPSPUPSPSPS
PSPSPUPSPURSPSPURPSP5nUR	U[37SSS9$)z*One critique pass over a generated review.�u�You are a strict editor checking whether an AI-generated review faithfully imitates a specific user. Be critical — your job is to catch problems, not to be nice.38 39�7=======================================================�40 41THE USER42�43�44 45z46ITEM REVIEWED47z	48Domain: z49Title: z#50THE GENERATED REVIEW (check this)51z	52Rating: u★53Review: �
54YOUR CHECKS55z;561. rating_text_consistent: Does the review TEXT match the z�-star rating? A 4-5 star review should read positive; a 1-2 star review should read negative; a 3 should read mixed.572. voice_match: Does it sound like THIS user? Check their typical review length (z.0fz words avg), tone (a=), and quirks. A terse user given a long essay = fail. A user who writes in all-caps given lowercase = fail.583. on_topic: Is the review about the actual item, or is it generic filler that could apply to anything?59 60If any check fails, describe specifically what to fix in 'issues'. If all pass, set 'issues' to 'none'.�	reasoningz7You are a meticulous editor. Catch every inconsistency.��model�system)�join�to_prompt_block�avg_review_length�tone�61structuredr)�llm�persona�62item_title�item_domain�rating�review�prompts       r�_critique_reviewrTis���63	0�	0�.�	0��*�	0��	0�64�*�	0�65�	0��"�"�$�66%�
	0�&*�
	0��*�	0��	0��*�	0��	0��-�	0� �	0���	0�"�	0��*�	0�.�	0��*�	0��	0��(�	0��	0� �(�!	0� �!	0�"�*�#	0�"�#	0�&�*�'	0�&E�'	0�(FL�H�)	0�(M�)	0�0�,�,�S�1�1	0�02E�1	0�0FM�\�\�N�1	0�0S/�1	0��B�>�>���k�H���rc���"SS[5nSSSSSUR5SUSUS	US67USUS3nURX�S
SS9n	U	RU	R4$)zFRegenerate a review given critique feedback. Returns (rating, review).c�B�\rSrSr%\"SS9rS\S'\"SS9rS\S'S	rg68)�%_refine_review.<locals>.RefinedReview�zStar rating 1.0-5.0r�floatrQz'The improved review in the user's voicerrRrN)	r"r#r$r%rrQr'rRr)rrr�
RefinedReviewrW�s"���*?�@���@��(Q�R���RrrZzxYou previously wrote a review imitating a specific user, but an editor found problems. Rewrite the review to fix them.69 70r?r@rAz	71 72ITEM: [�] z#73 74YOUR PREVIOUS ATTEMPT:75  Rating: u★76  Review: u577 78EDITOR'S FEEDBACK — fix these specific issues:79  z�80 81Rewrite the review addressing the feedback. Keep what worked; fix what the editor flagged. Stay in the user's authentic voice.rDzLYou are an expert behavioral simulator revising your work based on feedback.rE)rrIrLrQrR)82rMrNrOrP�prev_rating�prev_review�critique_issuesrZrS�results83          r�_refine_reviewr`�s���S�	�S�84>��*���*�B��"�"�$�85%�&���b���-� �M�"� �M�"
�
��B�
	C�� �^�^��[�]���F��=�=�&�-�-�'�'rc86��[5nXEp�[U5H�n87[XX#X�5nU88S-UlUR(a?URRS5 SUl[RSU89S-35  OsURRUR5 [RSU90S-S	UR35 [XX#X�UR5up�SUlM� X�U4$![a8n[RS[
U5RS35 SnA MASnAff=f![a8n[RS91[
U5RS35 SnA M�SnAff=f)u7Critique a generated review and refine it if needed.92 93Returns: (final_rating, final_review, trace)94 95The loop:96  1. Critique the current review.97  2. If it passes → stop, return as-is.98  3. If it fails → refine using the critique, then critique again.99  4. Stop after max_iterations even if still imperfect.100zReview critique failed (z); keeping current reviewN�rTz'Review reflection: passed on iteration zReview reflection iter �: issues = zReview refine failed (z); keeping pre-refine review)r4�rangerT�	Exception�log�warning�typer"r7rr9�appendr:�inforr`r;)
rMrNrOrPrQrR�max_iterations�trace�101cur_rating�102cur_review�i�critique�es
             r�reflect_on_reviewrr�sk��
��E�#�103�
�>�
"��	�'��j�(2�@�H� !�1�u����?�?��O�O�"�"�8�,�!%�E���H�H�>�q�1�u�g�F�G��
�����x���/����*�1�q�5�'��X�_�_�<M�N�O�		�%3��j�����&�"�J�!�E�M�3#�>�5�(�(��7�	��K�K�2�4��7�3C�3C�2D�E1�2�
3���	��,�	��K�K�0��a��1A�1A�0B�C4�5�
6���	�s/�
C:�!D?�:104D<�,D7�7D<�?105F�	,E<�<Fc���SRS[U555nSSSSSUR5SSSUSSSUSSS	SS1063nURU[SSS
9$)z-One critique pass over a recommendation list.rAc1073�Z# �UH!upSUS-SUSSUSSUS3v� M# g7f)	z  #rbz [�domainr[�titlez108     Why: rDNr)�.0ro�rs   r�	<genexpr>�,_critique_recommendations.<locals>.<genexpr>�sC����.�D�A��a��c�U�"�Q�x�[�M��A�g�J�<�|�A�k�N�;K�L�.�s�)+ujYou are a strict reviewer checking the quality of a recommendation list. Be critical — catch problems.109 110r?r@rBz111THE RECOMMENDATIONS (mode: z)112rCuI1131. titles_are_real: Do these look like real product titles? FAIL if any are review-headline fragments like 'Fast paced great read' or 'An enjoyable read' or 'Loved it!'.1142. well_matched: Do the picks genuinely fit this user's tastes?1153. reasoning_grounded: Does each 'Why' cite specific persona signals, or is it generic filler?1164. diverse_enough: Is there real variety, or are these 10 near-identical items?117 118If any check fails, describe specifically what to fix in 'issues' (e.g. 'items #4, #7, #9 have review-headline titles — replace them'). If all pass, set 'issues' to 'none'.rDz4You are a meticulous recommendation-quality auditor.rE)rH�	enumeraterIrLr+)rMrN�recommendations�mode�	rec_blockrSs      r�_critique_recommendationsr�s����	�	���o�.���I�1194��*���*�B��"�"�$�120%�T��*�&�&*�V�3��*�B��+�T��*���*�/�	0��4�>�>��&�k�E���rc��[5nUn[U5H�n[XXs5n	US-UlU	R(aAURRS5 SUl[RSUS-35  Xv4$URRU	R5 [RSUS-S	U	R35 U"U	R5nU(aUnSUlM�M� Xv4$![a9n121[RS[
U1225RS35 Sn123A124 Xv4$Sn125A126ff=f![a9n127[RS128[
U1295RS35 Sn130A131 Xv4$Sn132A133ff=f)ulCritique a recommendation list and refine if needed.134 135Unlike review reflection, refinement here can't just rewrite text — it136needs to re-run reranking with feedback. So the caller passes a137`refine_fn(issues: str) -> list[dict]` that re-runs the rerank with the138critique injected, and this function orchestrates the loop.139 140Returns: (final_recommendations, trace)141z Recommendation critique failed (z); keeping current listNrbrTz/Recommendation reflection: passed on iteration zRecommendation reflection iter rczRecommendation refine failed (z); keeping pre-refine list)r4rdrrerfrgrhr"r7rr9rir:rjrr;)rMrNr|r}�	refine_fnrkrl�cur_recsrorprqr;s            r�reflect_on_recommendationsr�s~��
��E��H�
�>�
"��	�0��x�N�H� !�1�u����?�?��O�O�"�"�8�,�!%�E���H�H�F�q�1�u�g�N�O�� �?��	�����x���/����2�1�q�5�'��X�_�_�DU�V�W�	�����0�G��"�� $��
��+#�:�?���5�	��K�K�:�4��7�;K�;K�:L�M/�0�
1��.�?���5	��*�	��K�K�8��a��9I�9I�8J�K2�3�
4���?���	�s/�C=�"E�=142E�,D;�;E�143F�
,F�F)rMr	rNr144rOrrPrrQrYrRrr!r)rMr	rNr145rOrrPrr\rYr]rr^rr!ztuple[float, str])�)rMr	rNr146rOrrPrrQrYrRrrkr6r!z"tuple[float, str, ReflectionTrace])147rMr	rNr148r|�149list[dict]r}rr!r+)rMr	rNr150r|r�r}rrkr6r!z"tuple[list[dict], ReflectionTrace])r&�151__future__r�logging�dataclassesrr�typingr�pydanticrr�core.llmr	�core.personar152�	getLoggerr"rfrr+r4rTr`rrrr�rrr�<module>r�s[���&#��(��%��$�
����!��R�Y�R�.E�Y�E�>�����(�!$�(�36�(�"�(�,/�(�4B�(�V(�"�(�14�(� %�(�47�(�%(�(�->�(�H-.�0)�"%�0)�47�0)�#�0)�-0�0)�'*�0)�3U�0)�n%�/9�%�$'�%�,B�%�V67�.�0:�.�BE�.�03�.�%G�	.r