1+ import sys
12from typing import Dict , Type
3+ from langchain .tools import BaseTool
24import json
5+ import subprocess
36
47from init .env_variables import GOOGLE_FUNCTION_LOCATION
5- from langchain_core .pydantic_v1 import create_model
8+ from langchain_core .pydantic_v1 import create_model , Field
69from .global_tools import GlobalBaseTool
710from models .mongo import Tool
811
912
1013import requests
14+ import os
1115import google .oauth2 .id_token
1216import google .auth .transport .requests
1317from google .cloud import logging_v2
1418from google .api_core .exceptions import GoogleAPIError
1519
16-
1720class GoogleCloudFunctionTool (GlobalBaseTool ):
1821 """
1922 Google Cloud Function execution tool
@@ -23,7 +26,6 @@ class GoogleCloudFunctionTool(GlobalBaseTool):
2326 properties_dict (dict): dictionary of tool.data.parameters.properties { property_name: { type: string | number | boolean, ... } }
2427 this dict is used to create a dynamic pydantic model for "args_schema"
2528 """
26-
2729 name : str = ""
2830 description : str = ""
2931 function_name : str
@@ -32,10 +34,7 @@ class GoogleCloudFunctionTool(GlobalBaseTool):
3234 function_id : str = None
3335
3436 def post_init (self ):
35- self .args_schema = create_model (
36- f"{ self .function_name } _model" ,
37- ** self .convert_args_dict_to_type (self .properties_dict ),
38- )
37+ self .args_schema = create_model (f"{ self .function_name } _model" , ** self .convert_args_dict_to_type (self .properties_dict ))
3938
4039 @classmethod
4140 def factory (cls , tool : Tool , ** kargs ):
@@ -44,16 +43,12 @@ def factory(cls, tool: Tool, **kargs):
4443 description = tool .description ,
4544 function_name = tool .data .name ,
4645 code = tool .data .code ,
47- properties_dict = (
48- tool .data .parameters .properties
49- if tool .data .parameters .properties
50- else []
51- ),
52- function_id = str (tool .functionId ),
46+ properties_dict = tool .data .parameters .properties if tool .data .parameters .properties else [],
47+ function_id = str (tool .functionId )
5348 )
5449 google_cloud_function_tool .post_init ()
5550 return google_cloud_function_tool
56-
51+
5752 def convert_args_dict_to_type (self , args_schema : Dict ):
5853 args_schema_pydantic = dict ()
5954 for k , v in args_schema .items ():
@@ -65,29 +60,25 @@ def convert_str_args_to_correct_type(self, args):
6560 for k , v in args .items ():
6661 prop = self .properties_dict .get (k )
6762 if prop :
68- typed_args [k ] = (
69- bool (v )
70- if prop .type == "boolean"
71- else (int (v ) if prop .type == "integer" else str (v ))
72- )
63+ typed_args [k ] = bool (v ) if prop .type == "boolean" else (int (v ) if prop .type == "integer" else str (v ))
7364 return typed_args
7465
7566 def query_log_entries (self , limit ):
7667 client = logging_v2 .Client ()
7768 filter_str = f'resource.type="cloud_run_revision" severity>=WARNING resource.labels.service_name="function-{ self .function_id } "'
7869 try :
7970 entries = client .list_entries (
80- filter_ = filter_str , page_size = limit , order_by = "timestamp desc"
71+ filter_ = filter_str ,
72+ page_size = limit ,
73+ order_by = 'timestamp desc'
8174 )
8275 # Convert entries to a list to access the entries
8376 entries_list = list (entries )
84- combined_payloads = "\n " .join (
85- entry .payload for entry in entries_list if entry .payload is not None
86- )
77+ combined_payloads = '\n ' .join (entry .payload for entry in entries_list if entry .payload is not None )
8778 return combined_payloads
8879 except GoogleAPIError as e :
8980 print (f"An error occurred: { e } " )
90- return "" # TODO: what is a sensible value here?
81+ return "" # TODO: what is a sensible value here?
9182
9283 def _run (self , ** kwargs ):
9384 typed_args = self .convert_str_args_to_correct_type (kwargs )
@@ -100,11 +91,8 @@ def _run(self, **kwargs):
10091 TOKEN = google .oauth2 .id_token .fetch_id_token (request , audience )
10192 r = requests .post (
10293 audience ,
103- headers = {
104- "Authorization" : f"Bearer { TOKEN } " ,
105- "Content-Type" : "application/json" ,
106- },
107- data = json .dumps (typed_args , ensure_ascii = False ),
94+ headers = {'Authorization' : f"Bearer { TOKEN } " , "Content-Type" : "application/json" },
95+ data = json .dumps (typed_args )
10896 )
10997 print (f"status code: { r .status_code } " )
11098 print (f"response body: { r .text } " )
0 commit comments