llm_service.cpython-311.pyc•5.8 kB
�
\s�h. � �` � d Z ddlZddlZddlZddlmZmZmZmZ ddl m
Z G d� d� � ZdS )z9
LLM Service for tool recommendation using Google Gemini
� N)�Dict�Optional�Any�Listc
� � e Zd ZdZd
dee fd�Zddedeeef de d e
e fd
�Zdeeef d efd�ZdS )�
LLMServicez/Service for LLM operations using Google Gemini.N�api_keyc �� � |pt j d� � | _ | j st d� � �t j | j �� � t j d� � | _ dS )z�
Initialize the LLM service.
Args:
api_key: Google AI API key. If not provided, will try to get from environment variable GEMINI_API_KEY
�GEMINI_API_KEYzKGEMINI_API_KEY environment variable must be set or api_key must be provided)r zgemini-2.5-flashN)�os�getenvr �
ValueError�genai� configure�GenerativeModel�model)�selfr s �=/Users/gauravsaini/instabase/ibhack-mcp-server/llm_service.py�__init__zLLMService.__init__ sa � � �=�"�)�,<�"=�"=����|� l��j�k�k�k�
����-�-�-�-��*�+=�>�>��
�
�
� � �query_description�available_tools�top_k�returnc �( � |sg S | � |� � }d|� d|� d|� d�} | j � |� � }|j � � � }|� d� � r|dd� � � � }n1|� d� � r|d d� � � � }t
j |� � }|� d
g � � } g }
| d|� D ]0}|� d� � }||v r|
� |� � �1|
S # t j
$ r*}
t d
|
� �t j
�� � g cY d}
~
S d}
~
wt $ r*}
t d|
� �t j
�� � g cY d}
~
S d}
~
ww xY w)a�
Find the most relevant tools for a given description using Gemini.
Args:
query_description: Description of what the user wants to do
available_tools: Dictionary of available tools (ToolInfo objects or dicts)
top_k: Number of top tools to return (default: 2)
Returns:
List of tool names that are most relevant to the query
z�
You are a tool recommendation system. Given a user's request description and a list of available tools,
return the top z. most relevant tools.
User Request: "z$"
Available Tools:
a]
Please analyze the user's request and return the most relevant tools in the following JSON format:
{
"recommendations": [
{
"tool_name": "exact_tool_name_from_list",
"reasoning": "Brief explanation of why this tool is relevant"
},
{
"tool_name": "exact_tool_name_from_list",
"reasoning": "Brief explanation of why this tool is relevant"
}
]
}
Only return the JSON response, no additional text.
z```json� �����z```� �recommendationsN� tool_namez$Error parsing LLM response as JSON: )�filezError calling Gemini API: )�_format_tools_for_llmr �generate_content�text�strip�
startswith�json�loads�get�append�JSONDecodeError�print�sys�stderr� Exception)r r r r �tools_description�prompt�response�
response_text�resultr �
tool_names�recr! �es r �find_relevant_toolszLLMService.find_relevant_tools s� � � � ��I� !�6�6��G�G����� � *� � �
�� � ��4 ��z�2�2�6�:�:�H�$�M�/�/�1�1�M� �'�'� �2�2�
<� -�a��d� 3� 9� 9� ;� ;�
�
��)�)�%�0�0�
<� -�a��d� 3� 9� 9� ;� ;�
��Z�
�.�.�F� %�j�j�):�B�?�?�O��J�&�v��v�.�
1�
1���G�G�K�0�0� ���/� 1��%�%�i�0�0�0������#� � � ��<��<�<�3�:�N�N�N�N��I�I�I�I�I�I������ � � ��2�q�2�2���D�D�D�D��I�I�I�I�I�I����� ���s0 �C>D&