Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt committed Nov 16, 2024
1 parent 31f2550 commit f9b0048
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 75 deletions.
92 changes: 20 additions & 72 deletions lunary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ def track_event(
metadata=None,
params=None,
runtime=None,
app_id=None, # should only be used by Langchain
app_id=None,
api_url=None,
callback_queue=None,
):
try:
config = get_config()
langchain_app_id = app_id
custom_app_id = app_id
project_id = app_id or config.app_id # used to generate a unique run_id
api_url = api_url or config.api_url

Expand Down Expand Up @@ -153,7 +153,7 @@ def track_event(
"metadata": metadata,
"params": params,
"templateId": template_id,
"appId": langchain_app_id,
"appId": custom_app_id, # should only be set when a custom app_id is provided, otherwise the app_id is set in consumer.py
}

if callback_queue is not None:
Expand Down Expand Up @@ -360,6 +360,7 @@ def wrap(
tags=None,
input_parser=default_input_parser,
output_parser=default_output_parser,
app_id=None,
):
def sync_wrapper(*args, **kwargs):
output = None
Expand Down Expand Up @@ -392,7 +393,8 @@ def sync_wrapper(*args, **kwargs):
tags=kwargs.pop("tags", None) or tags or tags_ctx.get(),
template_id=kwargs.get("extra_headers", {}).get(
"Template-Id", None
)
),
app_id=app_id,
)
except Exception as e:
logging.exception(e)
Expand All @@ -411,6 +413,7 @@ def sync_wrapper(*args, **kwargs):
"error",
run.id,
error={"message": str(e), "stack": traceback.format_exc()},
app_id=app_id,
)

# rethrow error
Expand All @@ -429,6 +432,7 @@ def sync_wrapper(*args, **kwargs):
], # Need name in case need to compute tokens usage server side
output=parsed_output["output"],
token_usage=parsed_output["tokensUsage"],
app_id=app_id
)
return output
except Exception as e:
Expand All @@ -450,6 +454,7 @@ def async_wrap(
tags=None,
input_parser=default_input_parser,
output_parser=default_output_parser,
app_id=None,
):
async def wrapper(*args, **kwargs):
async def async_wrapper(*args, **kwargs):
Expand Down Expand Up @@ -483,6 +488,7 @@ async def async_wrapper(*args, **kwargs):
template_id=kwargs.get("extra_headers", {}).get(
"Template-Id", None
),
app_id=app_id,
)
except Exception as e:
logger.exception(e)
Expand All @@ -496,6 +502,7 @@ async def async_wrapper(*args, **kwargs):
"error",
run.id,
error={"message": str(e), "stack": traceback.format_exc()},
app_id=app_id,
)

# rethrow error
Expand All @@ -514,6 +521,7 @@ async def async_wrapper(*args, **kwargs):
], # Need name in case need to compute tokens usage server side
output=parsed_output["output"],
token_usage=parsed_output["tokensUsage"],
app_id=app_id
)
return output
except Exception as e:
Expand Down Expand Up @@ -552,6 +560,7 @@ def async_stream_wrapper(*args, **kwargs):
template_id=kwargs.get("extra_headers", {}).get(
"Template-Id", None
),
app_id=app_id
)
except Exception as e:
logger.exception(e)
Expand Down Expand Up @@ -622,7 +631,7 @@ def monitor(object):
logging.info("The `openai` package is not installed")


def agent(name=None, user_id=None, user_props=None, tags=None):
def agent(name=None, user_id=None, user_props=None, tags=None, app_id=None):
def decorator(fn):
return wrap(
fn,
Expand All @@ -632,12 +641,13 @@ def decorator(fn):
user_props=user_props,
tags=tags,
input_parser=default_input_parser,
app_id=app_id
)

return decorator


def chain(name=None, user_id=None, user_props=None, tags=None):
def chain(name=None, user_id=None, user_props=None, tags=None, app_id=None):
def decorator(fn):
return wrap(
fn,
Expand All @@ -647,12 +657,13 @@ def decorator(fn):
user_props=user_props,
tags=tags,
input_parser=default_input_parser,
app_id=app_id
)

return decorator


def tool(name=None, user_id=None, user_props=None, tags=None):
def tool(name=None, user_id=None, user_props=None, tags=None, app_id=None):
def decorator(fn):
return wrap(
fn,
Expand All @@ -662,6 +673,7 @@ def decorator(fn):
user_props=user_props,
tags=tags,
input_parser=default_input_parser,
app_id=app_id
)

return decorator
Expand Down Expand Up @@ -1450,11 +1462,8 @@ def open_thread(id: Optional[str] = None, tags: Optional[List[str]] = None, app_
ThreadError: If there's any error creating or connecting to the thread
"""
try:
if app_id:
logger.warning("The `app_id` parameter is deprecated. Please use the `lunary.config` object to set the app ID.")

config = get_config()
token = config.app_id
token = app_id or config.app_id

if not token:
raise ThreadError("API token is required")
Expand Down Expand Up @@ -1506,12 +1515,6 @@ def get_raw_template(slug: str, app_id: str | None = None, api_url: str | None =
TemplateError: If fetching the template fails.
"""
try:
if app_id:
logger.warning("The `app_id` parameter is deprecated. Please use the `lunary.config` object to set the app ID.")

if api_url:
logger.warning("The `api_url` parameter is deprecated. Please use the `lunary.config` object to set the API URL.")

config = get_config()
token = app_id or config.app_id
base_url = api_url or config.api_url
Expand Down Expand Up @@ -1565,12 +1568,6 @@ async def get_raw_template_async(slug: str, app_id: str | None = None, api_url:
TemplateError: If fetching the template fails.
"""
try:
if app_id:
logger.warning("The `app_id` parameter is deprecated. Please use the `lunary.config` object to set the app ID.")

if api_url:
logger.warning("The `api_url` parameter is deprecated. Please use the `lunary.config` object to set the API URL.")

config = get_config()
token = app_id or config.app_id
api_url = api_url or config.api_url
Expand Down Expand Up @@ -1623,12 +1620,6 @@ def render_template(slug: str, data={}, app_id: str | None = None, api_url: str
TemplateError: If rendering fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

raw_template = get_raw_template(slug, app_id, api_url)

if raw_template.get("message") == "Template not found, is the project ID correct?":
Expand Down Expand Up @@ -1671,12 +1662,6 @@ async def render_template_async(slug: str, data={}, app_id: str | None = None, a
TemplateError: If rendering fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

raw_template = await get_raw_template_async(slug, app_id, api_url)

if raw_template.get("message") == "Template not found, is the project ID correct?":
Expand Down Expand Up @@ -1717,12 +1702,6 @@ def get_langchain_template(slug: str, app_id: str | None = None, api_url: str |
TemplateError: If creating the LangChain template fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

from langchain_core.prompts import ChatPromptTemplate, PromptTemplate

raw_template = get_raw_template(slug, app_id, api_url)
Expand Down Expand Up @@ -1772,12 +1751,6 @@ async def get_langchain_template_async(slug: str, app_id: str | None = None, api
TemplateError: If creating the LangChain template fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

from langchain_core.prompts import ChatPromptTemplate, PromptTemplate

raw_template = await get_raw_template_async(slug, app_id, api_url)
Expand Down Expand Up @@ -1825,12 +1798,6 @@ def get_live_templates(app_id: str | None = None, api_url: str | None = None):
TemplateError: If fetching templates fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

config = get_config()
token = app_id or config.app_id
api_url = api_url or config.api_url
Expand Down Expand Up @@ -1876,13 +1843,6 @@ def get_dataset(slug: str, app_id: str | None = None, api_url: str | None = None
DatasetError: If fetching the dataset fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")


config = get_config()
token = app_id or config.app_id
api_url = api_url or config.api_url
Expand Down Expand Up @@ -1919,12 +1879,6 @@ def score(run_id: str, label: str, value: int | float | str | bool, comment: str
ScoringError: If scoring fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

config = get_config()
token = app_id or config.app_id
api_url = api_url or config.api_url
Expand Down Expand Up @@ -1987,12 +1941,6 @@ def evaluate(
EvaluationError: If evaluation fails.
"""
try:
if app_id:
logger.warning("the `app_id` parameter is deprecated. please use the `lunary.config` object to set the app id.")

if api_url:
logger.warning("the `api_url` parameter is deprecated. please use the `lunary.config` object to set the api url.")

config = get_config()
token = app_id or config.app_id
api_url = api_url or config.api_url
Expand Down
13 changes: 11 additions & 2 deletions lunary/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ def __init__(
user_id: str | None = None,
user_props: dict | None = None,
id: str | None = None,
tags: List[str] | None = None
tags: List[str] | None = None,
app_id: str | None = None,
api_url: str | None = None,
):
self.id = id or str(uuid.uuid4())
self.user_id = user_id
self.user_props = user_props
self.tags = tags
self._track_event = track_event
self.app_id = app_id
self.api_url = api_url

def track_message(
self, message: Message, user_id=None, user_props=None, feedback=None
Expand All @@ -39,7 +43,9 @@ def track_message(
parent_run_id=self.id,
thread_tags=self.tags,
feedback=feedback,
message=message
message=message,
app_id=self.app_id,
api_url=self.api_url
)
return run_id

Expand All @@ -62,4 +68,7 @@ def track_event(self, event_name: str, user_id: str | None = None, user_props: d
user_props=user_props or self.user_props,
parent_run_id=self.id,
thread_tags=self.tags,
app_id=self.app_id,
api_url=self.api_url,
metadata=metadata,
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "lunary"
version = "1.1.14"
version = "1.2.1"
description = "Observability, analytics and evaluations for AI agents and chatbots."
authors = ["lunary <hello@lunary.ai>"]
readme = "README.md"
Expand Down

0 comments on commit f9b0048

Please sign in to comment.