Why does the knowledge feature not work and return a generic response: "I currently do not have the necessary information to determine which city John lives in or his age."

I ran the demo of “knowledge” on the official website and could not get the expected output.
output:
Task: Answer the following questions about the user: What city does John live in and how old is he?
Agent: About User
Final Answer:
I currently do not have the necessary information to determine which city John lives in or his age.

from crewai import Agent, Task, Crew, Process, LLM
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
from dotenv import load_dotenv
load_dotenv()

content = “John is 30 years old and lives in San Francisco.”
string_source = StringKnowledgeSource(
content=content, metadata={“preference”: “personal”}
)

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

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

Try to set the knowledge_sources parameter to the agent as follows:

agent = Agent(
    ...
    knowledge_sources=[
        StringKnowledgeSource(
            content="Users name is John. He is 30 years old and lives in San Francisco.",
            metadata={"preference": "personal"},
        )
    ],
)

[quote=“leocache, post:4, topic:1772, full:true”]
Thanks, this solution is very useful, but why can’t I use the method on the official website?If I customize a document class based on BaseKnowledgeSource, how should I solve it?

string_source = StringKnowledgeSource(
content=“Users name is John. He is 30 years old and lives in San Francisco.”,
metadata={“preference”: “personal”},
)
agent = Agent(

knowledge_sources=[string_source],
)

@leocache Not working for me as well. Looking into it… Will let you know what’s the issue.

@leocache You’re using OpenAI LLM, right?

Yes,I have tried the openai4.0 and 4o-mini. This seems to be a probability problem, the solution I just tried is not working now……

@leocache It seems to be a bug in the CrewAI SDK. I’m in contact with CrewAI staff and will let you know once knowledge starts working.

@leocache The issue was found. There were some changes made to the knowledge feature lately, and the docs are ahead of the SDK currently. A new version of the CrewAI SDK will be released shortly. Then the docs should be synchronized with the SDK, and everything should work fine. I’ll let you know.

1 Like

OK, thank you very much for your answer and look forward to the SDK update.

agent = Agent(

knowledge={
“sources”:[string_source]
}
)
will work. It is mismatch between documentation and source code I guess like @rokbenko said

I am facing the similar issue. I updated to 0.85 version and it still erroring out.

posting this here to see if it helps someone. I am using 0.85 version

def api_assistant(self) → Agent:

	return Agent(
		config=self.agents_config['api_assistant'],
		verbose=True,
		knowledge_sources=[JSONKnowledgeSource(file_path='/api_spec.json', metadata={'source': 'api'})],
	)

also make sure the role of the agent in the yaml file is not too long (65 chars is the limit looks like)

@leocache @sevvaalm As @Krish_Sagili already mentioned, CrewAI SDK 0.85.0 was released 3 hours ago.

Running the following code:

from crewai import Agent, Task, Crew, Process
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

# Create a knowledge source
string_source = StringKnowledgeSource(
    content="Users name is John. He is 30 years old and lives in San Francisco.",
    metadata={"preference": "personal"},
)

# Create an agent
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,
    knowledge_sources=[string_source],  # Add the knowledge source to the agent
)

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

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

Gave me the following output in the terminal:

See the docs:

Hi, does anyone have a working example for PDFknowledgesource please?
Huge thanks!

hi @rokbenko I’m currently working on the “Java Code generation” use case. input is .java file, which has more that 1000 lines of code. Could you please help me to configure the knowledge source.

@Daniel_Ryan @Paarttipaabhalaji Please ask a new question since this one was about knowledge not working at all, which is now solved with the CrewAI SDK update. Please include all the relevant details in the new question.