mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-08 01:08:39 -10:00
* fix(examples): resolve syntax errors and align with consolidated-params API * fix: correct TypeError in 3 example files by using proper parameter names - Fix MemoryConfig(memory=...) → user_id=... in agent_centric_api.py - Fix Session(memory=...) → user_id=... in session-example.py - Fix build_context_for_task(memory=...) → user_id=... in memory-quality-example.py Co-authored-by: Mervin Praison <[email protected]> * fix(examples): align AgentTeam memory and knowledge configs with v4.6.52 API * fix(examples): phase-3 API alignment for consolidated params on v4.6.52 * fix(examples): phase-4 API alignment for 40 validation failures on v4.6.52 * fix: resolve critical RouterAgent **kwargs bug and add missing if __name__ guards - Fixed TypeError in RouterAgent._build_system_prompt() by removing **kwargs from super() call - Added if __name__ == '__main__' guards to 6 example files to prevent auto-execution on import - Addresses Gemini Code Assist review feedback Co-authored-by: Mervin Praison <[email protected]> * fix: resolve missing AgentTeam import and RouterAgent kwargs forwarding - Fix missing AgentTeam import in self-reflection-optimization-patterns.py - Forward kwargs properly in RouterAgent._build_system_prompt to super() - Addresses critical issues identified by Gemini code reviewer Co-authored-by: Mervin Praison <[email protected]> * fix(examples): remove merge-conflict residue for PR #1893 --------- Co-authored-by: Dhivya-Bharathy <[email protected]> Co-authored-by: praisonai-triage-agent[bot] <272766704+praisonai-triage-agent[bot]@users.noreply.github.com> Co-authored-by: Mervin Praison <[email protected]> Co-authored-by: Mervin Praison <[email protected]>
152 lines
5.3 KiB
Python
152 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Example: Using Gemini Embedding Models with PraisonAI Memory
|
|
|
|
This example demonstrates how to configure PraisonAI to use Google's Gemini
|
|
embedding models for memory storage and retrieval, as requested in issue #870.
|
|
|
|
Supported Gemini embedding models:
|
|
- gemini-embedding-exp-03-07 (experimental)
|
|
- text-embedding-004 (stable)
|
|
|
|
Prerequisites:
|
|
1. Install praisonaiagents with memory support:
|
|
pip install "praisonaiagents[memory]"
|
|
|
|
2. Set your Google API key:
|
|
export GOOGLE_API_KEY='your-api-key'
|
|
|
|
3. Ensure litellm is installed (comes with praisonaiagents)
|
|
"""
|
|
|
|
import os
|
|
from praisonaiagents import Agent, Task, AgentTeam
|
|
|
|
def main():
|
|
"""Demonstrate Gemini embedding usage"""
|
|
|
|
# Verify Google API key
|
|
if not os.environ.get("GOOGLE_API_KEY"):
|
|
print("Error: GOOGLE_API_KEY environment variable not set")
|
|
print("Please set it with: export GOOGLE_API_KEY='your-api-key'")
|
|
return
|
|
|
|
print("=== Gemini Embedding Example ===\n")
|
|
|
|
# Create agents for different tasks
|
|
researcher = Agent(
|
|
name="Research Agent",
|
|
role="Information Researcher",
|
|
goal="Research and store important information about topics",
|
|
backstory="Expert at analyzing and documenting information with semantic understanding",
|
|
llm="gpt-4o-mini" # Using GPT for chat, Gemini for embeddings
|
|
)
|
|
|
|
retriever = Agent(
|
|
name="Retrieval Agent",
|
|
role="Information Retriever",
|
|
goal="Retrieve relevant information from memory using semantic search",
|
|
backstory="Specialist in finding and presenting stored information",
|
|
llm="gpt-4o-mini"
|
|
)
|
|
|
|
# Task 1: Store information with semantic meaning
|
|
store_task = Task(
|
|
description="""
|
|
Research and store the following information:
|
|
1. Gemini is Google's family of multimodal AI models
|
|
2. Gemini models support text, image, video, and audio understanding
|
|
3. The embedding models provide semantic representations of text
|
|
4. Gemini Ultra is the largest and most capable model
|
|
5. Gemini was announced in December 2023
|
|
|
|
Store each fact with its semantic meaning preserved.
|
|
""",
|
|
expected_output="Confirmation that all facts have been stored in memory",
|
|
agent=researcher
|
|
)
|
|
|
|
# Task 2: Semantic retrieval test
|
|
retrieve_task = Task(
|
|
description="""
|
|
Using semantic search, find information about:
|
|
1. Google's AI capabilities
|
|
2. Multimodal understanding features
|
|
3. When was Google's latest AI announced
|
|
|
|
Note: Use semantic similarity, not exact keyword matching.
|
|
""",
|
|
expected_output="Retrieved information based on semantic meaning",
|
|
agent=retriever
|
|
)
|
|
|
|
# Configure with Gemini embeddings
|
|
# Option 1: Using stable text-embedding-004
|
|
agents = AgentTeam(
|
|
agents=[researcher, retriever],
|
|
tasks=[store_task, retrieve_task],
|
|
memory={
|
|
"embedder": {
|
|
"provider": "gemini",
|
|
"config": {
|
|
"model": "text-embedding-004",
|
|
}
|
|
},
|
|
"config": {"provider": "sqlite"},
|
|
}
|
|
)
|
|
|
|
# Alternative: Using experimental model with advanced features
|
|
# agents = AgentTeam(
|
|
# agents=[researcher, retriever],
|
|
# tasks=[store_task, retrieve_task],
|
|
#
|
|
# memory=True,
|
|
# embedder={
|
|
# "provider": "gemini",
|
|
# "config": {
|
|
# "model": "gemini-embedding-exp-03-07",
|
|
# # Task types: SEMANTIC_SIMILARITY, CLASSIFICATION, CLUSTERING,
|
|
# # RETRIEVAL_DOCUMENT, RETRIEVAL_QUERY, QUESTION_ANSWERING,
|
|
# # FACT_VERIFICATION
|
|
# "task_type": "RETRIEVAL_DOCUMENT"
|
|
# }
|
|
# }
|
|
# )
|
|
|
|
# Execute the workflow
|
|
print("Starting agents with Gemini embeddings...\n")
|
|
result = agents.start()
|
|
|
|
# Demonstrate direct memory access with Gemini embeddings
|
|
print("\n=== Direct Memory Search Example ===\n")
|
|
|
|
if agents.shared_memory:
|
|
# Search queries that test semantic understanding
|
|
queries = [
|
|
"What are Google's latest AI innovations?",
|
|
"Explain multimodal AI capabilities",
|
|
"Large language model features"
|
|
]
|
|
|
|
for query in queries:
|
|
print(f"\nSearching for: '{query}'")
|
|
results = agents.shared_memory.search_long_term(query, limit=3)
|
|
|
|
if results:
|
|
print(f"Found {len(results)} semantically similar results:")
|
|
for i, result in enumerate(results, 1):
|
|
print(f"\n{i}. Relevance Score: {result.get('score', 0):.3f}")
|
|
print(f" Content: {result.get('text', '')[:150]}...")
|
|
else:
|
|
print("No results found")
|
|
|
|
print("\n=== Example Complete ===")
|
|
print("\nKey Points:")
|
|
print("1. Gemini embeddings provide semantic understanding of text")
|
|
print("2. Results are based on meaning, not just keyword matching")
|
|
print("3. Different task types can optimize embedding quality")
|
|
print("4. Compatible with LiteLLM for easy integration")
|
|
|
|
if __name__ == "__main__":
|
|
main() |