-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial knowledge * WIP * Adding core knowledge sources * Improve types and better support for file paths * added additional sources * fix linting * update yaml to include optional deps * adding in lorenze feedback * ensure embeddings are persisted * improvements all around Knowledge class * return this * properly reset memory * properly reset memory+knowledge * consolodation and improvements * linted * cleanup rm unused embedder * fix test * fix duplicate * generating cassettes for knowledge test * updated default embedder * None embedder to use default on pipeline cloning * improvements * fixed text_file_knowledge * mypysrc fixes * type check fixes * added extra cassette * just mocks * linted * mock knowledge query to not spin up db * linted * verbose run * put a flag * fix * adding docs * better docs * improvements from review * more docs * linted * rm print * more fixes * clearer docs * added docstrings and type hints for cli --------- Co-authored-by: João Moura <[email protected]> Co-authored-by: Lorenze Jay <[email protected]>
- Loading branch information
1 parent
fde1ee4
commit 14a36d3
Showing
37 changed files
with
2,305 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: Knowledge | ||
description: What is knowledge in CrewAI and how to use it. | ||
icon: book | ||
--- | ||
|
||
# Using Knowledge in CrewAI | ||
|
||
## Introduction | ||
|
||
The Knowledge class in CrewAI provides a powerful way to manage and query knowledge sources for your AI agents. This guide will show you how to implement knowledge management in your CrewAI projects. | ||
Additionally, we have specific tools for generate knowledge sources for strings, text files, PDF's, and Spreadsheets. You can expand on any source type by extending the `KnowledgeSource` class. | ||
|
||
## Basic Implementation | ||
|
||
Here's a simple example of how to use the Knowledge class: | ||
|
||
```python | ||
from crewai import Agent, Task, Crew, Process, LLM | ||
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource | ||
|
||
# Create a knowledge source | ||
content = "Users name is John. He is 30 years old and lives in San Francisco." | ||
string_source = StringKnowledgeSource( | ||
content=content, metadata={"preference": "personal"} | ||
) | ||
|
||
|
||
llm = LLM(model="gpt-4o-mini", temperature=0) | ||
# Create an agent with the knowledge store | ||
agent = Agent( | ||
role="About User", | ||
goal="You know everything about the user.", | ||
backstory="""You are a master at understanding people and their preferences.""", | ||
verbose=True, | ||
allow_delegation=False, | ||
llm=llm, | ||
) | ||
task = Task( | ||
description="Answer the following questions about the user: {question}", | ||
expected_output="An answer to the question.", | ||
agent=agent, | ||
) | ||
|
||
crew = Crew( | ||
agents=[agent], | ||
tasks=[task], | ||
verbose=True, | ||
process=Process.sequential, | ||
knowledge={"sources": [string_source], "metadata": {"preference": "personal"}}, # Enable knowledge by adding the sources here. You can also add more sources to the sources list. | ||
) | ||
|
||
result = crew.kickoff(inputs={"question": "What city does John live in and how old is he?"}) | ||
``` | ||
|
||
|
||
## Embedder Configuration | ||
|
||
You can also configure the embedder for the knowledge store. This is useful if you want to use a different embedder for the knowledge store than the one used for the agents. | ||
|
||
```python | ||
... | ||
string_source = StringKnowledgeSource( | ||
content="Users name is John. He is 30 years old and lives in San Francisco.", | ||
metadata={"preference": "personal"} | ||
) | ||
crew = Crew( | ||
... | ||
knowledge={ | ||
"sources": [string_source], | ||
"metadata": {"preference": "personal"}, | ||
"embedder_config": {"provider": "openai", "config": {"model": "text-embedding-3-small"}}, | ||
}, | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import List | ||
|
||
import numpy as np | ||
|
||
|
||
class BaseEmbedder(ABC): | ||
""" | ||
Abstract base class for text embedding models | ||
""" | ||
|
||
@abstractmethod | ||
def embed_chunks(self, chunks: List[str]) -> np.ndarray: | ||
""" | ||
Generate embeddings for a list of text chunks | ||
Args: | ||
chunks: List of text chunks to embed | ||
Returns: | ||
Array of embeddings | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def embed_texts(self, texts: List[str]) -> np.ndarray: | ||
""" | ||
Generate embeddings for a list of texts | ||
Args: | ||
texts: List of texts to embed | ||
Returns: | ||
Array of embeddings | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def embed_text(self, text: str) -> np.ndarray: | ||
""" | ||
Generate embedding for a single text | ||
Args: | ||
text: Text to embed | ||
Returns: | ||
Embedding array | ||
""" | ||
pass | ||
|
||
@property | ||
@abstractmethod | ||
def dimension(self) -> int: | ||
"""Get the dimension of the embeddings""" | ||
pass |
Oops, something went wrong.