parent
075f6320e1
commit
593b9b104e
@ -0,0 +1,39 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Import the OpenAIChat model and the Agent struct
|
||||
from swarms import Agent, OpenAIChat, ChromaDB
|
||||
|
||||
# Load the environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Get the API key from the environment
|
||||
api_key = os.environ.get("OPENAI_API_KEY")
|
||||
|
||||
|
||||
# Initilaize the chromadb client
|
||||
chromadb = ChromaDB(
|
||||
metric="cosine",
|
||||
output="results",
|
||||
)
|
||||
|
||||
# Initialize the language model
|
||||
llm = OpenAIChat(
|
||||
temperature=0.5,
|
||||
model_name="gpt-4",
|
||||
openai_api_key=api_key,
|
||||
max_tokens=1000,
|
||||
)
|
||||
|
||||
## Initialize the workflow
|
||||
agent = Agent(
|
||||
llm=llm,
|
||||
max_loops=4,
|
||||
autosave=True,
|
||||
dashboard=True,
|
||||
long_term_memory=ChromaDB(),
|
||||
)
|
||||
|
||||
# Run the workflow on a task
|
||||
agent.run("Generate a 10,000 word blog on health and wellness.")
|
@ -1,69 +1,67 @@
|
||||
import functools
|
||||
import os
|
||||
import logging
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from posthog import Posthog
|
||||
from swarms.telemetry.user_utils import generate_unique_identifier
|
||||
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# # Initialize Posthog client
|
||||
api_key = os.getenv("POSTHOG_API_KEY") or None
|
||||
host = os.getenv("POSTHOG_HOST") or None
|
||||
posthog = Posthog(api_key, host=host)
|
||||
posthog.debug = True
|
||||
|
||||
# return posthog
|
||||
|
||||
|
||||
def log_activity_posthog(event_name: str, **event_properties):
|
||||
"""Log activity to Posthog.
|
||||
|
||||
|
||||
Args:
|
||||
event_name (str): Name of the event to log.
|
||||
**event_properties: Properties of the event to log.
|
||||
|
||||
Examples:
|
||||
>>> from swarms.telemetry.posthog_utils import log_activity_posthog
|
||||
>>> @log_activity_posthog("test_event", test_property="test_value")
|
||||
... def test_function():
|
||||
... print("Hello, world!")
|
||||
>>> test_function()
|
||||
Hello, world!
|
||||
>>> # Check Posthog dashboard for event "test_event" with property
|
||||
>>> # "test_property" set to "test_value".
|
||||
"""
|
||||
|
||||
def decorator_log_activity(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper_log_activity(*args, **kwargs):
|
||||
result = func(*args, **kwargs)
|
||||
|
||||
# Assuming you have a way to get the user id
|
||||
distinct_user_id = generate_unique_identifier()
|
||||
|
||||
# Capture the event
|
||||
posthog.capture(
|
||||
distinct_user_id, event_name, event_properties
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
return wrapper_log_activity
|
||||
|
||||
return decorator_log_activity
|
||||
|
||||
|
||||
# @log_activity_posthog(
|
||||
# "function_executed", function_name="my_function"
|
||||
# )
|
||||
# def my_function():
|
||||
# # Function logic here
|
||||
# return "Function executed successfully!"
|
||||
|
||||
|
||||
# out = my_function()
|
||||
# print(out)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PosthogWrapper:
|
||||
def __init__(
|
||||
self, api_key, instance_address, debug=False, disabled=False
|
||||
):
|
||||
self.posthog = Posthog(api_key, host=instance_address)
|
||||
self.posthog.debug = debug
|
||||
self.posthog.disabled = disabled
|
||||
|
||||
def capture_event(self, distinct_id, event_name, properties=None):
|
||||
self.posthog.capture(distinct_id, event_name, properties)
|
||||
|
||||
def capture_pageview(self, distinct_id, url):
|
||||
self.posthog.capture(
|
||||
distinct_id, "$pageview", {"$current_url": url}
|
||||
)
|
||||
|
||||
def set_user_properties(
|
||||
self, distinct_id, event_name, properties
|
||||
):
|
||||
self.posthog.capture(
|
||||
distinct_id, event=event_name, properties=properties
|
||||
)
|
||||
|
||||
def is_feature_enabled(
|
||||
self, flag_key, distinct_id, send_feature_flag_events=True
|
||||
):
|
||||
return self.posthog.feature_enabled(
|
||||
flag_key, distinct_id, send_feature_flag_events
|
||||
)
|
||||
|
||||
def get_feature_flag_payload(self, flag_key, distinct_id):
|
||||
return self.posthog.get_feature_flag_payload(
|
||||
flag_key, distinct_id
|
||||
)
|
||||
|
||||
def get_feature_flag(self, flag_key, distinct_id):
|
||||
return self.posthog.get_feature_flag(flag_key, distinct_id)
|
||||
|
||||
def capture_with_feature_flag(
|
||||
self, distinct_id, event_name, flag_key, variant_key
|
||||
):
|
||||
self.posthog.capture(
|
||||
distinct_id,
|
||||
event_name,
|
||||
{"$feature/{}".format(flag_key): variant_key},
|
||||
)
|
||||
|
||||
def capture_with_feature_flags(
|
||||
self, distinct_id, event_name, send_feature_flags=True
|
||||
):
|
||||
self.posthog.capture(
|
||||
distinct_id,
|
||||
event_name,
|
||||
send_feature_flags=send_feature_flags,
|
||||
)
|
||||
|
Loading…
Reference in new issue