Skip to content

Commit

Permalink
Update camel_example and camel_rag_example
Browse files Browse the repository at this point in the history
  • Loading branch information
anjieyang committed Oct 28, 2024
1 parent b140715 commit bc627da
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
15 changes: 15 additions & 0 deletions crab-benchmark-v0/dataset/ubuntu/camel-task-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"description": "Using Firefox, navigate to the CAMEL-AI GitHub repository (https://github.com/camel-ai/camel). Download the example code file 'examples/role_playing.py', then use Visual Studio Code to open it and save a copy to '/home/crab/camel_examples/role_playing.py'.",
"tasks": [
{
"task": "a313ea4d-e501-4971-b4fe-db2aad19eac1",
"attribute": {
"url": "https://raw.githubusercontent.com/camel-ai/camel/master/examples/role_playing.py",
"file_path": "/home/crab/camel_examples/role_playing.py"
},
"output": "/home/crab/camel_examples/role_playing.py"
}
],
"adjlist": "0",
"id": "camel-example-001"
}
32 changes: 26 additions & 6 deletions crab/agents/backend_models/camel_rag_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from crab import BackendOutput, MessageType
from crab.agents.backend_models.camel_model import CamelModel
from camel.messages import BaseMessage
from langchain.schema import Document

try:
from camel.embeddings import OpenAIEmbedding
Expand Down Expand Up @@ -83,13 +84,19 @@ def _enhance_with_context(self, messages: List[Tuple[str, MessageType]]) -> List
""
)

retrieved_info = self.retriever.query(
query=query,
top_k=self.top_k,
similarity_threshold=self.similarity_threshold,
)
try:
retrieved_info = self.retriever.query(
query=query,
top_k=self.top_k,
similarity_threshold=self.similarity_threshold,
)
except Exception:
return messages

if not retrieved_info:
return messages

if not retrieved_info or retrieved_info[0].get('text', '').startswith('No suitable information'):
if not retrieved_info[0].get('payload'):
return messages

context = "Relevant context:\n\n"
Expand All @@ -106,3 +113,16 @@ def _enhance_with_context(self, messages: List[Tuple[str, MessageType]]) -> List
def chat(self, messages: List[Tuple[str, MessageType]]) -> BackendOutput:
enhanced_messages = self._enhance_with_context(messages)
return super().chat(enhanced_messages)

def get_relevant_content(self, query: str) -> List[Document]:
if not self.vector_storage:
return []

try:
return self.retriever.query(
query=query,
top_k=self.top_k,
similarity_threshold=self.similarity_threshold,
)
except Exception:
return []
1 change: 1 addition & 0 deletions examples/camel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def start_benchmark(benchmark: Benchmark, agent: SingleAgentPolicy):

if __name__ == "__main__":
benchmark = create_benchmark(template_benchmark_config)
#TODO: Use new task config
task, action_space = benchmark.start_task("0")
env_descriptions = benchmark.get_env_descriptions()

Expand Down
25 changes: 19 additions & 6 deletions examples/camel_rag_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,38 @@
from crab.benchmarks.template import template_benchmark_config
from camel.types import ModelType, ModelPlatformType


# TODO: Add new benchmark template
def start_benchmark(benchmark: Benchmark, agent: SingleAgentPolicy):
for step in range(20):
print("=" * 40)
print(f"Start agent step {step}:")
observation = benchmark.observe()["template_env"]
print(f"Current environment observation: {observation}")

try:
rag_content = agent.model_backend.get_relevant_content(str(observation))
print(colored("\nRelevant RAG content:", "magenta"))
if rag_content:
for idx, content in enumerate(rag_content, 1):
print(colored(f"\nDocument {idx}:", "magenta"))
if isinstance(content, dict):
print(colored(f"Source: {content.get('content path', 'Unknown')}", "yellow"))
print(colored(f"Content: {content.get('text', '')[:500]}...", "white"))
else:
print(colored(f"Content: {str(content)[:500]}...", "white"))
else:
print(colored("No relevant content found", "yellow"))
except Exception as e:
print(colored(f"Error retrieving RAG content: {str(e)}", "red"))

response = agent.chat(
{
"template_env": [
(f"Current environment observation: {observation}", 0),
]
}
)
print(colored(f"Agent take action: {response}", "blue"))
print(colored(f"\nAgent take action: {response}", "blue"))

for action in response:
response = benchmark.step(
Expand Down Expand Up @@ -74,7 +91,6 @@ def prepare_vim_docs():
response = requests.get(base_url)
soup = BeautifulSoup(response.text, 'html.parser')

# Process the main page first
main_content = soup.get_text(separator='\n', strip=True)
with open(os.path.join(content_dir, "main.txt"), 'w', encoding='utf-8') as f:
f.write(f"Source: {base_url}\n\n{main_content}")
Expand All @@ -91,14 +107,12 @@ def prepare_vim_docs():
try:
print(colored(f"Processing page {idx}/{total_links}: {href}", "yellow"))

# Fetch and process page
page_response = requests.get(full_url)
page_soup = BeautifulSoup(page_response.text, 'html.parser')
for tag in page_soup(['script', 'style']):
tag.decompose()
content = page_soup.get_text(separator='\n', strip=True)

# Save content
filename = os.path.join(content_dir, f"{href.replace('/', '_')}.txt")
with open(filename, 'w', encoding='utf-8') as f:
f.write(f"Source: {full_url}\n\n{content}")
Expand All @@ -115,7 +129,6 @@ def prepare_vim_docs():
if __name__ == "__main__":
print(colored("=== Starting RAG-enhanced benchmark ===", "cyan"))

# Initialize benchmark and environment
print(colored("\nInitializing benchmark environment...", "yellow"))
benchmark = create_benchmark(template_benchmark_config)
task, action_space = benchmark.start_task("0")
Expand Down

0 comments on commit bc627da

Please sign in to comment.