{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":8611872,"sourceType":"datasetVersion","datasetId":5146474}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Installation","metadata":{}},{"cell_type":"markdown","source":"Run in the first time, please consider to use virtual env (conda, env, ...)","metadata":{}},{"cell_type":"code","source":"!pip install requests openai fastapi pymongo uvicorn uuid chromadb\n!pip install llama-index llama-index-readers-chroma llama-index-llms-azure-openai llama-index-embeddings-huggingface llama-index-vector-stores-chroma llama-index-retrievers-bm25","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:12:00.609155Z","iopub.execute_input":"2024-06-05T09:12:00.609570Z","iopub.status.idle":"2024-06-05T09:13:21.654340Z","shell.execute_reply.started":"2024-06-05T09:12:00.609537Z","shell.execute_reply":"2024-06-05T09:13:21.652959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"import os, logging, sys, requests, re\nimport subprocess\n\nlogging.basicConfig(stream=sys.stdout, level=logging.INFO)\nlogging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n\nfrom pydantic import BaseModel\nfrom typing import Optional,Union\n\nfrom llama_index.core import PromptTemplate","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:32.663371Z","iopub.execute_input":"2024-06-05T09:13:32.664042Z","iopub.status.idle":"2024-06-05T09:13:32.671907Z","shell.execute_reply.started":"2024-06-05T09:13:32.663998Z","shell.execute_reply":"2024-06-05T09:13:32.670379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"URL = 'https://code-like-bosch.bluemushroom-85a8a0e6.southeastasia.azurecontainerapps.io'\nAPI_KEY = \"codelikeabosch_968ab358-e4d5-40c2-bfec-bc2965b63667\"\n\nCALL_LLM_ENPOINT = URL + \"/prompt/call_llms\"","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:43.249606Z","iopub.execute_input":"2024-06-05T09:13:43.250044Z","iopub.status.idle":"2024-06-05T09:13:43.255602Z","shell.execute_reply.started":"2024-06-05T09:13:43.250011Z","shell.execute_reply":"2024-06-05T09:13:43.254337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Necessary Function","metadata":{}},{"cell_type":"code","source":"class Item(BaseModel):\n    session_id : Optional[int]\n    prompt : Union[str,PromptTemplate]\n    \n\ndef excute_main_file(log_filename:str = 'log_file.log',script_name:str='main.py',timeout:int = 3600):\n    # Check if the log file exists and delete it if it does\n    if os.path.exists(log_filename):\n        os.remove(log_filename)\n        \n    logging.basicConfig(filename=log_filename, level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')\n    \n    # Run the script and catch any errors\n    try:\n        # Run the subprocess and capture standard output and standard error\n        result = subprocess.run(['python', script_name], text=True, capture_output=True,timeout=timeout)\n        # Check for errors\n        if result.returncode != 0:\n            logging.error(f'An error occurred while running {script_name}: {result.stderr}')\n        else:\n            logging.info(\"Script executed successfully without errors.\")\n\n    except subprocess.TimeoutExpired as e:\n        # Handle the timeout exception\n        logging.error(f'Timeout expired: {script_name} run time exceed {timeout} seconds.')\n        # print(f'Timeout expired: {script_name} did not complete within {timeout_seconds} seconds. {e}')\n\n    except Exception as e:\n        # Log the error if any unexpected exception occurs\n        logging.error(f'An unexpected error occurred while running {script_name}: {e}')\n        \n    return\n\n\ndef extract_and_concatenate_python_code(text):\n    # Pattern to match code blocks enclosed within triple backticks\n    pattern = r\"```python(.*?)```\"\n    \n    # Using re.DOTALL to make '.' match also newline characters\n    matches = re.findall(pattern, text, re.DOTALL)\n    \n    # Concatenating all python code snippets found, separated by new lines\n    concatenated_code = '\\n'.join(code.strip() for code in matches)\n    \n    return concatenated_code\n\n\ndef add_predefined_lib(code):\n    pre_lib=\"from custom_vehicle_motion_api import VehicleMotionAPI\\nimport time\\nimport rospy\\n\"\n    code =  pre_lib + code \n    return code \n\n\ndef append_code_to_file(code, filename):\n    # Append the concatenated code to a Python file\n    with open(filename, 'w') as file:\n        file.write(code + '\\n')  # Adding a newline for separation from existing content\n    print(f\"Python code has been appended to {filename}\")\n    \n    \n","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:44.488329Z","iopub.execute_input":"2024-06-05T09:13:44.488773Z","iopub.status.idle":"2024-06-05T09:13:44.506738Z","shell.execute_reply.started":"2024-06-05T09:13:44.488741Z","shell.execute_reply":"2024-06-05T09:13:44.505480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prompt Templates","metadata":{}},{"cell_type":"markdown","source":"## IMPLEMET_STATE_MACHINE","metadata":{}},{"cell_type":"code","source":"IMPLEMET_STATE_MACHINE = PromptTemplate(\n\"\"\"\n## INSTRUCTION ##\nCraft a description of a state machine tailored to specific vehicle behaviors. Note that this description should not be too long.\n\n## Vehicle Behavior ##\n{describe}\n\n## Requirements ##\n- State Definitions: Enumerate and describe each state comprehensively. Include the actions executed within each state.\n- Transitions: Clearly specify the conditions that trigger transitions between states.\n\"\"\"\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:45.780540Z","iopub.execute_input":"2024-06-05T09:13:45.780997Z","iopub.status.idle":"2024-06-05T09:13:45.789110Z","shell.execute_reply.started":"2024-06-05T09:13:45.780963Z","shell.execute_reply":"2024-06-05T09:13:45.787821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## GENERATE_CODE","metadata":{}},{"cell_type":"code","source":"GENERATE_CODE = PromptTemplate(\n\"\"\"\n## Objective\nDevelop Python code using a state machine approach to manage a virtual vehicle in a simulation environment. The code should enable the vehicle to navigate through various states, accomplishing specific driving tasks effectively.\n## Task Description \n{task_description}\n\n## Description of the State Machine\n{describe_state_machine}\n\n### Important Consideration:\nEach state must continuously apply real-time control commands (like throttle and steering) using the provided Vehicle Control API functions to ensure smooth and realistic vehicle movements. Use only the provided API functions when controling vehicle. Also, avoid using `time.sleep()` or any other delays that interrupt the execution flow.\n\n\n## Continuous Control Implementation Guide:\n- **Loop Execution**: Implement loops to continuously apply control commands during each state's active period.\n- **Avoid Delays**: Exclude `time.sleep()` or similar functions within loops to ensure seamless control command execution.\n- **Feedback Integration**: Dynamically adjust control commands based on real-time feedback from vehicle sensors within the control loops.\n- **Avoid IDLE state**: Do not define the IDLE state.\n- ** vehicle_control_manual**: Use this function for control vehicle\n\n## Code Template\n```python\nfrom vehicle_motion_api import VehicleMotionAPI\nimport time\nimport rospy\n\ndef main():\n    # Main function\n    VM = VehicleMotionAPI()\n    VM.General.vehicle_control_manual_override(True)\n    vehicle = VM.General  # This object, 'vehicle', is used for all API calling\n    \n    try:\n        while not VM.Generall.exit_flag:\n            # Begin application code \n            # Implement state machine to control 'vehicle'\n            # End application code \n    except KeyboardInterrupt:\n        print(\"Shutting down\")\n\nif __name__ == '__main__':\n    try:\n        main()\n    except rospy.ROSInterruptException:\n        pass\n```\n## Vehicle Control API Summary ##\n\n### Methods for Vehicle Control ###\n\n### `vehicle_control_manual_override(override)` ###\n**Input**: `override` (Bool) - Boolean value to enable or disable manual override.\n**Purpose**: Sets the manual override status for vehicle controls.\n**Output**: None.\n\n### `vehicle_control_manual(throttle, steer, brake, hand_brake, reverse, manual_gear_shift, gear)` ###\n**Input**:\n    - `throttle` (float) - Throttle value (0.0 to 1.0).\n    - `steer` (float) - Steering angle (-1.0 to 1.0).\n    - `brake` (float) - Brake intensity (0.0 to 1.0).\n    - `hand_brake` (bool) - State of the handbrake.\n    - `reverse` (bool) - Whether the vehicle is in reverse.\n    - `manual_gear_shift` (bool) - Whether manual gear shifting is enabled.\n    - `gear` (int) - Gear number.\n**Purpose**: Manually controls the vehicle with detailed parameters.\n**Output**: None.\n\n## Getter Methods for Vehicle States ##\n\n\n### `get_vehicle_velocity()` ###\n- **Output Example**: `1.2 (m/s)`\n- **Purpose**: Fetches the current velocity of the vehicle.\n- **Output**: Returns the vehicle's current speed as a float.\n\n### `get_vehicle_orientation()` ###\n**Output Example**:\n    ```json\n    \"y\": -1.6292922383290965e-06,\n    \"z\": 0.42434795213668,\n    \"w\": 0.905499207903972\n**Purpose**: Fetches the current orientation of the vehicle.\n**Output**: Returns a dictionary with quaternion components of the vehicle's orientation.\n\n### `get_vehicle_obstacle()` ###\n**Output Example**:\n    ```json\n    \"is_obstacle\": true,\n    \"obstacle_actor\": \"vehicle\",\n    \"obstacle_distance\": 5.243424241\n**Purpose**: Retrieves information about obstacles detected by the vehicle.\n**Output**: Returns a dictionary containing obstacle presence, type, and distance.\n\"\"\"\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:46.841128Z","iopub.execute_input":"2024-06-05T09:13:46.841553Z","iopub.status.idle":"2024-06-05T09:13:46.851234Z","shell.execute_reply.started":"2024-06-05T09:13:46.841519Z","shell.execute_reply":"2024-06-05T09:13:46.850019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GENERATE_CODE_RAG = PromptTemplate(\n\"\"\"\n## Objective\nDevelop Python code using a state machine approach to manage a virtual vehicle in a simulation environment. The code should enable the vehicle to navigate through various states, accomplishing specific driving tasks effectively.\n## Task Description \n{task_description}\n\n## Description of the State Machine\n{describe_state_machine}\n\n### Important Consideration:\nEach state must continuously apply real-time control commands (like throttle and steering) using the provided Vehicle Control API functions to ensure smooth and realistic vehicle movements. Use only the provided API functions when controling vehicle. Also, avoid using `time.sleep()` or any other delays that interrupt the execution flow.\n\n\n## Continuous Control Implementation Guide:\n- **Loop Execution**: Implement loops to continuously apply control commands during each state's active period.\n- **Avoid Delays**: Exclude `time.sleep()` or similar functions within loops to ensure seamless control command execution.\n- **Feedback Integration**: Dynamically adjust control commands based on real-time feedback from vehicle sensors within the control loops.\n- **Avoid IDLE state**: Do not define the IDLE state.\n- ** vehicle_control_manual**: Use this function for control vehicle\n\n## Code Template\n```python\nfrom vehicle_motion_api import VehicleMotionAPI\nimport time\nimport rospy\n\ndef main():\n    # Main function\n    VM = VehicleMotionAPI()\n    VM.General.vehicle_control_manual_override(True)\n    vehicle = VM.General  # This object, 'vehicle', is used for all API calling\n    \n    try:\n        while not VM.Generall.exit_flag:\n            # Begin application code \n            # Implement state machine to control 'vehicle'\n            # End application code \n    except KeyboardInterrupt:\n        print(\"Shutting down\")\n\nif __name__ == '__main__':\n    try:\n        main()\n    except rospy.ROSInterruptException:\n        pass\n```\n## Vehicle Control API ##\n{vehicle_control_api}\n\"\"\"\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:48.100048Z","iopub.execute_input":"2024-06-05T09:13:48.100565Z","iopub.status.idle":"2024-06-05T09:13:48.109682Z","shell.execute_reply.started":"2024-06-05T09:13:48.100528Z","shell.execute_reply":"2024-06-05T09:13:48.108168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## REFINE_CODE ","metadata":{}},{"cell_type":"code","source":"##  Refine Code with some specific requirements of GPT \nREFINE_CODE = PromptTemplate(\n\"\"\"\n{previous_prompt}\n\n## Objective\nReview and revise the provided Python code to ensure it complies with specific requirements, ensuring functionality and efficiency.\n\n## Provided Code\n{current_code}\n\n## Required Modifications\n{requirements}\n\n## Expectations\n- **Accuracy**: Verify that the code adheres strictly to the provided requirements.\n- **Optimization**: Propose enhancements for better performance or improved readability.\n- **Error Handling**: Implement robust error handling to manage potential exceptions or edge cases.\n- **Explanation**: Provide concise explanations for each modification made to the code.\n- **Code Formatting**: Return all modifications in properly formatted Python code blocks to maintain clarity and usability.\n\nPlease use these guidelines to modify and optimize the provided Python script, ensuring that all changes are returned in code format.\n\"\"\"\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:13:49.769083Z","iopub.execute_input":"2024-06-05T09:13:49.769468Z","iopub.status.idle":"2024-06-05T09:13:49.776163Z","shell.execute_reply.started":"2024-06-05T09:13:49.769440Z","shell.execute_reply":"2024-06-05T09:13:49.774638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Demo 1: The vehicle moves forward in 2s then stop completely. Then when vehicle stop completely  rerun the vehicle.","metadata":{}},{"cell_type":"markdown","source":"## Generate State Machine ","metadata":{}},{"cell_type":"code","source":"task_description= \"\"\"\nThe vehicle moves forward in 2s then stop completely and after vehicle stop completely then rerun the vehicle.\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:34:05.383468Z","iopub.execute_input":"2024-06-05T09:34:05.384341Z","iopub.status.idle":"2024-06-05T09:34:05.389180Z","shell.execute_reply.started":"2024-06-05T09:34:05.384308Z","shell.execute_reply":"2024-06-05T09:34:05.387926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"describe_state_machine = requests.post(CALL_LLM_ENPOINT ,json = {\n                                                \"apikey\": API_KEY,\n                                                \"prompt\": IMPLEMET_STATE_MACHINE.format(describe=task_description),\n                                                \"submit\": False,\n                                            }).json()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:37:36.521877Z","iopub.execute_input":"2024-06-05T09:37:36.522712Z","iopub.status.idle":"2024-06-05T09:37:42.678651Z","shell.execute_reply.started":"2024-06-05T09:37:36.522674Z","shell.execute_reply":"2024-06-05T09:37:42.677444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"describe_state_machine","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:38:46.281207Z","iopub.execute_input":"2024-06-05T09:38:46.281631Z","iopub.status.idle":"2024-06-05T09:38:46.288738Z","shell.execute_reply.started":"2024-06-05T09:38:46.281600Z","shell.execute_reply":"2024-06-05T09:38:46.287652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generate Code ","metadata":{}},{"cell_type":"code","source":"prompt_gen = GENERATE_CODE.format(task_description= task_description,\n                                describe_state_machine=describe_state_machine['respone'])\n\ngenerated_code = requests.post(CALL_LLM_ENPOINT ,json = {\n                                                \"apikey\": API_KEY,\n                                                \"prompt\": prompt_gen,\n                                                \"submit\": False,\n                                            }).json()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:34:46.131818Z","iopub.execute_input":"2024-06-05T09:34:46.132443Z","iopub.status.idle":"2024-06-05T09:34:57.661493Z","shell.execute_reply.started":"2024-06-05T09:34:46.132399Z","shell.execute_reply":"2024-06-05T09:34:57.660396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generated_code","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:34:57.663973Z","iopub.execute_input":"2024-06-05T09:34:57.664311Z","iopub.status.idle":"2024-06-05T09:34:57.673764Z","shell.execute_reply.started":"2024-06-05T09:34:57.664283Z","shell.execute_reply":"2024-06-05T09:34:57.672523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract Code and Add to file","metadata":{}},{"cell_type":"code","source":"code_python = extract_and_concatenate_python_code(str(generated_code['respone']))\nappend_code_to_file(code_python,'main_demo1.py')\n\n#excute the code to carla\n# excute_main_file(script_name='main_demo1.py',timeout=20)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T08:37:59.666632Z","iopub.execute_input":"2024-06-04T08:37:59.667033Z","iopub.status.idle":"2024-06-04T08:37:59.674702Z","shell.execute_reply.started":"2024-06-04T08:37:59.667002Z","shell.execute_reply":"2024-06-04T08:37:59.673474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Demo 2: The vehicle moves forward in 3s then move to the right in 1s then continue moving forward","metadata":{}},{"cell_type":"code","source":"task_description= \"\"\"\nThe vehicle moves forward in 3s with throotle is 1 then move to the right in 1s then continue moving forward\n\"\"\"\n\ndescribe_state_machine = requests.post(CALL_LLM_ENPOINT ,json = {\n                                                \"apikey\": API_KEY,\n                                                \"prompt\": task_description,\n                                                \"submit\": False,\n                                            }).json()\n\nprompt_gen = GENERATE_CODE.format(task_description= task_description,\n                                describe_state_machine=describe_state_machine['respone'])\n\ngenerated_code = requests.post(CALL_LLM_ENPOINT ,json = {\n                                                \"apikey\": API_KEY,\n                                                \"prompt\": prompt_gen,\n                                                \"submit\": False,\n                                            }).json()\nprint(generated_code)\n\ncode_python = extract_and_concatenate_python_code(str(generated_code['respone']))\nappend_code_to_file(code_python,'main_demo2.py')\n\n#excute the code to carla\n# excute_main_file(script_name='main_demo2.py',timeout=30)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T08:38:02.667242Z","iopub.execute_input":"2024-06-04T08:38:02.667668Z","iopub.status.idle":"2024-06-04T08:38:11.803631Z","shell.execute_reply.started":"2024-06-04T08:38:02.667636Z","shell.execute_reply":"2024-06-04T08:38:11.802336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Retrieval Augmented Generation (RAG)","metadata":{}},{"cell_type":"markdown","source":"## Build Dataset","metadata":{}},{"cell_type":"markdown","source":"### Import library","metadata":{}},{"cell_type":"code","source":"from llama_index.core.node_parser import  SimpleNodeParser\n# from llama_index.vector_stores.chroma import ChromaVectorStore\nfrom llama_index.core.storage.docstore import SimpleDocumentStore\nfrom llama_index.core import load_index_from_storage\nfrom llama_index.core import (\n    Settings,\n    Document,\n    VectorStoreIndex,\n    StorageContext\n)\n\nimport chromadb\nfrom llama_index.embeddings.huggingface import HuggingFaceEmbedding\nfrom llama_index.vector_stores.chroma import ChromaVectorStore\nfrom llama_index.core.storage.index_store import SimpleIndexStore\nfrom llama_index.embeddings.huggingface import HuggingFaceEmbedding\nfrom llama_index.retrievers.bm25 import BM25Retriever\nfrom llama_index.core.indices.vector_store.retrievers.retriever import VectorIndexRetriever\n\nSettings.embed_model = HuggingFaceEmbedding(\n    model_name=\"BAAI/bge-small-en\"\n)\n\nimport json ","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:14:02.502215Z","iopub.execute_input":"2024-06-05T09:14:02.502602Z","iopub.status.idle":"2024-06-05T09:14:10.304490Z","shell.execute_reply.started":"2024-06-05T09:14:02.502575Z","shell.execute_reply":"2024-06-05T09:14:10.303254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load document","metadata":{}},{"cell_type":"markdown","source":"We prepared for all team the base document that include serveral function for control the vehicel.\nFeel free to update it, but note that have to provide for us befor the elimination round.","metadata":{}},{"cell_type":"code","source":"with open('/kaggle/input/vm-documentation/document.json') as json_data:\n    data = json.load(json_data)\n    json_data.close()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:14:10.306983Z","iopub.execute_input":"2024-06-05T09:14:10.307853Z","iopub.status.idle":"2024-06-05T09:14:10.321561Z","shell.execute_reply.started":"2024-06-05T09:14:10.307784Z","shell.execute_reply":"2024-06-05T09:14:10.320353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Document  Data into Database","metadata":{}},{"cell_type":"code","source":"docs=[]\n\nfor idx,(func_name,desc) in enumerate(data.items()):\n    docs.append(Document(\n        text=desc['Objective'],\n        doc_id=idx,\n        extra_info={\n            'function_name' : func_name,\n            'Input': json.dumps(desc['Input']),\n            'Output': json.dumps(desc['Output']),\n            'Objective': json.dumps(desc['Objective']),\n            'Example': json.dumps(desc['Example']),\n        },\n        excluded_llm_metadata_keys=[\"Pattern Template\"],\n        excluded_embed_metadata_keys=['function_name','Input','Output','Objective', 'Example'],\n    ))\nnodes = SimpleNodeParser().get_nodes_from_documents(docs)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:14:42.997500Z","iopub.execute_input":"2024-06-05T09:14:42.998397Z","iopub.status.idle":"2024-06-05T09:14:43.451700Z","shell.execute_reply.started":"2024-06-05T09:14:42.998355Z","shell.execute_reply":"2024-06-05T09:14:43.450483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Initial DataBase","metadata":{}},{"cell_type":"code","source":"chroma_client = chromadb.PersistentClient('./database/Doc')\nchroma_collection = chroma_client.get_or_create_collection(\"Carla_API\")\nvector_store = ChromaVectorStore(chroma_collection=chroma_collection)\nstorage_context = StorageContext.from_defaults(docstore=SimpleDocumentStore(),vector_store=vector_store)\n\n\nstorage_context.docstore.add_documents(nodes)\n\n\nindex = VectorStoreIndex.from_documents(\n    docs, storage_context=storage_context\n)\nindex.storage_context.persist('./database/Doc')","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:14:56.808400Z","iopub.execute_input":"2024-06-05T09:14:56.808897Z","iopub.status.idle":"2024-06-05T09:14:58.133335Z","shell.execute_reply.started":"2024-06-05T09:14:56.808858Z","shell.execute_reply":"2024-06-05T09:14:58.132036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Loading DataBase","metadata":{}},{"cell_type":"code","source":"chroma_client = chromadb.PersistentClient('./database/Doc')\nchroma_collection = chroma_client.get_or_create_collection(\"Carla_API\")\nvector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n\nstorage_context = StorageContext.from_defaults(\n        docstore=SimpleDocumentStore.from_persist_dir(persist_dir='./database/Doc'),\n        vector_store=vector_store,\n        index_store=SimpleIndexStore.from_persist_dir(persist_dir='./database/Doc')\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:15:02.174907Z","iopub.execute_input":"2024-06-05T09:15:02.175804Z","iopub.status.idle":"2024-06-05T09:15:02.192146Z","shell.execute_reply.started":"2024-06-05T09:15:02.175765Z","shell.execute_reply":"2024-06-05T09:15:02.190575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vector_index = load_index_from_storage(storage_context=storage_context)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:15:02.899115Z","iopub.execute_input":"2024-06-05T09:15:02.899557Z","iopub.status.idle":"2024-06-05T09:15:02.909799Z","shell.execute_reply.started":"2024-06-05T09:15:02.899524Z","shell.execute_reply":"2024-06-05T09:15:02.908333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Initial Retrieve","metadata":{}},{"cell_type":"code","source":"vector_retriever = VectorIndexRetriever(index=vector_index,similarity_top_k=10)\nbm25_retriever = BM25Retriever.from_defaults(docstore=storage_context.docstore, similarity_top_k=10)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:15:04.489275Z","iopub.execute_input":"2024-06-05T09:15:04.489686Z","iopub.status.idle":"2024-06-05T09:15:04.536924Z","shell.execute_reply.started":"2024-06-05T09:15:04.489656Z","shell.execute_reply":"2024-06-05T09:15:04.535725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vector_nodes = vector_retriever.retrieve('vehicle_control_manual')\nbm25_nodes = bm25_retriever.retrieve('vehicle_control_manual')","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:15:05.781184Z","iopub.execute_input":"2024-06-05T09:15:05.781628Z","iopub.status.idle":"2024-06-05T09:15:05.873071Z","shell.execute_reply.started":"2024-06-05T09:15:05.781592Z","shell.execute_reply":"2024-06-05T09:15:05.871905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vehicle_control_api = \"\"\"\"\"\"\nfor node  in bm25_nodes:\n    api = node.node.metadata\n    example = str(api['Example']).replace('\\\\n', '\\n')\n    tmp = f\"\"\"\n### `{str(api['function_name'])}` ###\n**Description: {node.node.text}\n**Objective: {str(api['Objective'])} \n**Input : {str(api['Input'])}\n**Output: {str(api['Output'])}\n**Example: {example}\n\"\"\"\n    vehicle_control_api += tmp\n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:23:13.154290Z","iopub.execute_input":"2024-06-05T09:23:13.154743Z","iopub.status.idle":"2024-06-05T09:23:13.161987Z","shell.execute_reply.started":"2024-06-05T09:23:13.154709Z","shell.execute_reply":"2024-06-05T09:23:13.160659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(vehicle_control_api)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:23:13.858604Z","iopub.execute_input":"2024-06-05T09:23:13.859070Z","iopub.status.idle":"2024-06-05T09:23:13.866395Z","shell.execute_reply.started":"2024-06-05T09:23:13.859036Z","shell.execute_reply":"2024-06-05T09:23:13.864932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task_description= \"\"\"\nThe vehicle moves forward and when the car observe the obstacle, let's stop\n\"\"\"\n\ndescribe_state_machine = requests.post(CALL_LLM_ENPOINT ,json = {\n                                                \"apikey\": API_KEY,\n                                                \"prompt\": IMPLEMET_STATE_MACHINE.format(describe=task_description),\n                                                \"submit\": False,\n                                            }).json()\n\nbm25_nodes = bm25_retriever.retrieve(describe_state_machine['respone'])\nvehicle_control_api = \"\"\"\"\"\"\nfor node  in bm25_nodes:\n    api = node.node.metadata\n    example = str(api['Example']).replace('\\\\n', '\\n')\n    tmp = f\"\"\"\n### `{str(api['function_name'])}` ###\n**Description: {node.node.text}\n**Objective: {str(api['Objective'])} \n**Input : {str(api['Input'])}\n**Output: {str(api['Output'])}\n**Example: {example}\n\"\"\"\n    vehicle_control_api += tmp\n\nprompt_gen = GENERATE_CODE_RAG.format(task_description= task_description,\n                                describe_state_machine=describe_state_machine['respone'],\n                                vehicle_control_api =vehicle_control_api )\n\nprint(prompt_gen)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:43:20.437087Z","iopub.execute_input":"2024-06-05T09:43:20.437489Z","iopub.status.idle":"2024-06-05T09:43:25.781707Z","shell.execute_reply.started":"2024-06-05T09:43:20.437459Z","shell.execute_reply":"2024-06-05T09:43:25.780619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generated_code = requests.post(CALL_LLM_ENPOINT ,json = {\n                                                \"apikey\": API_KEY,\n                                                \"prompt\": prompt_gen,\n                                                \"submit\": False,\n                                            }).json()\nprint(generated_code)\n\ncode_python = extract_and_concatenate_python_code(str(generated_code['respone']))\nappend_code_to_file(code_python,'main_demo_rag.py')\n\n#excute the code to carla\n# excute_main_file(script_name='main_demo_rag.py',timeout=30)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T09:44:13.150983Z","iopub.execute_input":"2024-06-05T09:44:13.151412Z","iopub.status.idle":"2024-06-05T09:44:23.612285Z","shell.execute_reply.started":"2024-06-05T09:44:13.151381Z","shell.execute_reply":"2024-06-05T09:44:23.611021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}