Skip to content

[BUG] Cannot add knowledges to Agent and "knowledge_sources" parameter doesnt work on Crew #2806

Open
@tungnd173457

Description

@tungnd173457

Description

The test code:

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,
)

# Create an LLM with a temperature of 0 to ensure deterministic outputs
llm = LLM(model="gpt-4o", 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,
    knowledge_sources=[string_source],
)
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],
)

result = crew.kickoff(inputs={"question": "What city does John live in and how old is he?"})

Firstly, i use knowledge_sources on both Agent and Crew, but then i check my database, only knowledge of Crew is added to chromadb, Agent's knowledge isn't. So i check set_knowledge function in agent.py and added: self.knowledge.add_sources() , it works.
Secondly, if i only define knowledge_sources on Crew, the Agent actually doesnt get access to knowledge source. Continue checking, i realize that in this logic, knowledge of Crew only add to task_prompt when knowledge of Agent exist. I thought if i define knowledge_sources on Crew level, all Agents will get access to the knowledge as well?
Are these bugs or what am i missing here?

Steps to Reproduce

Run the test code

Expected behavior

Knowledges of Agent should be added to database and knowledge_sources should work on Crew level

Screenshots/Code snippets

Database's collection when run test code (Collection of Agent is empty):

Collection: knowledge_crew
{'ids': ['225f7a909030abdbcdd1f8d0657e0e5bcd64a725936f50246ec4ac28fcf4ffe9'], 'embeddings': None, 'documents': ['Users name is John. He is 30 years old and lives in San Francisco.'], 'uris': None, 'data': None, 'metadatas': [None], 'included': [<IncludeEnum.documents: 'documents'>, <IncludeEnum.metadatas: 'metadatas'>]}
Collection: knowledge_About_User
{'ids': [], 'embeddings': None, 'documents': [], 'uris': None, 'data': None, 'metadatas': [], 'included': [<IncludeEnum.documents: 'documents'>, <IncludeEnum.metadatas: 'metadatas'>]}

Operating System

Ubuntu 20.04

Python Version

3.12

crewAI Version

0.119.0

crewAI Tools Version

0.119.0

Virtual Environment

Venv

Evidence

None

Possible Solution

None

Additional context

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions