Skip to main content
Glama

OpenSCAD MCP Server

by jhacksman
parameter_extractor.cpython-312.pyc12.9 kB
� (��g�3��p�ddlZddlZddlmZmZmZmZmZddlZeje �Z Gd�d�Z y)�N)�Dict�Any�Tuple�List�Optionalc ��eZdZdZd�Zdedeeeeefffd�Z dedeeeffd�Z ded eeefde efd �Z dd ede ed e eeefdd fd �Zd ede efd�Zd ededeeeffd�Zdedefd�Zdededeeeffd�Zdededefd�Zded eeefdeeeffd�Zdede efd�Zdededefd�Zdedede efd�Zy )�ParameterExtractorz� Extract parameters from natural language descriptions. Implements dialog flow for collecting specifications and translating them to OpenSCAD parameters. c �n�dddddddddd� |_ddd d d d �|_d dddddddd�|_i|_y)z#Initialize the parameter extractor.g�?g$@g@�@gffffff9@g����� s@) �mm�cm�m�in�inch�inches�ft�foot�feetz!\b(cube|box|square|rectangular)\bz \b(sphere|ball|round|circular)\bz,\b(cylinder|tube|pipe|rod|circular column)\bz)\b(hollow box|container|case|enclosure)\bzM\b(rounded box|rounded container|rounded case|rounded enclosure|smooth box)\b��cube�sphere�cylinder�box� rounded_boxzE(\d+(?:\.\d+)?)\s*(?:mm|cm|m|in|inch|inches)?\s*(?:wide|width|across)zD(\d+(?:\.\d+)?)\s*(?:mm|cm|m|in|inch|inches)?\s*(?:high|height|tall)zC(\d+(?:\.\d+)?)\s*(?:mm|cm|m|in|inch|inches)?\s*(?:deep|depth|long)z:(\d+(?:\.\d+)?)\s*(?:mm|cm|m|in|inch|inches)?\s*(?:radius)z<(\d+(?:\.\d+)?)\s*(?:mm|cm|m|in|inch|inches)?\s*(?:diameter)zC(\d+(?:\.\d+)?)\s*(?:mm|cm|m|in|inch|inches)?\s*(?:thick|thickness)z (\d+)\s*(?:segments|sides|faces)z\b(centered|center)\b)�width�height�depth�radius�diameter� thickness�segments�centerN)�unit_conversions�shape_patterns�parameter_patterns� dialog_state)�selfs �?/home/ubuntu/openscad-mcp-server/src/nlp/parameter_extractor.py�__init__zParameterExtractor.__init__ss����������� ! ���9�9�G�?�k�  ���^�]�[�S�W�_�;�.� # ������� description�returnc��|j|�}|j||�}|j||�}tj d|�d|���||fS)a Extract model type and parameters from a natural language description. Args: description: Natural language description of the 3D object Returns: Tuple of (model_type, parameters) zExtracted model type: z, parameters: )�_determine_shape_type�_extract_shape_parameters�_apply_default_parameters�logger�info)r&r*� model_type� parameterss r'�extract_parametersz%ParameterExtractor.extract_parameters5sa���/�/� �<� ��3�3�K��L� ��3�3�J� �K� �� � �,�Z�L��z�l�S�T��:�%�%r)� modificationsc�,�i}|jj�D]\\}}tj||tj�}|s�.|d}t |t �r|d}|j||�||<�^tjd|���|S)z� Extract parameters from modification description. Args: modifications: Description of modifications to make Returns: Dictionary of parameters to update �����rz#Extracted modification parameters: ) r$�items�re�findall� IGNORECASE� isinstance�tuple�_convert_to_mmr0r1)r&r5r3� param_name�pattern�matches�values r'�%extract_parameters_from_modificationsz8ParameterExtractor.extract_parameters_from_modificationsKs���� �#'�#:�#:�#@�#@�#B� �J���j�j��-����G�G���� ���e�U�+�!�!�H�E�)-�)<�)<�U�M�)R� �:�&�$C� � � �9�*��F�G��r)r2r3c�X�|j|�}|D�cgc] }||vs�|�� c}Scc}w)a Determine which required parameters are missing for a given model type. Args: model_type: Type of model parameters: Currently extracted parameters Returns: List of missing parameter names )�_get_required_parameters)r&r2r3�required_params�params r'�get_missing_parametersz)ParameterExtractor.get_missing_parameterscs3���7�7� �C��#2�N�?�%�e�:�6M��?�N�N��Ns� '�'N�user_idc�d�||jvrdigdd�|j|<|r||j|d<|r!|j|dj|�|j|drE|j|j|d|j|d�}||j|d<yy)z� Update the dialog state for a user. Args: user_id: Unique identifier for the user model_type: Optional model type to update parameters: Optional parameters to update N)r2r3�missing_parameters�current_questionr2r3rK)r%�updaterH)r&rIr2r3�missings r'�update_dialog_statez&ParameterExtractor.update_dialog_stateqs��� �$�+�+� +�"� �&(�$(� *�D� � �g� &� �7A�D� � �g� &�|� 4� � � � �g� &�|� 4� ;� ;�J� G� � � �W� %�l� 3��1�1��!�!�'�*�<�8��!�!�'�*�<�8��G�@G�D� � �g� &�';� <� 4r)c��||jvry|j|}|ds d|d<|dS|dr$|dd}|j||d�}||d<|Sd|d<y)a Get the next question to ask the user based on missing parameters. Args: user_id: Unique identifier for the user Returns: Question string or None if all parameters are collected z0What kind of 3D object would you like to create?r2rLrKrN)r%�_get_parameter_question)r&rI�staterG�questions r'�get_next_questionz$ParameterExtractor.get_next_question�s��� �$�+�+� +�E��!�!�'�*���\�"�(Z�E�$� %��+�,� ,� �%� &��.�/��2�E��3�3�E�5��;N�O�H�(0�E�$� %��O�%)�� �!�r)�answerc�F�||jvr|j|�|j|}|d}|ds%|j|�}|j||��n6|dr1|dd}|j||�}|�|j|||i��|j|S)z� Process a user's answer to a question. Args: user_id: Unique identifier for the user answer: User's answer to the current question Returns: Updated dialog state rLr2)r2rKr)r3)r%rOr-�_extract_parameter_value)r&rIrUrRrLr2rGrBs r'�process_answerz!ParameterExtractor.process_answer�s��� �$�+�+� +� � $� $�W� -��!�!�'�*�� �!3�4���\�"��3�3�F�;�J� � $� $�W�� $� D� �'� (��.�/��2�E��1�1�%��@�E�� ��(�(��e�U�^�(�L�� � ��)�)r)c��|jj�D].\}}tj||tj�s�,|cSy)z.Determine the shape type from the description.r)r#r8r9�searchr;)r&r*�shaper@s r'r-z(ParameterExtractor._determine_shape_type�s=��"�1�1�7�7�9�N�E�7��y�y��+�r�}�}�=�� �:� r)c�:�i}|jj�D]\\}}tj||tj�}|s�.|d}t |t �r|d}|j||�||<�^d|vrd|vr|ddz |d<|d=d|vrd|d<|S)z-Extract parameters for a specific shape type.r7rrr�r!�true)r$r8r9r:r;r<r=r>)r&r*r2r3r?r@rArBs r'r.z,ParameterExtractor._extract_shape_parameters�s���� �$(�#:�#:�#@�#@�#B� �J���j�j��+�r�}�}�E�G���� ���e�U�+�!�!�H�E�)-�)<�)<�U�K�)P� �:�&�$C� �� #�� �(B�#-�j�#9�A�#=�J�x� ��:�&� �z� !�#)�J�x� ��r)� value_str�contextc��� t|�}|jj�D]\}}||j�vs�||zcS|S#t$rt j d|���YywxYw)z@Convert a value to millimeters based on the unit in the context.z"Could not convert value to float: g)�floatr"r8�lower� ValueErrorr0�warning)r&r_r`rB�unit� conversions r'r>z!ParameterExtractor._convert_to_mm�sx�� ��)�$�E�%)�$9�$9�$?�$?�$A� ��j��7�=�=�?�*� �:�-�-�%B� �L��� � �N�N�?� �{�K� L�� �s�=A �A �A � !A.�-A.c ��ddddd�ddd�ddddd�ddd d d �ddd d dd �d�}|j|i�}|j�D]\}}||vs� |||<�|S)z1Apply default parameters based on the model type.� �false)rrrr!� )rr �)rrr!r ��r]�rrrr�)rrrrr r)�getr8)r&r2r3�defaults�model_defaultsrG� default_values r'r/z,ParameterExtractor._apply_default_parameterss���!�2��w�O�!#��4�#%��w�TV�W��"���K�%'�"��a�]_�`�  ��"���j�"�5��%3�$8�$8�$:� �E�=��J�&�$1� �5�!�%;��r)c�H�gd�dgddggd�gd�d�}|j|g�S)z5Get the list of required parameters for a model type.)rrrrrro)rrrrr�rq)r&r2rFs r'rEz+ParameterExtractor._get_required_parameterss6��1��j�!�8�,�<�A�  ���"�"�:�r�2�2r)rGc�v�d|�d�d|�d�d|�d�d|�d�d|�d�d|�d�d �}|j|d |�d |�d��S) z/Get a question to ask for a specific parameter.z What should be the width of the �?z What should be the depth of the z!What should be the height of the z!What should be the radius of the z)What should be the wall thickness of the zHow many segments should the z have for smoothness?)rrrrrr zWhat should be the z of the rv)r&rGr2� questionss r'rQz*ParameterExtractor._get_parameter_question#sy��8� �|�1�E�7� �|�1�E�9�*��Q�G�9�*��Q�G�D�Z�L�PQ�R�7� �|�CX�Y�  � ��}�}�U�&9�%���� �TU�$V�W�Wr)c�F�|jj|�}|sd}tj||tj�}|r,|d}t |t �r|d}|j||�Stjd|�}|r|d}|j||�Sy)z)Extract a parameter value from an answer.z(\d+(?:\.\d+)?)r7rN)r$rqr9r:r;r<r=r>)r&rGrUr@rArBs r'rWz+ParameterExtractor._extract_parameter_value0s����)�)�-�-�e�4���(�G��*�*�W�f�b�m�m�<�� ��B�K�E��%��'��a����&�&�u�f�5� 5��*�*�/��8�� ��B�K�E��&�&�u�f�5� 5�r))NN)�__name__� __module__� __qualname__�__doc__r(�strrrrr4rCrrHrrOrTrXr-r.rbr>r/rErQrW�r)r'r r s���� %�N&�c�&�e�C��c�3�h��<O�6P�&�,�3��4�PS�UX�PX�>��0 O�� O�$�s�C�x�.� O�UY�Z]�U^� O�MQ�BF�G�3�G�H�S�M�G�'/��S�#�X��'?�G�KO�G�@����#���>*�c�*�3�*�4��S��>�*�B������S��c��d�SV�X[�S[�n��2���c��e�� �C��T�#�s�(�^��X\�]`�be�]e�Xf��( 3�3� 3�4��9� 3� X�S� X�c� X�c� X��c��3��8�E�?�r)r ) r9�logging�typingrrrrr�json� getLoggerr{r0r r�r)r'�<module>r�s2�� ��3�3� � �� � �8� $��|�|r)

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/jhacksman/OpenSCAD-MCP-Server'

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