Skip to content

Commit 37ea1fc

Browse files
authored
Merge pull request #215 from mplachta/knowledge-example
Knowledge example
2 parents fe723ea + 8f8bb55 commit 37ea1fc

File tree

12 files changed

+4945
-0
lines changed

12 files changed

+4945
-0
lines changed

meta_quest_knowledge/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
__pycache__/

meta_quest_knowledge/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# PDF Knowledge Example
2+
3+
This project demonstrates how to create a Crew of AI agents and tasks using crewAI. It uses a PDF knowledge source to answer user questions based on the content of the PDF. The PDF is loaded from a file and the knowledge source is initialized with it. The project also includes a custom task that uses the knowledge source to answer user questions. You can modify the question in the `main.py` file.
4+
5+
## Installation
6+
7+
Ensure you have Python >=3.10 <=3.13 installed on your system. This project uses [UV](https://docs.astral.sh/uv/) for dependency management and package handling, offering a seamless setup and execution experience.
8+
9+
First, if you haven't already, install uv:
10+
11+
```bash
12+
pip install uv
13+
```
14+
15+
Next, navigate to your project directory and install the dependencies:
16+
17+
(Optional) Lock the dependencies and install them by using the CLI command:
18+
```bash
19+
crewai install
20+
```
21+
### Customizing
22+
23+
**Add your `OPENAI_API_KEY` into the `.env` file**
24+
25+
- Modify `src/meta_quest_knowledge/config/agents.yaml` to define your agents
26+
- Modify `src/meta_quest_knowledge/config/tasks.yaml` to define your tasks
27+
- Modify `src/meta_quest_knowledge/crew.py` to add your own logic, tools and specific args
28+
- Modify `src/meta_quest_knowledge/main.py` to add custom inputs for your agents and tasks
29+
30+
## Running the Project
31+
32+
To kickstart your crew of AI agents and begin task execution, run this from the root folder of your project:
33+
34+
```bash
35+
$ crewai run
36+
```
37+
38+
This command initializes the Crew, assembling the agents and assigning them tasks as defined in your configuration.
39+
40+
## Additional Knowledge Sources
41+
42+
Explore [Knowledge](https://docs.crewai.com/concepts/knowledge) documentation for more information on how to use different knowledge sources.
43+
You can select from multiple different knowledge sources such as:
44+
* Text files
45+
* PDFs
46+
* CSV & Excel files
47+
* JSON files
48+
* Sources supported by [docling](https://github.com/DS4SD/docling)
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
User name is John Doe.
2+
User is an AI Engineer.
3+
User is interested in AI Agents.
4+
User is based in San Francisco, California.

meta_quest_knowledge/pyproject.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[project]
2+
name = "Meta Quest Knowledge"
3+
version = "0.1.0"
4+
description = "Knowledge Example using crewAI"
5+
authors = [{ name = "Mike Plachta", email = "mike@crewai.com" }]
6+
requires-python = ">=3.10,<=3.13"
7+
dependencies = [
8+
"crewai[tools]>=0.95.0,<1.0.0",
9+
]
10+
11+
[project.scripts]
12+
meta_quest_knowledge = "meta_quest_knowledge.main:run"
13+
run_crew = "meta_quest_knowledge.main:run"
14+
train = "meta_quest_knowledge.main:train"
15+
replay = "meta_quest_knowledge.main:replay"
16+
test = "meta_quest_knowledge.main:test"
17+
18+
[build-system]
19+
requires = ["hatchling"]
20+
build-backend = "hatchling.build"

meta_quest_knowledge/src/meta_quest_knowledge/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
meta_quest_expert:
2+
role: >
3+
Meta Quest Expert
4+
goal: >
5+
Provide the best possible answers to questions about Meta Quest
6+
backstory: >
7+
You're a seasoned expert in the world of Meta Quest. You're known for your
8+
ability to provide the best possible answers to questions about this
9+
cutting-edge technology, ensuring that your audience is well-informed and
10+
satisfied with the latest advancements in the field.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
answer_question_task:
2+
description: >
3+
Answer the user question with the most relevant information from the context and available knowledge sources.
4+
Question: {question}
5+
6+
Do not answer questions that are not related to the context or knowledge sources.
7+
expected_output: >
8+
Best answer to the user question
9+
agent: meta_quest_expert
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from crewai import Agent, Crew, Process, Task
2+
from crewai.project import CrewBase, agent, crew, task
3+
from crewai.knowledge.source.pdf_knowledge_source import PDFKnowledgeSource
4+
5+
# Knowledge sources
6+
pdf_source = PDFKnowledgeSource(
7+
file_paths=["meta_quest_manual.pdf"]
8+
)
9+
10+
@CrewBase
11+
class MetaQuestKnowledge():
12+
"""MetaQuestKnowledge crew"""
13+
14+
agents_config = 'config/agents.yaml'
15+
tasks_config = 'config/tasks.yaml'
16+
17+
@agent
18+
def meta_quest_expert(self) -> Agent:
19+
return Agent(
20+
config=self.agents_config['meta_quest_expert'],
21+
verbose=True
22+
)
23+
24+
@task
25+
def answer_question_task(self) -> Task:
26+
return Task(
27+
config=self.tasks_config['answer_question_task'],
28+
)
29+
30+
@crew
31+
def crew(self) -> Crew:
32+
"""Creates the MetaQuestKnowledge crew"""
33+
34+
return Crew(
35+
agents=self.agents, # Automatically created by the @agent decorator
36+
tasks=self.tasks, # Automatically created by the @task decorator
37+
process=Process.sequential,
38+
verbose=True,
39+
knowledge_sources=[
40+
pdf_source
41+
]
42+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
import sys
3+
import warnings
4+
5+
from meta_quest_knowledge.crew import MetaQuestKnowledge
6+
7+
warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")
8+
9+
# This main file is intended to be a way for you to run your
10+
# crew locally, so refrain from adding unnecessary logic into this file.
11+
# Replace with inputs you want to test with, it will automatically
12+
# interpolate any tasks and agents information
13+
14+
def run():
15+
"""
16+
Run the crew.
17+
"""
18+
inputs = {
19+
'question': 'How often should I take breaks?',
20+
}
21+
MetaQuestKnowledge().crew().kickoff(inputs=inputs)
22+
23+
24+
def train():
25+
"""
26+
Train the crew for a given number of iterations.
27+
"""
28+
inputs = {
29+
'question': 'How often should I take breaks?',
30+
}
31+
try:
32+
MetaQuestKnowledge().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)
33+
34+
except Exception as e:
35+
raise Exception(f"An error occurred while training the crew: {e}")
36+
37+
def replay():
38+
"""
39+
Replay the crew execution from a specific task.
40+
"""
41+
try:
42+
MetaQuestKnowledge().crew().replay(task_id=sys.argv[1])
43+
44+
except Exception as e:
45+
raise Exception(f"An error occurred while replaying the crew: {e}")
46+
47+
def test():
48+
"""
49+
Test the crew execution and returns the results.
50+
"""
51+
inputs = {
52+
'question': 'How often should I take breaks?',
53+
}
54+
try:
55+
MetaQuestKnowledge().crew().test(n_iterations=int(sys.argv[1]), openai_model_name=sys.argv[2], inputs=inputs)
56+
57+
except Exception as e:
58+
raise Exception(f"An error occurred while replaying the crew: {e}")

meta_quest_knowledge/src/meta_quest_knowledge/tools/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)