| 
 | 1 | +# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========  | 
 | 2 | +# Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 3 | +# you may not use this file except in compliance with the License.  | 
 | 4 | +# You may obtain a copy of the License at  | 
 | 5 | +#  | 
 | 6 | +#     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 7 | +#  | 
 | 8 | +# Unless required by applicable law or agreed to in writing, software  | 
 | 9 | +# distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 11 | +# See the License for the specific language governing permissions and  | 
 | 12 | +# limitations under the License.  | 
 | 13 | +# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========  | 
 | 14 | +from termcolor import colored  | 
 | 15 | +import os  | 
 | 16 | +import requests  | 
 | 17 | +from bs4 import BeautifulSoup  | 
 | 18 | +from urllib.parse import urljoin  | 
 | 19 | + | 
 | 20 | +from crab import Benchmark, create_benchmark  | 
 | 21 | +from crab.agents.backend_models.camel_rag_model import CamelRAGModel  | 
 | 22 | +from crab.agents.policies import SingleAgentPolicy  | 
 | 23 | +from crab.benchmarks.template import template_benchmark_config  | 
 | 24 | +from camel.types import ModelType, ModelPlatformType  | 
 | 25 | + | 
 | 26 | + | 
 | 27 | +def start_benchmark(benchmark: Benchmark, agent: SingleAgentPolicy):  | 
 | 28 | +    for step in range(20):  | 
 | 29 | +        print("=" * 40)  | 
 | 30 | +        print(f"Start agent step {step}:")  | 
 | 31 | +        observation = benchmark.observe()["template_env"]  | 
 | 32 | +        print(f"Current environment observation: {observation}")  | 
 | 33 | +        response = agent.chat(  | 
 | 34 | +            {  | 
 | 35 | +                "template_env": [  | 
 | 36 | +                    (f"Current environment observation: {observation}", 0),  | 
 | 37 | +                ]  | 
 | 38 | +            }  | 
 | 39 | +        )  | 
 | 40 | +        print(colored(f"Agent take action: {response}", "blue"))  | 
 | 41 | + | 
 | 42 | +        for action in response:  | 
 | 43 | +            response = benchmark.step(  | 
 | 44 | +                action=action.name,  | 
 | 45 | +                parameters=action.arguments,  | 
 | 46 | +                env_name=action.env,  | 
 | 47 | +            )  | 
 | 48 | +            print(  | 
 | 49 | +                colored(  | 
 | 50 | +                    f'Action "{action.name}" success, stat: '  | 
 | 51 | +                    f"{response.evaluation_results}",  | 
 | 52 | +                    "green",  | 
 | 53 | +                )  | 
 | 54 | +            )  | 
 | 55 | +            if response.terminated:  | 
 | 56 | +                print("=" * 40)  | 
 | 57 | +                print(  | 
 | 58 | +                    colored(  | 
 | 59 | +                        f"Task finished, result: {response.evaluation_results}",  | 
 | 60 | +                        "green"  | 
 | 61 | +                    )  | 
 | 62 | +                )  | 
 | 63 | +                return  | 
 | 64 | + | 
 | 65 | + | 
 | 66 | +def prepare_vim_docs():  | 
 | 67 | +    """Prepare Vim documentation for RAG"""  | 
 | 68 | +    print(colored("Starting Vim documentation preparation...", "yellow"))  | 
 | 69 | +    base_url = "https://vimdoc.sourceforge.net/htmldoc/usr_07.html"  | 
 | 70 | +    content_dir = "vim_docs"  | 
 | 71 | +    os.makedirs(content_dir, exist_ok=True)  | 
 | 72 | +      | 
 | 73 | +    print(colored("Fetching main page...", "yellow"))  | 
 | 74 | +    response = requests.get(base_url)  | 
 | 75 | +    soup = BeautifulSoup(response.text, 'html.parser')  | 
 | 76 | +      | 
 | 77 | +    # Process the main page first  | 
 | 78 | +    main_content = soup.get_text(separator='\n', strip=True)  | 
 | 79 | +    with open(os.path.join(content_dir, "main.txt"), 'w', encoding='utf-8') as f:  | 
 | 80 | +        f.write(f"Source: {base_url}\n\n{main_content}")  | 
 | 81 | +      | 
 | 82 | +    links = [link for link in soup.find_all('a')   | 
 | 83 | +             if link.get('href') and not link.get('href').startswith(('#', 'http'))]  | 
 | 84 | +    total_links = len(links)  | 
 | 85 | +    print(colored(f"Found {total_links} documentation pages to process", "yellow"))  | 
 | 86 | +      | 
 | 87 | +    processed_files = []  | 
 | 88 | +    for idx, link in enumerate(links, 1):  | 
 | 89 | +        href = link.get('href')  | 
 | 90 | +        full_url = urljoin(base_url, href)  | 
 | 91 | +        try:  | 
 | 92 | +            print(colored(f"Processing page {idx}/{total_links}: {href}", "yellow"))  | 
 | 93 | +              | 
 | 94 | +            # Fetch and process page  | 
 | 95 | +            page_response = requests.get(full_url)  | 
 | 96 | +            page_soup = BeautifulSoup(page_response.text, 'html.parser')  | 
 | 97 | +            for tag in page_soup(['script', 'style']):  | 
 | 98 | +                tag.decompose()  | 
 | 99 | +            content = page_soup.get_text(separator='\n', strip=True)  | 
 | 100 | +              | 
 | 101 | +            # Save content  | 
 | 102 | +            filename = os.path.join(content_dir, f"{href.replace('/', '_')}.txt")  | 
 | 103 | +            with open(filename, 'w', encoding='utf-8') as f:  | 
 | 104 | +                f.write(f"Source: {full_url}\n\n{content}")  | 
 | 105 | +            processed_files.append(filename)  | 
 | 106 | +            print(colored(f"✓ Saved {href}", "green"))  | 
 | 107 | +              | 
 | 108 | +        except Exception as e:  | 
 | 109 | +            print(colored(f"✗ Error processing {full_url}: {e}", "red"))  | 
 | 110 | +      | 
 | 111 | +    print(colored("Documentation preparation completed!", "green"))  | 
 | 112 | +    return processed_files  | 
 | 113 | + | 
 | 114 | + | 
 | 115 | +if __name__ == "__main__":  | 
 | 116 | +    print(colored("=== Starting RAG-enhanced benchmark ===", "cyan"))  | 
 | 117 | +      | 
 | 118 | +    # Initialize benchmark and environment  | 
 | 119 | +    print(colored("\nInitializing benchmark environment...", "yellow"))  | 
 | 120 | +    benchmark = create_benchmark(template_benchmark_config)  | 
 | 121 | +    task, action_space = benchmark.start_task("0")  | 
 | 122 | +    env_descriptions = benchmark.get_env_descriptions()  | 
 | 123 | + | 
 | 124 | +    doc_files = prepare_vim_docs()  | 
 | 125 | +      | 
 | 126 | +    print(colored("\nInitializing RAG model...", "yellow"))  | 
 | 127 | +    rag_model = CamelRAGModel(  | 
 | 128 | +        model="gpt-4o",  | 
 | 129 | +        model_platform=ModelPlatformType.OPENAI,  | 
 | 130 | +        parameters={"temperature": 0.7}  | 
 | 131 | +    )  | 
 | 132 | +      | 
 | 133 | +    print(colored("Processing documents for RAG...", "yellow"))  | 
 | 134 | +    for doc_file in doc_files:  | 
 | 135 | +        print(colored(f"Processing {doc_file}...", "yellow"))  | 
 | 136 | +        rag_model.process_documents(doc_file)  | 
 | 137 | +    print(colored("RAG model initialization complete!", "green"))  | 
 | 138 | +      | 
 | 139 | +    print(colored("\nSetting up agent...", "yellow"))  | 
 | 140 | +    agent = SingleAgentPolicy(model_backend=rag_model)  | 
 | 141 | +    agent.reset(task.description, action_space, env_descriptions)  | 
 | 142 | +      | 
 | 143 | +    print(colored("\nStarting benchmark execution:", "cyan"))  | 
 | 144 | +    print("Start performing task: " + colored(f'"{task.description}"', "green"))  | 
 | 145 | +    start_benchmark(benchmark, agent)  | 
 | 146 | +    benchmark.reset()  | 
0 commit comments