phidatahq / phidata

Build AI Agents with memory, knowledge, tools and reasoning. Chat with them using a beautiful Agent UI.
https://docs.phidata.com
Mozilla Public License 2.0
15.58k stars 2.14k forks source link

Question with Knowledge Base #1464

Closed benedictdebrah closed 6 days ago

benedictdebrah commented 6 days ago

``

  from phi.knowledge.json import JSONKnowledgeBase
  from langchain_openai import OpenAIEmbeddings
  from phi.agent import Agent
  from phi.model.groq import Groq
  from phi.vectordb.chroma import ChromaDb
  import os 

  json_data_dir = "./api_data"  
  chroma_db_dir = "./vector_db"

  ##fixing it 
  class CustomOpenAIEmbedder:
      def __init__(self, model="text-embedding-3-small"):
          self.embedder = OpenAIEmbeddings(model=model)

      def get_embedding_and_usage(self, content):
          # Check if content is a single document or list and use appropriate method
          if isinstance(content, str):
              embedding = self.embedder.embed_query(content)
          elif isinstance(content, list):
              embedding = self.embedder.embed_documents(content)
          else:
              raise ValueError("Content must be a string or list of strings.")

          usage = {"tokens_used": len(content.split())} if isinstance(content, str) else {"tokens_used": sum(len(doc.split()) for doc in content)}

          return embedding, usage

  embeddings = CustomOpenAIEmbedder(model="text-embedding-3-small")
  knowledge_base = JSONKnowledgeBase(
      path=json_data_dir,
      vector_db=ChromaDb(
          collection="test",
          embedder=embeddings,
          path=chroma_db_dir,
          persistent_client=True
      ),
  )

  agent = Agent(model=Groq(id="llama3-groq-70b-8192-tool-use-preview"),
      description="You are  an advanced soccer chatbot "
      "with expertise in soccer statistics, analysis, and advice. "
      "You assist users with a deep understanding of soccer, offering insights into match predictions, "
      "player performance, team comparisons, and historical data.",
      knowledge_base=knowledge_base, 
      search_knowledge=True,
      add_references_to_prompt=True,
      use_tools=True,
      show_tool_calls=True,
      debug_mode=True,
  )

  knowledge_base.load(
      recreate=False,
      skip_existing=True
  )

  agent.print_response("tell me what you have in the knowledge base")

``

So i am trying to create a knowledge base with chroma DB there were some issues with the normal embedding function in Phi so i had to create a custom one with the help of the Phi embedding class. The embedder works fine now but the agent is unable to access the knowledge base which contains information.

My question here is

  1. Am i doing it correctly?
  2. Why is the agent failing to access the knowledge base?