feat(api): emit Langfuse generation traces for folder indexer
This commit is contained in:
@@ -12,6 +12,7 @@ from docx import Document as DocxDocument
|
|||||||
from app.core.langfuse_client import (
|
from app.core.langfuse_client import (
|
||||||
compile_prompt,
|
compile_prompt,
|
||||||
extract_usage,
|
extract_usage,
|
||||||
|
get_langfuse,
|
||||||
get_prompt_or_fallback,
|
get_prompt_or_fallback,
|
||||||
)
|
)
|
||||||
from app.core.llm import get_llm
|
from app.core.llm import get_llm
|
||||||
@@ -55,7 +56,7 @@ async def _llm_vision(messages: list) -> object:
|
|||||||
return await llm.ainvoke(messages)
|
return await llm.ainvoke(messages)
|
||||||
|
|
||||||
|
|
||||||
async def summarize_image(*, image_b64: str, mime: str) -> IndexResult:
|
async def summarize_image(*, image_b64: str, mime: str, file_name: str | None = None) -> IndexResult:
|
||||||
"""Return a compact summary of an image file using vision.
|
"""Return a compact summary of an image file using vision.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@@ -64,6 +65,8 @@ async def summarize_image(*, image_b64: str, mime: str) -> IndexResult:
|
|||||||
Base64-encoded image bytes.
|
Base64-encoded image bytes.
|
||||||
mime:
|
mime:
|
||||||
MIME type of the image, e.g. ``"image/png"``.
|
MIME type of the image, e.g. ``"image/png"``.
|
||||||
|
file_name:
|
||||||
|
Optional file name, attached to the Langfuse trace as input metadata.
|
||||||
"""
|
"""
|
||||||
template, prompt_obj = get_prompt_or_fallback("folder_file_summary_image", _IMAGE_FALLBACK)
|
template, prompt_obj = get_prompt_or_fallback("folder_file_summary_image", _IMAGE_FALLBACK)
|
||||||
messages = [
|
messages = [
|
||||||
@@ -73,8 +76,21 @@ async def summarize_image(*, image_b64: str, mime: str) -> IndexResult:
|
|||||||
{"type": "image_url", "image_url": {"url": f"data:{mime};base64,{image_b64}"}},
|
{"type": "image_url", "image_url": {"url": f"data:{mime};base64,{image_b64}"}},
|
||||||
]),
|
]),
|
||||||
]
|
]
|
||||||
response = await _llm_vision(messages)
|
lf = get_langfuse()
|
||||||
usage = extract_usage(response)
|
if lf is not None:
|
||||||
|
with lf.start_as_current_observation(
|
||||||
|
as_type="generation",
|
||||||
|
name="folder-summarize-image",
|
||||||
|
model="gpt-4o-mini",
|
||||||
|
prompt=prompt_obj,
|
||||||
|
input={"file_name": file_name, "mime": mime},
|
||||||
|
) as gen:
|
||||||
|
response = await _llm_vision(messages)
|
||||||
|
usage = extract_usage(response)
|
||||||
|
gen.update(output=response.content, usage_details=usage)
|
||||||
|
else:
|
||||||
|
response = await _llm_vision(messages)
|
||||||
|
usage = extract_usage(response)
|
||||||
summary = (response.content or "").strip()[:500]
|
summary = (response.content or "").strip()[:500]
|
||||||
return IndexResult(summary=summary, tokens_used=usage.get("total", 0))
|
return IndexResult(summary=summary, tokens_used=usage.get("total", 0))
|
||||||
|
|
||||||
@@ -98,8 +114,21 @@ async def summarize_text(*, content: str, ext: str, name: str) -> IndexResult:
|
|||||||
SystemMessage(content=compiled),
|
SystemMessage(content=compiled),
|
||||||
HumanMessage(content="Summarise this file."),
|
HumanMessage(content="Summarise this file."),
|
||||||
]
|
]
|
||||||
response = await _llm_text(messages)
|
lf = get_langfuse()
|
||||||
usage = extract_usage(response)
|
if lf is not None:
|
||||||
|
with lf.start_as_current_observation(
|
||||||
|
as_type="generation",
|
||||||
|
name="folder-summarize-text",
|
||||||
|
model="gpt-4o-mini",
|
||||||
|
prompt=prompt_obj,
|
||||||
|
input={"file_name": name, "ext": ext, "content_chars": len(truncated)},
|
||||||
|
) as gen:
|
||||||
|
response = await _llm_text(messages)
|
||||||
|
usage = extract_usage(response)
|
||||||
|
gen.update(output=response.content, usage_details=usage)
|
||||||
|
else:
|
||||||
|
response = await _llm_text(messages)
|
||||||
|
usage = extract_usage(response)
|
||||||
summary = (response.content or "").strip()[:500]
|
summary = (response.content or "").strip()[:500]
|
||||||
return IndexResult(summary=summary, tokens_used=usage.get("total", 0))
|
return IndexResult(summary=summary, tokens_used=usage.get("total", 0))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user