Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple fixes #116

Merged
merged 8 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions langfuse/api/resources/commons/types/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class Observation(pydantic.BaseModel):
completion_start_time: typing.Optional[dt.datetime] = pydantic.Field(alias="completionStartTime")
model: typing.Optional[str]
model_parameters: typing.Optional[typing.Dict[str, MapValue]] = pydantic.Field(alias="modelParameters")
prompt: typing.Optional[typing.Any]
input: typing.Optional[typing.Any]
version: typing.Optional[str]
metadata: typing.Optional[typing.Any]
completion: typing.Optional[str]
output: typing.Optional[typing.Any]
prompt_tokens: int = pydantic.Field(alias="promptTokens")
completion_tokens: int = pydantic.Field(alias="completionTokens")
total_tokens: int = pydantic.Field(alias="totalTokens")
Expand Down
10 changes: 3 additions & 7 deletions langfuse/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def __init__(
self,
public_key: Optional[str] = None,
secret_key: Optional[str] = None,
host: str = "https://cloud.langfuse.com",
host: str = None,
debug: bool = False,
statefulClient: Optional[Union[StatefulTraceClient, StatefulSpanClient]] = None,
release: Optional[str] = None,
) -> None:
# If we're provided a stateful trace client directly
prioritized_public_key = public_key if public_key else os.environ.get("LANGFUSE_PUBLIC_KEY")
prioritized_secret_key = secret_key if secret_key else os.environ.get("LANGFUSE_SECRET_KEY")
prioritized_host = host if host else os.environ.get("LANGFUSE_HOST")
prioritized_host = host if host else os.environ.get("LANGFUSE_HOST", "https://cloud.langfuse.com")

if debug:
# Ensures that debug level messages are logged when debug mode is on.
Expand Down Expand Up @@ -542,11 +542,7 @@ def on_llm_end(

extracted_response = (
last_response.text
if last_response.generation_info is None
or (
"finish_reason" not in last_response.generation_info
or last_response.generation_info["finish_reason"] != "function_call"
)
if last_response.text is not None and last_response.text != ""
else str(last_response.message.additional_kwargs)
)

Expand Down
90 changes: 2 additions & 88 deletions tests/test_core_sdk.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import os
from datetime import datetime

import pytest

from langfuse import Langfuse
from langfuse.api.client import FintoLangfuse
from langfuse.model import (
CreateEvent,
CreateGeneration,
Expand All @@ -18,37 +14,7 @@

from langfuse.task_manager import TaskStatus
from tests.api_wrapper import LangfuseAPI
from tests.utils import create_uuid


def test_langfuse_release():
# Backup environment variables to restore them later
backup_environ = os.environ.copy()

# Clearing the environment variables
os.environ.clear()

# These key are required
client = Langfuse(public_key="test", secret_key="test")
assert client.release is None

# If neither the LANGFUSE_RELEASE env var nor the release parameter is given,
# it should fall back to get_common_release_envs
os.environ["CIRCLE_SHA1"] = "mock-sha1"
client = Langfuse(public_key="test", secret_key="test")
assert client.release == "mock-sha1"

# If LANGFUSE_RELEASE env var is set, it should take precedence
os.environ["LANGFUSE_RELEASE"] = "mock-langfuse-release"
client = Langfuse(public_key="test", secret_key="test")
assert client.release == "mock-langfuse-release"

# If the release parameter is given during initialization, it should take the highest precedence
client = Langfuse(public_key="test", secret_key="test", release="parameter-release")
assert client.release == "parameter-release"

# Restoring the environment variables
os.environ.update(backup_environ)
from tests.utils import create_uuid, get_api


def test_flush():
Expand All @@ -67,54 +33,6 @@ def test_flush():
assert langfuse.task_manager.queue.empty()


def test_setup_without_keys():
public_key, secret_key, host = (
os.environ["LANGFUSE_PUBLIC_KEY"],
os.environ["LANGFUSE_SECRET_KEY"],
os.environ["LANGFUSE_HOST"],
)
os.environ.pop("LANGFUSE_PUBLIC_KEY")
os.environ.pop("LANGFUSE_SECRET_KEY")
os.environ.pop("LANGFUSE_HOST")
with pytest.raises(ValueError):
Langfuse()

os.environ["LANGFUSE_PUBLIC_KEY"] = public_key
os.environ["LANGFUSE_SECRET_KEY"] = secret_key
os.environ["LANGFUSE_HOST"] = host


def test_setup_without_pk():
public_key = os.environ["LANGFUSE_PUBLIC_KEY"]
os.environ.pop("LANGFUSE_PUBLIC_KEY")
with pytest.raises(ValueError):
Langfuse()
os.environ["LANGFUSE_PUBLIC_KEY"] = public_key


def test_setup_without_sk():
secret_key = os.environ["LANGFUSE_SECRET_KEY"]
os.environ.pop("LANGFUSE_SECRET_KEY")
with pytest.raises(ValueError):
Langfuse()
os.environ["LANGFUSE_SECRET_KEY"] = secret_key


def test_public_key_in_header():
langfuse = Langfuse(public_key="test_LANGFUSE_PUBLIC_KEY")
assert langfuse.client.x_langfuse_public_key == "test_LANGFUSE_PUBLIC_KEY"


def test_secret_key_in_header():
langfuse = Langfuse(secret_key="test_LANGFUSE_SECRET_KEY")
assert langfuse.client._password == "test_LANGFUSE_SECRET_KEY"


def test_host_in_header():
langfuse = Langfuse(host="http://localhost:8000/")
assert langfuse.client._environment == "http://localhost:8000/"


def test_shutdown():
langfuse = Langfuse(debug=True)

Expand Down Expand Up @@ -477,11 +395,7 @@ def test_create_trace_and_event():


def test_create_span_and_generation():
api = FintoLangfuse(
username=os.environ.get("LANGFUSE_PUBLIC_KEY"),
password=os.environ.get("LANGFUSE_SECRET_KEY"),
environment=os.environ.get("LANGFUSE_HOST"),
)
api = get_api()

langfuse = Langfuse(debug=True)

Expand Down
8 changes: 2 additions & 6 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from langfuse.model import CreateDatasetRequest


from tests.utils import create_uuid
from tests.utils import create_uuid, get_api


def test_create_and_get_dataset():
Expand Down Expand Up @@ -100,11 +100,7 @@ def test_langchain_dataset():
assert len(run.dataset_run_items) == 1
assert run.dataset_run_items[0].dataset_run_id == run.id

api = FintoLangfuse(
username=os.environ.get("LANGFUSE_PUBLIC_KEY"),
password=os.environ.get("LANGFUSE_SECRET_KEY"),
environment=os.environ.get("LANGFUSE_HOST"),
)
api = get_api()
api.generations.get()
trace = api.trace.get(handler.get_trace_id())

Expand Down
Loading
Loading