|
| 1 | +import os |
| 2 | +from crewai import Agent |
| 3 | +from crewai import Task |
| 4 | +from crewai import Crew, Process, LLM |
| 5 | +from crewai.cli.constants import DEFAULT_LLM_MODEL |
| 6 | +from crewai_tools import MongoDBVectorSearchTool, MongoDBVectorSearchConfig |
| 7 | +from langchain_community.document_loaders import PyPDFLoader |
| 8 | +import time |
| 9 | + |
| 10 | +# Set environment as LiteLLM expects |
| 11 | +os.environ["AZURE_API_KEY"] = os.environ["AZURE_OPENAI_API_KEY"] |
| 12 | +os.environ["AZURE_API_BASE"] = os.environ["AZURE_OPENAI_ENDPOINT"] |
| 13 | +os.environ["AZURE_API_VERSION"] = os.environ["OPENAI_API_VERSION"] |
| 14 | + |
| 15 | +# Pre-populate a collection and an index |
| 16 | +print("Creating collection...") |
| 17 | +conn_string = os.environ.get( |
| 18 | + "MONGODB_URI", "mongodb://localhost:27017?directConnection=true" |
| 19 | +) |
| 20 | +database_name = "crewai_test_db" |
| 21 | +collection_name = "vector_test" |
| 22 | + |
| 23 | +tool = MongoDBVectorSearchTool( |
| 24 | + connection_string=conn_string, |
| 25 | + database_name=database_name, |
| 26 | + collection_name=collection_name, |
| 27 | +) |
| 28 | +coll = tool._coll |
| 29 | +coll.delete_many({}) |
| 30 | + |
| 31 | +# Insert documents from a pdf. |
| 32 | +print("Loading documents...") |
| 33 | +loader = PyPDFLoader("https://arxiv.org/pdf/2303.08774.pdf") |
| 34 | +tool.add_texts([i.page_content for i in loader.load()]) |
| 35 | + |
| 36 | +print("Creating vector index...") |
| 37 | +if not any([ix["name"] == "vector_index" for ix in coll.list_search_indexes()]): |
| 38 | + tool.create_vector_search_index(dimensions=1536, auto_index_timeout=60) |
| 39 | + |
| 40 | +# Create the MongoDB tool |
| 41 | +print("Creating tool and waiting for index to be complete...") |
| 42 | + |
| 43 | +# Wait for index to be complete. |
| 44 | +n_docs = coll.count_documents({}) |
| 45 | +tool.query_config = MongoDBVectorSearchConfig(limit=n_docs, oversampling_factor=1) |
| 46 | +start = time.monotonic() |
| 47 | +while time.monotonic() - start <= 60: |
| 48 | + if len(tool._run("sandwich")) == n_docs: |
| 49 | + break |
| 50 | + else: |
| 51 | + time.sleep(1) |
| 52 | + |
| 53 | +# Assemble a crew |
| 54 | +researcher = Agent( |
| 55 | + role="AI Accuracy Researcher", |
| 56 | + goal="Find and extract key information from a technical document", |
| 57 | + backstory="You're specialized in analyzing technical content to extract insights and answers", |
| 58 | + verbose=False, |
| 59 | + tools=[tool], |
| 60 | + llm=LLM(model=f"azure/{DEFAULT_LLM_MODEL}"), |
| 61 | +) |
| 62 | +research_task = Task( |
| 63 | + description="Research information in a technical document", |
| 64 | + expected_output="A summary of the accuracy of GPT-4", |
| 65 | + agent=researcher, |
| 66 | +) |
| 67 | +crew = Crew( |
| 68 | + agents=[researcher], |
| 69 | + tasks=[research_task], |
| 70 | + process=Process.sequential, |
| 71 | + verbose=False, |
| 72 | +) |
| 73 | + |
| 74 | +# Get the result and assert something about the results |
| 75 | +print("Running the crew...") |
| 76 | +result = crew.kickoff() |
| 77 | +text = result.raw.lower() |
| 78 | +assert "advancements" in text or "improvements" in text, text |
| 79 | +assert "GPT-4" in result.raw |
0 commit comments