I run the DocumentKnowledgeBase Sample Code,But fail

I run the DocumentKnowledgeBase Sample Code,But fail

I change model into deepseek, and not use the environment api-key , so i change the code like this:

from agno.agent import Agent
from agno.document.base import Document
from agno.knowledge.document import DocumentKnowledgeBase
from agno.vectordb.pgvector import PgVector
from agno.models.deepseek import DeepSeek
from config.aiconfig import MODEL_NAME, OPENAI_API_KEY, OPENAI_BASE_URL

fun_facts = """
- Earth is the third planet from the Sun and the only known astronomical object to support life.
- Approximately 71% of Earth's surface is covered by water, with the Pacific Ocean being the largest.
- The Earth's atmosphere is composed mainly of nitrogen (78%) and oxygen (21%), with traces of other gases.
- Earth rotates on its axis once every 24 hours, leading to the cycle of day and night.
- The planet has one natural satellite, the Moon, which influences tides and stabilizes Earth's axial tilt.
- Earth's tectonic plates are constantly shifting, leading to geological activities like earthquakes and volcanic eruptions.
- The highest point on Earth is Mount Everest, standing at 8,848 meters (29,029 feet) above sea level.
- The deepest part of the ocean is the Mariana Trench, reaching depths of over 11,000 meters (36,000 feet).
- Earth has a diverse range of ecosystems, from rainforests and deserts to coral reefs and tundras.
- The planet's magnetic field protects life by deflecting harmful solar radiation and cosmic rays.
"""

# Load documents from the data/docs directory
documents = [Document(content=fun_facts)]


# Database connection URL
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# Create a knowledge base with the loaded documents
knowledge_base = DocumentKnowledgeBase(
    documents=documents,
    vector_db=PgVector(
        table_name="documents",
        db_url=db_url,
    ),
    
)

# Load the knowledge base
knowledge_base.load(recreate=False)

# Create an agent with the knowledge base
agent = Agent(
    knowledge=knowledge_base,
    model= DeepSeek(
    id=MODEL_NAME,
    api_key=OPENAI_API_KEY,
    base_url=OPENAI_BASE_URL,
    max_tokens = 4096
    ),
)




# Ask the agent about the knowledge base
agent.print_response(
    "Ask me about something from the knowledge base about earth", markdown=True
)

But it can’t find the API_KEY and fail to add the knowledge

PS E:\AgentDemo\AgentWarDemo> & C:/Users/Admin/AppData/Local/Programs/Python/Python312/python.exe e:/AgentDemo/AgentWarDemo/agenttest.py
INFO Embedder not provided, using OpenAIEmbedder as default.
INFO Loading knowledge base
ERROR    Error processing document 'None': The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\agno\vectordb\pgvector\pgvector.py:321: SAWarning: Column 'ai.documents.id' is marked as a member of the primary key for table 'ai.documents', but has no Python-side or server-side default generator indicated, nor does it indicate 'autoincrement=True' or 'nullable=True', and no explicit value is passed.  Primary key columns typically may not store NULL.
  sess.execute(insert_stmt, batch_records)
ERROR    Error with batch starting at index 0: (psycopg.errors.NotNullViolation) null value in column "id" of relation "documents" violates not-null constraint
         DETAIL:  Failing row contains (null, null, {}, {}, null, null, null, 2025-03-31 13:03:39.06541+00, null, null).
         [SQL: INSERT INTO ai.documents DEFAULT VALUES]
         (Background on this error at: https://sqlalche.me/e/20/gkpj)
ERROR    Error inserting documents: (psycopg.errors.NotNullViolation) null value in column "id" of relation "documents" violates not-null constraint
         DETAIL:  Failing row contains (null, null, {}, {}, null, null, null, 2025-03-31 13:03:39.06541+00, null, null).
         [SQL: INSERT INTO ai.documents DEFAULT VALUES]
         (Background on this error at: https://sqlalche.me/e/20/gkpj)
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\default.py", line 945, in do_execute
    cursor.execute(statement, parameters)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\psycopg\cursor.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.errors.NotNullViolation: null value in column "id" of relation "documents" violates not-null constraint
DETAIL:  Failing row contains (null, null, {}, {}, null, null, null, 2025-03-31 13:03:39.06541+00, null, null).

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "e:\AgentDemo\AgentWarDemo\agenttest.py", line 38, in <module>
    knowledge_base.load(recreate=False)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\agno\knowledge\agent.py", line 133, in load
    self.vector_db.insert(documents=documents_to_load, filters=filters)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\agno\vectordb\pgvector\pgvector.py", line 321, in insert
    sess.execute(insert_stmt, batch_records)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\session.py", line 2365, in execute
    return self._execute_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\session.py", line 2260, in _execute_internal
    result = conn.execute(
             ^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1416, in execute
    return meth(
           ^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\sql\elements.py", line 523, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1638, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1843, in _execute_context
    return self._exec_single_context(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1983, in _exec_single_context
    self._handle_dbapi_exception(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 2352, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\engine\default.py", line 945, in do_execute
    cursor.execute(statement, parameters)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\psycopg\cursor.py", line 97, in execute
    raise ex.with_traceback(None)
sqlalchemy.exc.IntegrityError: (psycopg.errors.NotNullViolation) null value in column "id" of relation "documents" violates not-null constraint
DETAIL:  Failing row contains (null, null, {}, {}, null, null, null, 2025-03-31 13:03:39.06541+00, null, null).
[SQL: INSERT INTO ai.documents DEFAULT VALUES]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

Hi @komiya
Thanks for reaching out and for using Agno! I’ve looped in the right engineers to help with your question. We usually respond within 48 hours, but if this is urgent, just let us know, and we’ll do our best to prioritize it.
Appreciate your patience—we’ll get back to you soon!

Hey @komiya I am little confused with this configuration of model:

DeepSeek(
    id=MODEL_NAME,
    api_key=OPENAI_API_KEY,
    base_url=OPENAI_BASE_URL,
    max_tokens = 4096
    )

Are you sure the api key being passed is right? It seems like you’re using OPENAI_API_KEY: api_key=OPENAI_API_KEY

If you’re using OpenAI format please check this out: OpenAI Like - Agno

The model itself has no issues; I’ve used the same parameters to build models in other projects, and they’ve worked fine. What I’m actually more concerned about is the issue of data not being able to be inserted.

Yes you’re right @komiya - document insertion in a vector db expects an Embedder - if nothing is specified it uses OpenAIEmbedder as default and expects an api key for OPENAI_API_KEY and hence the error:

ERROR    Error processing document 'None': The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

You can ready more about it here: OpenAI Embedder - Agno

It seems that Embedder does not have a version corresponding to DeepSeek?

Yes you’re right - but as far as I know = DeepSeek currently does not offer any embeddings models . Please correct me if I am wrong.

1 Like

if I use DeepSeek’s model, I won’t be able to use a knowledge base? Or do I need to prepare an OpenAI API to support my use of a knowledge base?

You can use different models for the knowledge base and the agent. The OpenAI API key is the default if no other provider is specified for either the knowledge base or the agent. So, yes, you should be able to use DeepSeek’s model for the agent and a different model for the knowledge base (possibly using OpenAI or any other embedder), provided both have the correct model and API keys passed for the ones you want to use.

ok,thanks,I get it

Blockquote