We will demonstrate how to create an AI agent using the latest technology. Nomic Google Gemini and embeddings. The architecture is designed from scratch, with semantic memory, context reasoning and multi-agent coordination integrated into one intelligent framework. Using LangChain Faiss and LangChain Nomic, our agents are equipped with the capability to store, retrieve and reason information using natural-language queries. Our goal is to build an AI system which can be modularized and extended in order to support friendly conversation and both research and analysis.
!pip install -qU langchain-nomic langchain-core langchain-community langchain-google-genai faiss-cpu numpy matplotlib
Import os
import getpass
Numpy can be imported as np
Type import List, Dict Any, Optional
Dataclasses can be imported from other dataclasses
NomicEmbeddings imported by langchain_nomic
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_core.documents import Document
from langchain_google_genai import ChatGoogleGenerativeAI
Import json
If not, os.getenv ("NOMIC_API_KEY"):
os.environ["NOMIC_API_KEY"] = getpass.getpass("Enter your Nomic API key: ")
If not, os.getenv ("GOOGLE_API_KEY"):
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google API key (for Gemini): ")
We begin by installing all the required libraries, including langchain-nomic, langchain-google-genai, and faiss-cpu, to support our agent’s embedding, reasoning, and vector search capabilities. Then, we import all the required modules. We also set up our Nomic API and Google API key using getpass. This ensures smooth integration. Check out the full Codes.
@dataclass
class AgentMemory
"""Agent's episodic and semantic memory"""
Episode List[Dict[str, Any]]
semantic: Dict[str, Any]
working: Dict[str, Any]
class IntelligentAgent:
"""Advanced AI Agent with Nomic Embeddings for semantic reasoning"""
def __init__(self, agent_name: str = "AIAgent"The personality of the person: "helpful"):
self.name = agent_name
Self.personality is the same as personality
self.embeddings = NomicEmbeddings(
model="nomic-embed-text-v1.5",
dimensionality=384,
inference_mode="remote"
)
self.llm = ChatGoogleGenerativeAI(
model="gemini-1.5-flash",
temperature=0.7,
max_tokens=512
)
self.memory = AgentMemory(
episodic=[],
semantic={},
working={}
)
self.knowledge_base is None
self.vector_store = None
self.capabilities = {
"reasoning": True,
"memory_retrieval": True,
"knowledge_search": True,
"context_awareness": True,
"learning": True
}
print(f"🤖 {self.name} initialized with Nomic embeddings + Gemini LLM")
Def add_knowledge() (self, list of documents)[str]List[Dict] = None):
"""Add knowledge to agent's semantic memory"""
If metadata is None
These are the keywords: [{"source": f"doc_{i}"} for i in range(len(documents))]
Docs = [Document(page_content=doc, metadata=meta)
for doc, meta in zip(documents, metadata)]
If self.vector_store = None
self.vector_store = InMemoryVectorStore.from_documents(docs, self.embeddings)
else:
self.vector_store.add_documents(docs)
print(f"📚 Added {len(documents)} documents to knowledge base")
def remember_interaction(self, user_input: str, agent_response: str, context: Dict = None):
"""Store interaction in episodic memory"""
memory_entry = {
"timestamp": len(self.memory.episodic),
"user_input": user_input,
"agent_response": agent_response,
"context": context or {},
"embedding": self.embeddings.embed_query(f"{user_input} {agent_response}")
}
self.memory.episodic.append(memory_entry)
def retrieve_similar_memories(self, query: str, k: int = 3) -> List[Dict]:
"""Retrieve similar past interactions"""
if not self.memory.episodic:
Return to the Homepage []
query_embedding = self.embeddings.embed_query(query)
Similarities = []
for memory in self.memory.episodic:
similarity = np.dot(query_embedding, memory["embedding"])
similarities.append((similarity, memory))
similarities.sort(reverse=True, key=lambda x: x[0])
Return to the Homepage [mem for _, mem in similarities[:k]]
def search_knowledge(self, query: str, k: int = 3) -> List[Document]:
"""Search knowledge base for relevant information"""
If self.vector_store = None
Return to the Homepage []
return self.vector_store.similarity_search(query, k=k)
def reason_and_respond(self, user_input: str) -> str:
"""Main reasoning pipeline with context integration"""
similar_memories = self.retrieve_similar_memories(user_input, k=2)
relevant_docs = self.search_knowledge(user_input, k=3)
context = {
"similar_memories": similar_memories,
"relevant_knowledge": [doc.page_content for doc in relevant_docs],
"working_memory": self.memory.working
}
response = self._generate_contextual_response(user_input, context)
self.remember_interaction(user_input, response, context)
self.memory.working["last_query"] = user_input
self.memory.working["last_response"] = Response
Return Response
def _generate_contextual_response(self, query: str, context: Dict) -> str:
"""Generate response using Gemini LLM with context"""
context_info = ""
If you are not sure, then check the context["relevant_knowledge"]:
context_info = f"Relevant Knowledge: {' '.join(context['relevant_knowledge'][:2])}n"
If you are not sure, then check the context["similar_memories"]:
Remember = context["similar_memories"][0]
context_info = f"Similar Past Interaction: User asked '{memory['user_input']}', I responded '{memory['agent_response'][:100]}...'n"
prompt = f"""You are {self.name}, an AI agent with personality: {self.personality}.
Details about the context:
{context_info}
User Query: {query}
Please respond with a thoughtful response, based upon the context. Maintain your personality and keep the response short (under 100 words)."""
try:
response = self.llm.invoke(prompt)
return response.content.strip()
Except Exception As e.
If context["relevant_knowledge"]:
knowledge_summary = " ".join(context["relevant_knowledge"][:2])
The return of f"Based on my knowledge: {knowledge_summary[:200]}..."
Elif context["similar_memories"]:
Last_memory = Context["similar_memories"][0]
The return of f"I recall a similar question. Previously: {last_memory['agent_response'][:150]}..."
else:
You can return to your original language by clicking here. "I need more information to provide a comprehensive answer."
By creating a memory model that emulates episodic recall and semantic retrieval, we define the structure of our agent. Gemini LLM is used to create contextual and personality-driven answers. We use Nomic embeddings, which provide semantic understanding. We enable our agent to learn and interact with each other intelligently by integrating built-in features like knowledge searching, memory retrieval and reasoning. Check out the full Codes.
class ResearchAgent(IntelligentAgent):
"""Specialized agent for research and analysis tasks"""
def __init__(self):
You can also check out our supersized().__init__("ResearchBot", "analytical and thorough")
self.research_domains = []
def analyze_topic(self, topic: str) -> Dict[str, Any]:
"""Analyze a topic using semantic similarity and Gemini reasoning"""
related_docs = self.search_knowledge(topic, k=5)
If not related_documentsReturn
return {"analysis": "No relevant information found", "confidence": 0.0}
topic_embedding = self.embeddings.embed_query(topic)
doc_embeddings = [self.embeddings.embed_query(doc.page_content)
for doc in related_docs]
Similarities = [np.dot(topic_embedding, doc_emb)
for doc_emb in doc_embeddings]
For example, context " ".join([doc.page_content for doc in related_docs[:3]])
Analysis_prompt=f"""As a research analyst, analyze the topic: {topic}
Information available:
{context}
Include in your analysis:
1. The Key Insights (2-3 Points)
2. Confidence levels assessment
3. Researchers may have research limitations or gaps
4. Considerations for Practical Application
Please limit your response to 200 words.= analysis"""
try:
gemini_analysis = self.llm.invoke(analysis_prompt)
detailed_analysis = gemini_analysis.content.strip()
except:
detailed_analysis = f"Analysis of {topic} based on available documents with {len(related_docs)} relevant sources."
analysis = {
"topic": topic,
"related_documents": len(related_docs),
"max_similarity": max(similarities),
"avg_similarity": np.mean(similarities),
"key_insights": [doc.page_content[:100] + "..." Related_docs for the doc you are looking for[:3]],
"confidence": max(similarities),
"detailed_analysis": detailed_analysis
}
Return analysis
class ConversationalAgent(IntelligentAgent):
"""Agent optimized for natural conversations"""
def __init__(self):
You can also check out our supersized().__init__("ChatBot", "friendly and engaging")
self.conversation_history = []
def maintain_conversation_context(self, user_input: str) -> str:
"""Maintain conversation flow with context awareness"""
self.conversation_history.append({"role": "user", "content": user_input})
recent_context = " ".join([msg["content"] for msg in self.conversation_history[-3:]])
response = self.reason_and_respond(recent_context)
self.conversation_history.append({"role": "assistant", "content": response})
Return Response
Our intelligent agent is extended into two versions, a ResearchAgent to perform structured topic analyses and a ConversationalAgent that can engage in natural dialog. The research agent combines Gemini LLM with semantic similarity in order to provide confident, insightful analyses. Meanwhile, the conversational agent provides a chat-like experience with a sense of coherence and engagement. We can tailor AI behavior to specific user requirements using this modular approach. Check out the full Codes.
def demonstrate_agent_capabilities():
"""Comprehensive demonstration of agent capabilities"""
print("🎯 Creating and testing AI agents...")
ResearchAgent()
chat_agent is the Conversational Agent()
knowledge_documents = [
"Artificial intelligence is transforming industries through automation and intelligent decision-making systems.",
"Machine learning algorithms require large datasets to identify patterns and make predictions.",
"Natural language processing enables computers to understand and generate human language.",
"Computer vision allows machines to interpret and analyze visual information from images and videos.",
"Robotics combines AI with physical systems to create autonomous machines.",
"Deep learning uses neural networks with multiple layers to solve complex problems.",
"Reinforcement learning teaches agents to make decisions through trial and error.",
"Quantum computing promises to solve certain problems exponentially faster than classical computers."
]
research_agent.add_knowledge(knowledge_documents)
chat_agent.add_knowledge(knowledge_documents)
print("n🔬 Testing Research Agent...")
Topics = ["machine learning", "robotics", "quantum computing"]
For topics:
analysis = research_agent.analyze_topic(topic)
print(f"n📊 Analysis of '{topic}':")
print(f" Confidence: {analysis['confidence']:.3f}")
print(f" Related docs: {analysis['related_documents']}")
print(f" Detailed Analysis: {analysis.get('detailed_analysis', 'N/A')[:200]}...")
print(f" Key insight: {analysis['key_insights'][0] if analysis['key_insights'] else 'None'}")
print("n💬 Testing Conversational Agent...")
conversation_inputs = [
"Tell me about artificial intelligence",
"How does machine learning work?",
"What's the difference between AI and machine learning?",
"Can you explain neural networks?"
]
For user_input into conversation_inputs
response = chat_agent.maintain_conversation_context(user_input)
print(f"n👤 User: {user_input}")
print(f"🤖 Agent: {response}")
print("n🧠 Memory Analysis...")
print(f"Research Agent memories: {len(research_agent.memory.episodic)}")
print(f"Chat Agent memories: {len(chat_agent.memory.episodic)}")
similar_memories = chat_agent.retrieve_similar_memories("artificial intelligence", k=2)
Similar_memories
print(f"n🔍 Similar memory found:")
print(f" Query: {similar_memories[0]['user_input']}")
print(f" Response: {similar_memories[0]['agent_response'][:100]}...")
Our AI agents are evaluated by evaluating their performance on both conversational and research tasks. We test the ResearchAgent’s ability to generate insightful analyses on key topics and validate the ConversationalAgent’s performance across multi-turn queries. We confirm through introspection that agents can retrieve and retain relevant interactions. Check out the full Codes.
MultiAgentSystem class:
"""Orchestrate multiple specialized agents"""
def __init__(self):
self.agents = {
"research"ResearchAgent(),
"chat"ConversationalAgent()
}
self.coordinator_embeddings = NomicEmbeddings(model="nomic-embed-text-v1.5", dimensionality=256)
def route_query(self, query: str) -> str:
"""Route query to most appropriate agent"""
agent_descriptions = {
"research": "analysis, research, data, statistics, technical information",
"chat": "conversation, questions, general discussion, casual talk"
}
query_embedding = self.coordinator_embeddings.embed_query(query)
best_agent = "chat"
best_similarity = 0
for agent_name, description in agent_descriptions.items():
desc_embedding = self.coordinator_embeddings.embed_query(description)
similarity = np.dot(query_embedding, desc_embedding)
if similarity > best_similarity:
The best_similarity is similarity
best_agent = agent_name
return best_agent
def process_query(self, query: str) -> Dict[str, Any]:
"""Process query through appropriate agent"""
selected_agent, confidence = self.route_query_with_confidence(query)
Self-agents = Agents[selected_agent]
If selected_agent== "research":
If you want to know more about if "analyze" in query.lower() The following are some examples of how to use "research" in query.lower():
topic = query.replace("analyze", "").replace("research", "").strip()
result = agent.analyze_topic(topic)
"Response = F"Return"Research Analysis: {result.get('detailed_analysis', str(result))}"
else:
response = agent.reason_and_respond(query)
else:
response = agent.maintain_conversation_context(query)
return {
"query": query,
"selected_agent": selected_agent,
"response": response,
"confidence"?????????????????????????????????????????????????????????????????????????????????????????????
}
def route_query_with_confidence(self, query: str) -> tuple[str, float]:
"""Route query to most appropriate agent and return confidence"""
agent_descriptions = {
"research": "analysis, research, data, statistics, technical information",
"chat": "conversation, questions, general discussion, casual talk"
}
query_embedding = self.coordinator_embeddings.embed_query(query)
best_agent = "chat"
best_similarity = 0.0
for agent_name, description in agent_descriptions.items():
desc_embedding = self.coordinator_embeddings.embed_query(description)
similarity = np.dot(query_embedding, desc_embedding)
if similarity > best_similarity:
The best_similarity is similarity
best_agent = agent_name
return best_agent, best_similarity
The multi-agent we built intelligently routed queries to the agent that is best suited for research, or one who can converse with users based upon semantic similarity. By using Nomic embedded embeddings for both user queries and agent specialties, we can ensure that each request is handled by the expert who has the greatest relevance. This architecture allows intelligent behavior to scale while maintaining precision and specialization. Check out the full Codes.
If the __name__ equals "__main__":
print("n🚀 Advanced AI Agent System with Nomic Embeddings + Gemini LLM")
print("=" * 70)
print("💡 Note: This uses Google's Gemini 1.5 Flash (free tier) for reasoning")
print("📚 Get your free Google API key at: https://makersuite.google.com/app/apikey")
print("🎯 Get your Nomic API key at: https://atlas.nomic.ai/")
print("=" * 70)
demonstrate_agent_capabilities()
print("n🎛️ Testing Multi-Agent System...")
MultiAgentSystem()
knowledge_docs = [
"Python is a versatile programming language used in AI development.",
"TensorFlow and PyTorch are popular machine learning frameworks.",
"Data preprocessing is crucial for successful machine learning projects."
]
for agent in multi_system.agents.values():
agent.add_knowledge(knowledge_docs)
test_queries = [
"Analyze the impact of AI on society",
"How are you doing today?",
"Research machine learning trends",
"What's your favorite color?"
]
Test_queries for query:
result = multi_system.process_query(query)
print(f"n📝 Query: {query}")
print(f"🎯 Routed to: {result['selected_agent']} agent")
print(f"💬 Response: {result['response'][:150]}...")
print("n✅ Advanced AI Agent demonstration complete!")
In order to conclude, we run a complete demonstration of our AI System, initializing agents, loading knowledge and testing real world queries. Our modular system is demonstrated by the way the multi-agent intelligently routes every query according to its content. The agents are able to demonstrate their reasoning abilities, as well as memory and adaptive response generation, in this final execution.
We have created a flexible and powerful AI agent framework using Gemini LLM and Nomic embeddings to generate contextual responses. The agents are shown to be able to independently retrieve knowledge and make intelligent decisions. They also demonstrate the ability of the system as a whole, which allows them the freedom of managing memory. In both the research-focused interaction and in conversational settings, we demonstrate how this set up can provide a solid foundation to create AI assistants with real intelligence and responsiveness.
Click here to find out more Codes. This research is the work of researchers. Also, feel free to follow us on Twitter Don’t forget about our 100k+ ML SubReddit Subscribe now our Newsletter.
Asif Razzaq, CEO of Marktechpost Media Inc. is a visionary engineer and entrepreneur who is dedicated to harnessing Artificial Intelligence’s potential for the social good. Marktechpost is his latest venture, a media platform that focuses on Artificial Intelligence. It is known for providing in-depth news coverage about machine learning, deep learning, and other topics. The content is technically accurate and easy to understand by an audience of all backgrounds. Over 2 million views per month are a testament to the platform’s popularity.


