mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 15:48:29 +00:00
fix: Improve knowledge context formatting for better LLM effectiveness
- Make knowledge context more explicit and instructive for LLM - Add clear instructions to use provided context - Add comprehensive test coverage for knowledge utils Fixes #2269 Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -2,11 +2,19 @@ from typing import Any, Dict, List
|
||||
|
||||
|
||||
def extract_knowledge_context(knowledge_snippets: List[Dict[str, Any]]) -> str:
|
||||
"""Extract knowledge from the task prompt."""
|
||||
"""Extract knowledge from the task prompt and format it for effective LLM usage."""
|
||||
valid_snippets = [
|
||||
result["context"]
|
||||
for result in knowledge_snippets
|
||||
if result and result.get("context")
|
||||
]
|
||||
if not valid_snippets:
|
||||
return ""
|
||||
|
||||
snippet = "\n".join(valid_snippets)
|
||||
return f"Additional Information: {snippet}" if valid_snippets else ""
|
||||
return (
|
||||
"Important Context (You MUST use this information to complete your task "
|
||||
"accurately and effectively):\n"
|
||||
f"{snippet}\n\n"
|
||||
"Make sure to incorporate the above context into your response."
|
||||
)
|
||||
|
||||
39
tests/knowledge/test_knowledge_utils.py
Normal file
39
tests/knowledge/test_knowledge_utils.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Test knowledge utils functionality."""
|
||||
|
||||
from typing import Dict, List, Any
|
||||
|
||||
import pytest
|
||||
|
||||
from crewai.knowledge.utils.knowledge_utils import extract_knowledge_context
|
||||
|
||||
|
||||
def test_extract_knowledge_context_with_valid_snippets():
|
||||
"""Test extracting knowledge context with valid snippets."""
|
||||
snippets = [
|
||||
{"context": "Fact 1: The sky is blue", "score": 0.9},
|
||||
{"context": "Fact 2: Water is wet", "score": 0.8},
|
||||
]
|
||||
result = extract_knowledge_context(snippets)
|
||||
expected = "Additional Information: Fact 1: The sky is blue\nFact 2: Water is wet"
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_extract_knowledge_context_with_empty_snippets():
|
||||
"""Test extracting knowledge context with empty snippets."""
|
||||
snippets: List[Dict[str, Any]] = []
|
||||
result = extract_knowledge_context(snippets)
|
||||
assert result == ""
|
||||
|
||||
|
||||
def test_extract_knowledge_context_with_none_snippets():
|
||||
"""Test extracting knowledge context with None snippets."""
|
||||
snippets = [None, {"context": "Valid context"}] # type: ignore
|
||||
result = extract_knowledge_context(snippets)
|
||||
assert result == "Additional Information: Valid context"
|
||||
|
||||
|
||||
def test_extract_knowledge_context_with_missing_context():
|
||||
"""Test extracting knowledge context with missing context."""
|
||||
snippets = [{"score": 0.9}, {"context": "Valid context"}]
|
||||
result = extract_knowledge_context(snippets)
|
||||
assert result == "Additional Information: Valid context"
|
||||
Reference in New Issue
Block a user