Created
June 23, 2025 23:33
-
-
Save enakai00/05fe6f12cdca275669ae6664dd229029 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- fast_api.py.orig 2025-06-23 20:29:43.919757312 +0000 | |
| +++ fast_api.py 2025-06-23 20:30:19.763378438 +0000 | |
| @@ -960,4 +960,61 @@ | |
| StaticFiles(directory=ANGULAR_DIST_PATH, html=True), | |
| name="static", | |
| ) | |
| + | |
| + async def _get_runner_async(app_name: str): | |
| + """Returns the runner for the given app.""" | |
| + import vertexai, json | |
| + | |
| + envs.load_dotenv_for_agent(os.path.basename(app_name), agents_dir) | |
| + PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] | |
| + LOCATION = os.environ['GOOGLE_CLOUD_LOCATION'] | |
| + AGENT_ID = os.environ['AGENT_ID'] | |
| + vertexai.init(project=PROJECT_ID, location=LOCATION) | |
| + remote_agent = vertexai.agent_engines.get(AGENT_ID) | |
| + | |
| + if app_name in runner_dict: | |
| + return runner_dict[app_name] | |
| + | |
| + def convert_dict_keys_to_camel_case(data): | |
| + if isinstance(data, dict): | |
| + new_dict = {} | |
| + for key, value in data.items(): | |
| + components = key.split('_') | |
| + new_key = components[0] + ''.join(x.title() for x in components[1:]) | |
| + new_dict[new_key] = convert_dict_keys_to_camel_case(value) | |
| + return new_dict | |
| + elif isinstance(data, list): | |
| + return [convert_dict_keys_to_camel_case(elem) for elem in data] | |
| + else: | |
| + return data | |
| + | |
| + class myEvent: | |
| + def __init__(self, event): | |
| + self.event = event | |
| + def model_dump_json(self, exclude_none, by_alias): | |
| + return json.dumps(convert_dict_keys_to_camel_case(self.event)) | |
| + | |
| + class remote_runner: | |
| + def __init__(self, app_name): | |
| + self._remote_agent = remote_agent | |
| + self._session_map = {} | |
| + | |
| + async def run_async(self, user_id, session_id, new_message, run_config): | |
| + query = new_message.parts[-1].text | |
| + if not session_id in self._session_map.keys(): | |
| + self._session_map[session_id] = self._remote_agent.create_session(user_id=user_id) | |
| + session = self._session_map[session_id] | |
| + events = self._remote_agent.stream_query( | |
| + user_id=user_id, | |
| + session_id=session['id'], | |
| + message=query, | |
| + ) | |
| + for event in events: | |
| + yield myEvent(event) | |
| + | |
| + runner = remote_runner(app_name) | |
| + | |
| + runner_dict[app_name] = runner | |
| + return runner | |
| + | |
| return app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment