This will help you get started with the Cognee retriever. For detailed documentation of all CogneeRetriever features and configurations head to the API reference.
For cognee default setup, only thing you need is your OpenAI API key.If you want to get automated tracing from individual queries, you can also set your LangSmith API key by uncommenting below:
Copy
Ask AI
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")# os.environ["LANGSMITH_TRACING"] = "true"
Add some documents, process them, and then run queries. Cognee retrieves relevant knowledge to your queries and generates final answers.
Copy
Ask AI
# Example of adding and processing documentsfrom langchain_core.documents import Documentdocs = [ Document(page_content="Elon Musk is the CEO of SpaceX."), Document(page_content="SpaceX focuses on rockets and space travel."),]retriever.add_documents(docs)retriever.process_data()# Now let's query the retrieverquery = "Tell me about Elon Musk"results = retriever.invoke(query)for idx, doc in enumerate(results, start=1): print(f"Doc {idx}: {doc.page_content}")
Like other retrievers, CogneeRetriever can be incorporated into LLM applications via chains.We will need a LLM or chat model:
Copy
Ask AI
from langchain_openai import ChatOpenAIllm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
Copy
Ask AI
from langchain_cognee import CogneeRetrieverfrom langchain_core.documents import Documentfrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import RunnablePassthrough# Instantiate the retriever with your Cognee configretriever = CogneeRetriever(llm_api_key="sk-", dataset_name="my_dataset", k=3)# Optionally, prune/reset the dataset for a clean slateretriever.prune()# Add some documentsdocs = [ Document(page_content="Elon Musk is the CEO of SpaceX."), Document(page_content="SpaceX focuses on space travel."),]retriever.add_documents(docs)retriever.process_data()prompt = ChatPromptTemplate.from_template( """Answer the question based only on the context provided.Context: {context}Question: {question}""")def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs)chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser())