1- import json
21from .config import API_KEY , MODEL_NAME
32from .tools import TOOL_EXECUTORS , TOOL_FUNCTIONS
43
54try :
65 import google .generativeai as genai
76except ImportError :
8- print ("Warning: google-generativeai package is not installed. Please `pip install google-generativeai`." )
7+ print (
8+ "Warning: google-generativeai package is not installed. Please `pip install google-generativeai`."
9+ )
10+
911
1012class Agent :
1113 def __init__ (self ):
1214 # Configure Gemini API
1315 genai .configure (api_key = API_KEY )
1416 self .model_name = MODEL_NAME
15-
17+
1618 system_instruction = (
1719 "You are Lambda, a minimal and highly efficient AI coding agent. "
1820 "Your primary goal is to help the user by writing code, executing commands, "
@@ -21,14 +23,14 @@ def __init__(self):
2123 "something that requires these tools, you should use them autonomously. "
2224 "Be concise and professional."
2325 )
24-
26+
2527 # Initialize the generative model with the built tools and system instructions
2628 self .model = genai .GenerativeModel (
2729 model_name = self .model_name ,
2830 system_instruction = system_instruction ,
29- tools = TOOL_FUNCTIONS
31+ tools = TOOL_FUNCTIONS ,
3032 )
31-
33+
3234 # Gemini manages history cleanly through the ChatSession
3335 self .chat_session = self .model .start_chat ()
3436
@@ -44,44 +46,48 @@ def chat(self, user_input: str) -> str:
4446 try :
4547 # 1. Check if the model returned a function_call in any part
4648 tool_calls = [
47- part .function_call for part in response .parts if getattr (part , 'function_call' , None )
49+ part .function_call
50+ for part in response .parts
51+ if getattr (part , "function_call" , None )
4852 ]
49-
53+
5054 # 2. If it did, act on each function call
5155 if tool_calls :
5256 tool_responses = []
53-
57+
5458 for function_call in tool_calls :
5559 function_name = function_call .name
56-
60+
5761 # Convert protobuf args to dict
58- arguments = {key : value for key , value in function_call .args .items ()}
62+ arguments = {
63+ key : value for key , value in function_call .args .items ()
64+ }
5965 print (f"\\ n[Lambda is executing: { function_name } ({ arguments } )]" )
60-
66+
6167 # 3. Execute the tool locally
6268 if function_name in TOOL_EXECUTORS :
6369 function_to_call = TOOL_EXECUTORS [function_name ]
6470 # Call the function dynamically
6571 tool_result = function_to_call (** arguments )
6672 else :
6773 tool_result = f"Error: Tool { function_name } not found."
68-
74+
6975 # Format the result back into Gemini's expected Response format
70- tool_responses .append ({
71- "function_response" : {
72- "name" : function_name ,
73- "response" : {"result" : str (tool_result )}
76+ tool_responses .append (
77+ {
78+ "function_response" : {
79+ "name" : function_name ,
80+ "response" : {"result" : str (tool_result )},
81+ }
7482 }
75- } )
83+ )
7684
77-
78- # 4. Send ALL the tool responses back to the model
85+ # 4. Send ALL the tool responses back to the model
7986 # so it can continue reasoning based on the new information
8087 response = self .chat_session .send_message (tool_responses )
81- continue # Start the loop over to see if it calls more tools
88+ continue # Start the loop over to see if it calls more tools
8289 else :
8390 # No more tool calls; the LLM has generated a final text response.
8491 return response .text
8592 except Exception as e :
8693 return f"An error occurred in the agent loop: { str (e )} "
87-
0 commit comments