Spaces:
Sleeping
Sleeping
Download negotiation_engine.py from re-eul/mulberry-negotiation: direct link, hf CLI and curl.
- Browser
- Download file 11.5 kB
-
https://huggingface.co/spaces/re-eul/mulberry-negotiation/resolve/main/negotiation_engine.py
- Command line
-
hf download hf://spaces/re-eul/mulberry-negotiation/negotiation_engine.py
-
curl -L -o negotiation_engine.py https://huggingface.co/spaces/re-eul/mulberry-negotiation/resolve/main/negotiation_engine.py
11.5 kB
| """ | |
| Mulberry ํ์ ์์ง - ํต์ฌ ๋ก์ง | |
| ์์ด์ ํธ ๊ฐ ๊ฐ๊ฒฉ ํ์ ๋ฐ ๊ณต๋๊ตฌ๋งค ์๋ฎฌ๋ ์ด์ | |
| Based on Malu's business logic + AgentPassport integration | |
| """ | |
| from typing import Dict, List, Tuple | |
| from agent_passport import AgentPassport | |
| import random | |
| class NegotiationEngine: | |
| """์์ด์ ํธ ํ์ ์์ง | |
| AgentPassport ๊ธฐ๋ฐ์ผ๋ก ์์ ์ง๊ณ ์์ด์ ํธ์ ๊ณต๊ธ์ฌ ์์ด์ ํธ ๊ฐ | |
| ๊ฐ๊ฒฉ ํ์์ ์๋ฎฌ๋ ์ด์ ํฉ๋๋ค. | |
| """ | |
| def __init__(self): | |
| self.negotiation_history: List[Dict] = [] | |
| def calculate_base_discount( | |
| self, | |
| current_quantity: int, | |
| target_goal: int, | |
| max_discount: float = 0.30 | |
| ) -> float: | |
| """๊ธฐ๋ณธ ํ ์ธ์จ ๊ณ์ฐ (์๋ ๊ธฐ๋ฐ) | |
| Args: | |
| current_quantity: ํ์ฌ ์ฐธ์ฌ ์ธ์/์๋ | |
| target_goal: ๋ชฉํ ์ธ์/์๋ | |
| max_discount: ์ต๋ ํ ์ธ์จ (๊ธฐ๋ณธ 30%) | |
| Returns: | |
| float: ํ ์ธ์จ (0.0 ~ max_discount) | |
| """ | |
| progress = current_quantity / target_goal | |
| return min(max_discount, progress * max_discount) | |
| def calculate_negotiation_bonus( | |
| self, | |
| demand_agent: AgentPassport, | |
| supply_agent: AgentPassport | |
| ) -> float: | |
| """ํ์ ๋ณด๋์ค ๊ณ์ฐ (์์ด์ ํธ ํ์๋ ฅ ๊ธฐ๋ฐ) | |
| ์์ ์ง๊ณ ์์ด์ ํธ์ ํ์๋ ฅ์ด ๋์์๋ก, | |
| ๊ณต๊ธ์ฌ ์์ด์ ํธ์ ์ ๋ขฐ๋๊ฐ ๋์์๋ก ์ถ๊ฐ ํ ์ธ ๊ฐ๋ฅ | |
| Args: | |
| demand_agent: ์์ ์ง๊ณ ์์ด์ ํธ | |
| supply_agent: ๊ณต๊ธ์ฌ ์์ด์ ํธ | |
| Returns: | |
| float: ์ถ๊ฐ ํ ์ธ์จ (0.0 ~ 0.10) | |
| """ | |
| # ์์ ์์ด์ ํธ ํ์๋ ฅ | |
| demand_power = demand_agent.calculate_negotiation_power() | |
| # ๊ณต๊ธ์ฌ ์์ด์ ํธ ์ ๋ขฐ๋ (Spirit Score) | |
| supply_trust = supply_agent.self_status_info['spirit_score'] / 5.0 | |
| # ํ์ ๋ณด๋์ค (์ต๋ 10% ์ถ๊ฐ) | |
| bonus = (demand_power * 0.6 + supply_trust * 0.4) * 0.10 | |
| return min(0.10, bonus) | |
| def negotiate_price( | |
| self, | |
| base_price: int, | |
| current_quantity: int, | |
| target_goal: int, | |
| demand_agent: AgentPassport, | |
| supply_agent: AgentPassport | |
| ) -> Tuple[int, Dict]: | |
| """๊ฐ๊ฒฉ ํ์ ์คํ | |
| Args: | |
| base_price: ๊ธฐ๋ณธ ๊ฐ๊ฒฉ (์๋งค๊ฐ) | |
| current_quantity: ํ์ฌ ์ฐธ์ฌ ์๋ | |
| target_goal: ๋ชฉํ ์๋ | |
| demand_agent: ์์ ์ง๊ณ ์์ด์ ํธ | |
| supply_agent: ๊ณต๊ธ์ฌ ์์ด์ ํธ | |
| Returns: | |
| Tuple[int, Dict]: (ํ์๋ ๊ฐ๊ฒฉ, ํ์ ์์ธ ์ ๋ณด) | |
| """ | |
| # 1. ๊ธฐ๋ณธ ํ ์ธ ๊ณ์ฐ | |
| base_discount = self.calculate_base_discount(current_quantity, target_goal) | |
| # 2. ํ์ ๋ณด๋์ค ๊ณ์ฐ | |
| negotiation_bonus = self.calculate_negotiation_bonus(demand_agent, supply_agent) | |
| # 3. ์ด ํ ์ธ์จ | |
| total_discount = min(0.40, base_discount + negotiation_bonus) | |
| # 4. ์ต์ข ๊ฐ๊ฒฉ | |
| negotiated_price = int(base_price * (1 - total_discount)) | |
| # 5. ํ์ ์์ธ ์ ๋ณด | |
| details = { | |
| 'base_price': base_price, | |
| 'negotiated_price': negotiated_price, | |
| 'savings': base_price - negotiated_price, | |
| 'savings_per_unit': base_price - negotiated_price, | |
| 'base_discount_rate': round(base_discount * 100, 1), | |
| 'negotiation_bonus_rate': round(negotiation_bonus * 100, 1), | |
| 'total_discount_rate': round(total_discount * 100, 1), | |
| 'demand_agent_power': round(demand_agent.calculate_negotiation_power(), 2), | |
| 'supply_agent_trust': round(supply_agent.self_status_info['spirit_score'], 1), | |
| 'current_quantity': current_quantity, | |
| 'target_goal': target_goal, | |
| 'progress': round((current_quantity / target_goal) * 100, 1) | |
| } | |
| # 6. ํ๋ ๊ธฐ๋ก | |
| demand_agent.record_activity('ํ์', { | |
| 'item': f"ํ์ ์๋ฃ", | |
| 'original_price': base_price, | |
| 'final_price': negotiated_price, | |
| 'discount_rate': total_discount | |
| }) | |
| supply_agent.record_activity('ํ์', { | |
| 'buyer': demand_agent.agent_name, | |
| 'agreed_price': negotiated_price, | |
| 'quantity': current_quantity | |
| }) | |
| # 7. ํ์ ํ์คํ ๋ฆฌ์ ๊ธฐ๋ก | |
| self.negotiation_history.append(details) | |
| return negotiated_price, details | |
| def generate_negotiation_dialogue( | |
| self, | |
| item_name: str, | |
| base_price: int, | |
| current_quantity: int, | |
| target_goal: int, | |
| negotiated_price: int, | |
| demand_agent: AgentPassport, | |
| supply_agent: AgentPassport | |
| ) -> List[Dict]: | |
| """ํ์ ๋ํ ์์ฑ (์๋ฎฌ๋ ์ด์ ) | |
| Args: | |
| item_name: ์ํ๋ช | |
| base_price: ๊ธฐ๋ณธ ๊ฐ๊ฒฉ | |
| current_quantity: ํ์ฌ ์๋ | |
| target_goal: ๋ชฉํ ์๋ | |
| negotiated_price: ํ์๋ ๊ฐ๊ฒฉ | |
| demand_agent: ์์ ์ง๊ณ ์์ด์ ํธ | |
| supply_agent: ๊ณต๊ธ์ฌ ์์ด์ ํธ | |
| Returns: | |
| List[Dict]: ๋ํ ๋ชฉ๋ก | |
| """ | |
| dialogues = [] | |
| # 1. ์์ ์์ด์ ํธ ์ ์ | |
| dialogues.append({ | |
| 'speaker': 'demand', | |
| 'agent_name': demand_agent.agent_name, | |
| 'avatar': '๐ฒ', | |
| 'message': f"์๋ ํ์ธ์. ํ์ฌ {item_name}์ ๋ํด {current_quantity}๊ฐ๊ตฌ๊ฐ ๊ตฌ๋งค ์์ฌ๋ฅผ ๋ณด์์ต๋๋ค. " | |
| f"๋ชฉํ๋ {target_goal}๊ฐ๊ตฌ์ ๋๋ค. ๋๋ ๊ตฌ๋งค ์กฐ๊ฑด์ผ๋ก ๋จ๊ฐ ์กฐ์ ์์ฒญ๋๋ฆฝ๋๋ค." | |
| }) | |
| # 2. ๊ณต๊ธ์ฌ ์์ด์ ํธ ์๋ต | |
| initial_offer = int(base_price * 0.92) # 8% ํ ์ธ ์ ์ | |
| dialogues.append({ | |
| 'speaker': 'supply', | |
| 'agent_name': supply_agent.agent_name, | |
| 'avatar': '๐ช', | |
| 'message': f"๋ฐ๊ฐ์ต๋๋ค. ํ์ฌ ์๋งค๊ฐ๋ {base_price:,}์์ ๋๋ค. " | |
| f"{current_quantity}๊ฐ๊ตฌ๋ผ๋ฉด {initial_offer:,}์๊น์ง ๊ฐ๋ฅํฉ๋๋ค." | |
| }) | |
| # 3. ์์ ์์ด์ ํธ ์ฌํ์ | |
| counter_offer = int((negotiated_price + initial_offer) / 2) | |
| dialogues.append({ | |
| 'speaker': 'demand', | |
| 'agent_name': demand_agent.agent_name, | |
| 'avatar': '๐ฒ', | |
| 'message': f"๊ฐ์ฌํฉ๋๋ค. ์ ํฌ๋ ์ ๊ธฐ ๋ฐฐ์ก(๊ตฌ๋ ) ๋ชจ๋ธ๋ ๊ณ ๋ ค ์ค์ ๋๋ค. " | |
| f"ํ์ ๋ฌผ๋์ {target_goal}๊ฐ๊ตฌ๋ก ๋๋ฆด ํ ๋ {counter_offer:,}์์ ๋ง์ถฐ์ฃผ์ค ์ ์์๊น์?" | |
| }) | |
| # 4. ๊ณต๊ธ์ฌ ์์ด์ ํธ ํํ | |
| dialogues.append({ | |
| 'speaker': 'supply', | |
| 'agent_name': supply_agent.agent_name, | |
| 'avatar': '๐ช', | |
| 'message': f"๊ฒํ ํ์ต๋๋ค. {demand_agent.self_status_info['trust_level']} ๋ฑ๊ธ ์์ด์ ํธ์๋ " | |
| f"์ ๋ขฐํ๊ฒ ์ต๋๋ค. '{item_name} ์์ ํน๊ฐ'๋ก {negotiated_price:,}์์ ํ๊ฒฐํ๊ฒ ์ต๋๋ค." | |
| }) | |
| # 5. ์ต์ข ํ๊ฒฐ | |
| savings = base_price - negotiated_price | |
| savings_rate = (savings / base_price) * 100 | |
| dialogues.append({ | |
| 'speaker': 'system', | |
| 'agent_name': 'Mulberry System', | |
| 'avatar': '๐พ', | |
| 'message': f"๐ ํ์ ํ๊ฒฐ! ์ต์ข ๊ฐ: {negotiated_price:,}์ " | |
| f"(๊ฐ๋น {savings:,}์ ์ ๊ฐ, {savings_rate:.1f}% ํ ์ธ)" | |
| }) | |
| return dialogues | |
| def calculate_economic_impact( | |
| self, | |
| items: Dict[str, Dict], | |
| agents: Dict[str, AgentPassport] | |
| ) -> Dict: | |
| """๊ฒฝ์ ์ ์ํฉํธ ๊ณ์ฐ | |
| Args: | |
| items: ๊ณต๋๊ตฌ๋งค ์์ดํ ๋ชฉ๋ก | |
| agents: ์์ด์ ํธ ๋ชฉ๋ก | |
| Returns: | |
| Dict: ๊ฒฝ์ ์ ์ํฉํธ ์์ฝ | |
| """ | |
| total_savings = 0 | |
| total_original = 0 | |
| total_transactions = 0 | |
| for item_name, item_info in items.items(): | |
| if item_info['current'] > 0: | |
| # ์๋ ๊ฐ๊ฒฉ | |
| original_total = item_info['base_price'] * item_info['current'] | |
| total_original += original_total | |
| # ํ์๋ ๊ฐ๊ฒฉ | |
| negotiated_price = item_info.get('negotiated_price', item_info['base_price']) | |
| negotiated_total = negotiated_price * item_info['current'] | |
| # ์ ๊ฐ์ก | |
| savings = original_total - negotiated_total | |
| total_savings += savings | |
| # ๊ฑฐ๋ ๊ฑด์ | |
| total_transactions += item_info['current'] | |
| # Mulberry ์ด์ ์์ต (CPO 3%) | |
| mulberry_revenue = int(total_savings * 0.03) | |
| # AP2 ๊ฑฐ๋ ์์๋ฃ (0.5%) | |
| ap2_fee = int(total_original * 0.005) | |
| return { | |
| 'total_original': total_original, | |
| 'total_negotiated': total_original - total_savings, | |
| 'total_savings': total_savings, | |
| 'savings_rate': round((total_savings / total_original * 100) if total_original > 0 else 0, 1), | |
| 'mulberry_revenue': mulberry_revenue, | |
| 'ap2_fee': ap2_fee, | |
| 'net_community_benefit': total_savings - mulberry_revenue - ap2_fee, | |
| 'total_transactions': total_transactions, | |
| 'avg_savings_per_transaction': int(total_savings / total_transactions) if total_transactions > 0 else 0 | |
| } | |
| # ํ ์คํธ ์ฝ๋ | |
| if __name__ == '__main__': | |
| print("๐พ Mulberry ํ์ ์์ง ํ ์คํธ\n") | |
| # ์์ด์ ํธ ์์ฑ | |
| demand_agent = AgentPassport( | |
| agent_name="์ํ๋ฉด ์์์ง๊ณ ์์ด์ ํธ", | |
| initial_region="์ธ์ ๊ตฐ", | |
| task_type="๊ณต๋๊ตฌ๋งค" | |
| ) | |
| demand_agent.update_rapport(0.7) | |
| demand_agent.record_activity('ํ์', {'history': '๊ณผ๊ฑฐ 10๊ฑด ์ฑ๊ณต'}) | |
| supply_agent = AgentPassport( | |
| agent_name="์ธ์ ๋ํ ๊ณต๊ธ ์์ด์ ํธ", | |
| initial_region="์ธ์ ๊ตฐ", | |
| task_type="๋์ฐ๋ฌผ ๊ณต๊ธ" | |
| ) | |
| supply_agent.self_status_info['spirit_score'] = 4.2 | |
| # ํ์ ์์ง | |
| engine = NegotiationEngine() | |
| # ํ์ ์คํ | |
| negotiated_price, details = engine.negotiate_price( | |
| base_price=35000, | |
| current_quantity=14, | |
| target_goal=20, | |
| demand_agent=demand_agent, | |
| supply_agent=supply_agent | |
| ) | |
| print(f"1. ํ์ ๊ฒฐ๊ณผ:") | |
| print(f" ์๊ฐ: {details['base_price']:,}์") | |
| print(f" ํ์๊ฐ: {negotiated_price:,}์") | |
| print(f" ์ ๊ฐ: {details['savings']:,}์ ({details['total_discount_rate']}%)") | |
| print(f" ์งํ๋ฅ : {details['progress']}%\n") | |
| print(f"2. ํ์ ๋ํ:") | |
| dialogues = engine.generate_negotiation_dialogue( | |
| "์ธ์ ์ฌ๊ณผ 5kg", | |
| 35000, | |
| 14, | |
| 20, | |
| negotiated_price, | |
| demand_agent, | |
| supply_agent | |
| ) | |
| for i, dialogue in enumerate(dialogues, 1): | |
| print(f" [{i}] {dialogue['avatar']} {dialogue['agent_name']}:") | |
| print(f" {dialogue['message']}\n") | |