Skip to main content
Glama
ai_strategy_sommelier.cpython-311.pyc12.6 kB
� �k�h�'��t�ddlZddlZddlmZmZmZddlZddlZeje ��Z Gd�d��Z dS)�N)�Dict�List�Anyc �V�eZdZddefd�Zddedeeefdeeeffd�Zdedefd�Zdedefd �Z dedeeefd edeeeffd �Z d ededeeeffd �Z ded edefd�Z deeefde efd�Zdeeefde efd�Zdeeeffd�ZdS)�AIStrategySommelierN�openai_api_keyc�|�||_|r |t_dddddd�dd�d d d d d d�dd�dddddd�dd�d�|_dS)N�Low�4-6%�<�� ��stable_lending�liquid_staking� yield_farmingz-Like a savings account that actually pays you)� risk_level� expected_apy� allocation� personality�Mediumz8-12%�(�#�z$Perfect balance of safety and growth�Highz15-25%��2z$High octane fuel for maximum returns�� conservative�balanced� aggressive)r�openai�api_key�strategy_templates)�selfrs �//home/runner/workspace/ai_strategy_sommelier.py�__init__zAIStrategySommelier.__init__ s���,��� � ,�+�F�N� $� &�&(�&(�%'��� O� � �'� '�&(�&(�%'��� F� � �%� (�&(�&(�%'��� F� � �+# �# ������ user_goals�portfolio_data�returnc��� |�|��}|�|��}|jr|�|||��}n|�||��}|�|��|d<|�|��|d<d|d<|S#t$r;}t� d|����|� ��cYd}~Sd}~wwxYw)z*Create custom strategy based on user goals�implementation�risksz 2-4 weeks to full implementation�timelinezStrategy creation failed: N) �_analyze_risk_profile�_determine_strategy_typer�_create_ai_strategy�_create_template_strategy�_generate_implementation_steps�_identify_risks� Exception�logger�error�_fallback_strategy)r%r)r*� risk_profile� strategy_type�strategy�es r&�create_strategyz#AIStrategySommelier.create_strategy2s�� -��5�5�j�A�A�L� �9�9�*�E�E�M��"� U��3�3�J��P\�]�]����9�9�-��T�T��*.�)L�)L�X�)V�)V�H�%� &� $� 4� 4�X� >� >�H�W� �#E�H�Z� ��O��� -� -� -� �L�L�9�a�9�9� :� :� :��*�*�,�,� ,� ,� ,� ,� ,� ,����� -���s�BB� C�"0C�C�Cc����|����gd�}gd�}t�fd�|D����}t�fd�|D����}||krdS||krdSdS)z.Analyze user's risk tolerance from their goals)�safe�stable�securer�scared�losses� retirement�steady)�growthr!�maximum�high�risk�moon�gains�fastc3�$�K�|] }|�v�dV�� dS��N���.0�keyword�user_goals_lowers �r&� <genexpr>z<AIStrategySommelier._analyze_risk_profile.<locals>.<genexpr>Qs.����� g� g�w�7�Vf�Kf�Kf��Kf�Kf�Kf�Kf� g� gr(c3�$�K�|] }|�v�dV�� dSrOrQrRs �r&rVz<AIStrategySommelier._analyze_risk_profile.<locals>.<genexpr>Rs.�����c�c�W�w�Rb�Gb�Gb�q�Gb�Gb�Gb�Gb�c�cr(rr!r )�lower�sum)r%r)�conservative_keywords�aggressive_keywords�conservative_score�aggressive_scorerUs @r&r0z)AIStrategySommelier._analyze_risk_profileJs����%�+�+�-�-�� x� x� x��j�j�j�� � g� g� g� g�2G� g� g� g�g�g���c�c�c�c�0C�c�c�c�c�c�� � 0� 0� 0�!�>� � 2� 2� 2��<��:r(c�,�|�|��S)z'Determine strategy type from user goals)r0)r%r)s r&r1z,AIStrategySommelier._determine_strategy_type[s���)�)�*�5�5�5r(r:c �`� d}|r{d|�dd��d�dt|�dg�����dd �d �|�dg��d d �D�����d �}d|�d|�d|�d�}tj�dd|d�gdd���}|jdjj� ��}tj d|tj ��}|r(tj|�����} | S|j|���} |�||��| d<|| d<| S#t&$r=} t(�d| ����|�||��cYd } ~ Sd } ~ wwxYw)z(Create AI-enhanced strategy using OpenAI�zD Current Portfolio: - Total Value: $�total_value_usdrz,.2fz - Assets: �tokensz) tokens - Main Holdings: z, c�:�g|]}|�dd����S)�symbolr`)�get)rS�ts r&� <listcomp>z;AIStrategySommelier._create_ai_strategy.<locals>.<listcomp>hs&��,o�,o�,o�Q�Q�U�U�8�R�-@�-@�,o�,o�,or(N�z z� As an expert DeFi strategy sommelier, create a custom investment strategy for this user: User Goals: "z" Risk Profile: z a Return a JSON strategy with: 1. strategy_name (creative, wine-themed name) 2. risk_level (Low/Medium/High) 3. expected_apy (realistic range) 4. allocation (percentages for stable_lending, liquid_staking, yield_farming) 5. personality (wine sommelier description with tasting notes) 6. reasoning (why this strategy fits their goals) Make it sound like a wine sommelier describing a fine vintage! z gpt-3.5-turbo�user)�role�contenti�g�������?)�model�messages� max_tokens� temperaturez\{.*\}� strategy_name� reasoningzAI strategy creation failed: )re�len�joinr"�ChatCompletion�create�choices�messagerk�strip�re�search�DOTALL�json�loads�groupr$�copy�_generate_strategy_namer6r7�warningr3) r%r)r*r:�portfolio_context�prompt�response�ai_text� json_matchr<�templater=s r&r2z'AIStrategySommelier._create_ai_strategy_s,��4 L� "� �� �%�!/�!3�!3�4E�q�!I�!I�%�%�%��~�1�1�(�B�?�?�@�@�%�%�#'�)�)�,o�,o�.�J\�J\�]e�gi�Jj�Jj�km�lm�km�Jn�,o�,o�,o�"p�"p� %�%�%�!��%���(� �� � ���F�$�,�3�3�%�#)�f�=�=�>��� 4���H��&�q�)�1�9�?�?�A�A�G���9�g�r�y�A�A�J�� ��:�j�&6�&6�&8�&8�9�9���� �2�<�@�E�E�G�G��,0�,H�,H��Ua�,b�,b���)�(/���%����� L� L� L� �N�N�>�1�>�>� ?� ?� ?��1�1�,� �K�K� K� K� K� K� K� K����� L���s$�D$E&�'>E&�& F-�02F(�"F-�(F-r;c��|j|���}|�||��|d<d|�d|dd��d�|d<|S)zCreate strategy from templaterpzThis z% strategy aligns with your goals of: N�dz...rq)r$rr�)r%r;r)r<s r&r3z-AIStrategySommelier._create_template_strategy�sd���*�=�9�>�>�@�@��$(�$@�$@��]�$[�$[���!� q� � q� q�\f�gk�hk�gk�\l� q� q� q�����r(c��gd�gd�gd�d�}ddl}|�|�||d����S)z Generate creative strategy names)zSteady Sipper ReservezSafe Harbor BlendzConservative VintagezGentle Giant Portfolio)zPerfect Balance BordeauxzHarmony House BlendzGolden Ratio ReservezBalanced Barrel Select)zHigh Octane VintagezMaximum Yield MerlotzAggressive Growth BlendzRocket Fuel ReserverrNr )�random�choicere)r%r)r:�namesr�s r&r�z+AIStrategySommelier._generate_strategy_name�sk����� ��� ��� � ��* � � � ��}�}�U�Y�Y�|�U�:�5F�G�G�H�H�Hr(r<c���|�di��}g}|�dd��dkr|�d|d�d���|�dd��dkr|�d|d�d���|�d d��dkr|�d |d �d ���|�d ��|�d ��|S)z.Generate implementation steps for the strategyrrrz1. Lend z$% in USDC on Aave for stable returnsrz 2. Stake z+% in ETH liquid staking for network rewardsrz3. Farm z#% in high-yield pools (diversified)z.4. Set up auto-compounding to maximize returnsz 5. Monitor and rebalance monthly�re�append)r%r<r�stepss r&r4z2AIStrategySommelier._generate_implementation_steps�s����\�\�,��3�3� ��� �>�>�*�A� .� .�� 2� 2� �L�L�f�J�/?�$@�f�f�f� g� g� g� �>�>�*�A� .� .�� 2� 2� �L�L�n�Z�0@�%A�n�n�n� o� o� o� �>�>�/�1� -� -�� 1� 1� �L�L�d�J��$?�d�d�d� e� e� e� � � �E�F�F�F� � � �7�8�8�8�� r(c���|�dd��}|�di��}g}|dkr*|�d��|�d��|�dd��d kr|�d ��|�d d��d kr|�d ��|�d��|�d��|S)zIdentify strategy risksrrrrz*High volatility in yield farming positionsz&Smart contract risks in DeFi protocolsrrr z#Impermanent loss in liquidity poolsrrz3Limited stable asset buffer during market downturnszGeneral DeFi protocol risksz-Market volatility affecting all crypto assetsr�)r%r<rrr.s r&r5z#AIStrategySommelier._identify_risks�s����\�\�,��9�9� ��\�\�,��3�3� ��� �� � � �L�L�E� F� F� F� �L�L�A� B� B� B� �>�>�/�1� -� -�� 2� 2� �L�L�>� ?� ?� ?� �>�>�*�A� .� .�� 3� 3� �L�L�N� O� O� O� � � �2�3�3�3� � � �D�E�E�E�� r(c �(�ddddddd�dd gd �d gd d � S)z%Fallback strategy when creation failszSafe & Simple Starterr r �FrrrzFA gentle introduction to DeFi, like training wheels for your portfolioz7Default safe strategy while we analyze your preferences)z"1. Start with USDC lending on Aavez!2. Add small ETH staking positionz"3. Gradually explore yield farmingz-Minimal risks with this conservative approachz1-2 weeks to implement) rprrrrrqr-r.r/rQ)r%s r&r9z&AIStrategySommelier._fallback_strategy�sS��5��"�"$�"$�!#��� d�R���� F�F�0�# � � r()N)�__name__� __module__� __qualname__�strr'rrr>r0r1r2r3r�rr4r5r9rQr(r&rr s�������% �% �s�% �% �% �% �N-�-�#�-�t�C��H�~�-�Y]�^a�cf�^f�Yg�-�-�-�-�0��������"6�3�6�3�6�6�6�6�6L�c�6L�4��S��>�6L�ad�6L�im�nq�sv�nv�iw�6L�6L�6L�6L�p�s����PT�UX�Z]�U]�P^�����I�#�I�S�I�S�I�I�I�I�4�t�C��H�~��$�s�)�����&��S�#�X���4��9�����, �D��c��N� � � � � � r(r) �loggingr"�typingrrrr|ry� getLoggerr�r7rrQr(r&�<module>r�s������� � � � �"�"�"�"�"�"�"�"�"�"� � � � � � � � � �� �8� $� $��k �k �k �k �k �k �k �k �k �k r(

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JMadhan1/OneDefi-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server