Skip to content

Commit

Permalink
add-azureembedder-cookbook-example-phi-1918
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Nov 20, 2024
1 parent aa4ecaf commit 9a90183
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 133 deletions.
19 changes: 19 additions & 0 deletions cookbook/embedders/azure_embedder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.azure_openai import AzureOpenAIEmbedder

embeddings = AzureOpenAIEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="azure_openai_embeddings",
embedder=AzureOpenAIEmbedder(),
),
num_documents=2,
)
26 changes: 9 additions & 17 deletions cookbook/embedders/fireworks_embedder.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import os

from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.fireworks import FireworksEmbedder

# Create knowledge base
embeddings = FireworksEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="fireworks_embeddings",
embedder=FireworksEmbedder(
api_key=os.getenv("FIREWORKS_API_KEY"),
dimensions=768,
),
embedder=FireworksEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
26 changes: 9 additions & 17 deletions cookbook/embedders/gemini_embedder.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import os

from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.google import GeminiEmbedder

# Create knowledge base
embeddings = GeminiEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="gemini_embeddings",
embedder=GeminiEmbedder(
api_key=os.getenv("GOOGLE_API_KEY"),
dimensions=768,
),
embedder=GeminiEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
19 changes: 19 additions & 0 deletions cookbook/embedders/huggingface_embedder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.huggingface import HuggingfaceCustomEmbedder

embeddings = HuggingfaceCustomEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="huggingface_embeddings",
embedder=HuggingfaceCustomEmbedder(),
),
num_documents=2,
)
25 changes: 9 additions & 16 deletions cookbook/embedders/mistral_embedder.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import os
from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.mistral import MistralEmbedder

# Create knowledge base
embeddings = MistralEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="mistral_embeddings",
embedder=MistralEmbedder(
api_key=os.getenv("MISTRAL_API_KEY"),
dimensions=1024,
),
embedder=MistralEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
24 changes: 9 additions & 15 deletions cookbook/embedders/ollama_embedder.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.ollama import OllamaEmbedder

# Create knowledge base
embeddings = OllamaEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="ollama_embeddings",
embedder=OllamaEmbedder(
model="openhermes",
dimensions=4096,
),
embedder=OllamaEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
26 changes: 9 additions & 17 deletions cookbook/embedders/openai_embedder.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import os

from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.openai import OpenAIEmbedder

# Create knowledge base
embeddings = OpenAIEmbedder().get_embedding("Embed me")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="openai_embeddings",
embedder=OpenAIEmbedder(
api_key=os.getenv("OPENAI_API_KEY"),
dimensions=1536,
),
embedder=OpenAIEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
29 changes: 14 additions & 15 deletions cookbook/embedders/qdrant_fastembed.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.fastembed import FastEmbedEmbedder
from typing import List

documents: List[str] = [
"This classic spaghetti carbonara combines perfectly cooked al dente pasta",
"with crispy pancetta, which adds a savory crunch and depth of flavor,",
"creamy eggs that create a luscious sauce when mixed with the hot pasta,",
"and a generous sprinkle of freshly grated Parmesan cheese",
"for a comforting, flavorful dish that's sure to impress any pasta lover.",
"Finish with a dash of black pepper and a garnish of parsley for a touch of freshness.",
]
embeddings = FastEmbedEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

"""FastEmbed supported models can be found here: https://qdrant.github.io/fastembed/examples/Supported_Models/"""
# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

embedder = FastEmbedEmbedder(
# model="BAAI/bge-small-en-v1.5"
# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="qdrant_embeddings",
embedder=FastEmbedEmbedder(),
),
num_documents=2,
)

embeddings = embedder.get_embedding("\n".join(documents))
print(embeddings[0])
19 changes: 19 additions & 0 deletions cookbook/embedders/sentence_transformer_embedder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.sentence_transformer import SentenceTransformerEmbedder

embeddings = SentenceTransformerEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="sentence_transformer_embeddings",
embedder=SentenceTransformerEmbedder(),
),
num_documents=2,
)
26 changes: 9 additions & 17 deletions cookbook/embedders/together_embedder.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import os

from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.together import TogetherEmbedder

# Create knowledge base
embeddings = TogetherEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="together_embeddings",
embedder=TogetherEmbedder(
api_key=os.getenv("TOGETHER_API_KEY"),
dimensions=768,
),
embedder=TogetherEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
26 changes: 9 additions & 17 deletions cookbook/embedders/voyageai_embedder.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import os

from phi.agent import Agent, AgentKnowledge
from phi.agent import AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.voyageai import VoyageAIEmbedder

# Create knowledge base
embeddings = VoyageAIEmbedder().get_embedding("The quick brown fox jumps over the lazy dog.")

# Print the embeddings and their dimensions
print(f"Embeddings: {embeddings[:5]}")
print(f"Dimensions: {len(embeddings)}")

# Example usage:
knowledge_base = AgentKnowledge(
vector_db=PgVector(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
table_name="voyageai_embeddings",
embedder=VoyageAIEmbedder(
api_key=os.getenv("VOYAGE_API_KEY"),
dimensions=1024,
),
embedder=VoyageAIEmbedder(),
),
num_documents=2,
)

# Add information to the knowledge base
knowledge_base.load_text(
"This classic spaghetti carbonara combines perfectly cooked al dente pasta "
"with crispy pancetta and creamy eggs that create a luscious sauce."
)

# Add the knowledge base to the Agent
agent = Agent(knowledge_base=knowledge_base)
8 changes: 6 additions & 2 deletions phi/embedder/huggingface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
from os import getenv
from typing import Any, Dict, List, Optional, Tuple

from phi.embedder.base import Embedder
Expand All @@ -14,7 +16,7 @@ class HuggingfaceCustomEmbedder(Embedder):
"""Huggingface Custom Embedder"""

model: str = "jinaai/jina-embeddings-v2-base-code"
api_key: Optional[str] = None
api_key: Optional[str] = getenv("HUGGINGFACE_API_KEY")
client_params: Optional[Dict[str, Any]] = None
huggingface_client: Optional[InferenceClient] = None

Expand All @@ -39,7 +41,9 @@ def _response(self, text: str):
def get_embedding(self, text: str) -> List[float]:
response = self._response(text=text)
try:
return response
decoded_string = response.decode("utf-8")
return json.loads(decoded_string)

except Exception as e:
logger.warning(e)
return []
Expand Down

0 comments on commit 9a90183

Please sign in to comment.