Skip to content

Commit 71c717b

Browse files
committed
add langchain test
1 parent ea186e4 commit 71c717b

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

tests/test_langchain.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from langfuse.callback import CallbackHandler
3737
from langfuse.client import Langfuse
3838
from tests.api_wrapper import LangfuseAPI
39-
from tests.utils import create_uuid, get_api
39+
from tests.utils import create_uuid, encode_file_to_base64, get_api
4040

4141

4242
def test_callback_init():
@@ -2210,3 +2210,39 @@ def _generate_random_dict(n: int, key_length: int = 8) -> Dict[str, Any]:
22102210
print(f"Full execution took {duration_full}ms")
22112211

22122212
assert duration_full > 1000, "Full execution should take longer than 1 second"
2213+
2214+
2215+
def test_multimodal():
2216+
api = get_api()
2217+
handler = CallbackHandler()
2218+
model = ChatOpenAI(model="gpt-4o-mini")
2219+
2220+
image_data = encode_file_to_base64("static/puton.jpg")
2221+
2222+
message = HumanMessage(
2223+
content=[
2224+
{"type": "text", "text": "What's in this image?"},
2225+
{
2226+
"type": "image_url",
2227+
"image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
2228+
},
2229+
],
2230+
)
2231+
2232+
response = model.invoke([message], config={"callbacks": [handler]})
2233+
2234+
print(response.content)
2235+
2236+
handler.flush()
2237+
2238+
trace = api.trace.get(handler.get_trace_id())
2239+
2240+
assert len(trace.observations) == 1
2241+
assert trace.observations[0].type == "GENERATION"
2242+
2243+
print(trace.observations[0].input)
2244+
2245+
assert (
2246+
"@@@langfuseMedia:type=image/jpeg|id="
2247+
in trace.observations[0].input[0]["content"][1]["image_url"]["url"]
2248+
)

tests/test_openai.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import base64
21
import os
32

43
import pytest
@@ -14,7 +13,7 @@
1413
_is_openai_v1,
1514
openai,
1615
)
17-
from tests.utils import create_uuid, get_api
16+
from tests.utils import create_uuid, encode_file_to_base64, get_api
1817

1918
chat_func = (
2019
openai.chat.completions.create if _is_openai_v1() else openai.ChatCompletion.create
@@ -1482,7 +1481,7 @@ def test_base_64_image_input():
14821481
content_path = "static/puton.jpg"
14831482
content_type = "image/jpeg"
14841483

1485-
base64_image = encode_file(content_path)
1484+
base64_image = encode_file_to_base64(content_path)
14861485

14871486
client.chat.completions.create(
14881487
name=generation_name,
@@ -1532,7 +1531,7 @@ def test_audio_input_and_output():
15321531
generation_name = "test_audio_input_and_output" + create_uuid()[:8]
15331532

15341533
content_path = "static/joke_prompt.wav"
1535-
base64_string = encode_file(content_path)
1534+
base64_string = encode_file_to_base64(content_path)
15361535

15371536
client.chat.completions.create(
15381537
name=generation_name,
@@ -1580,8 +1579,3 @@ def test_audio_input_and_output():
15801579
"@@@langfuseMedia:type=audio/wav|id="
15811580
in generation.data[0].output["content"][1]["output_audio"]["data"]
15821581
)
1583-
1584-
1585-
def encode_file(image_path):
1586-
with open(image_path, "rb") as file:
1587-
return base64.b64encode(file.read()).decode("utf-8")

tests/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import os
23
import typing
34
from uuid import uuid4
@@ -7,18 +8,17 @@
78
except ImportError:
89
import pydantic # type: ignore
910

10-
from langfuse.api.client import FernLangfuse
11-
1211
from llama_index.core import (
1312
Settings,
14-
VectorStoreIndex,
1513
SimpleDirectoryReader,
16-
load_index_from_storage,
1714
StorageContext,
15+
VectorStoreIndex,
16+
load_index_from_storage,
1817
)
19-
2018
from llama_index.core.callbacks import CallbackManager
2119

20+
from langfuse.api.client import FernLangfuse
21+
2222

2323
def create_uuid():
2424
return str(uuid4())
@@ -106,3 +106,8 @@ def get_llama_index_index(callback, force_rebuild: bool = False):
106106
index = load_index_from_storage(storage_context)
107107

108108
return index
109+
110+
111+
def encode_file_to_base64(image_path) -> str:
112+
with open(image_path, "rb") as file:
113+
return base64.b64encode(file.read()).decode("utf-8")

0 commit comments

Comments
 (0)