Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

llm-call

Max Deichmann82bf15d0a946
diff --git a/enterprise/Readme.md b/ee/Readme.md
similarity index 100%
rename from enterprise/Readme.md
rename to ee/Readme.md
diff --git a/fern/api/definition/span.yml b/fern/api/definition/span.yml
index 586d0c1970..f586ef0432 100644
--- a/fern/api/definition/span.yml
+++ b/fern/api/definition/span.yml
@@ -4,6 +4,12 @@ service:
auth: false
base-path: /api
endpoints:
+ createLLMCall:
+ docs: a
+ method: POST
+ path: /llm-span
+ request: CreateLLMSpanRequest
+ response: Span
create:
docs: Add a span to the database
method: POST
@@ -25,10 +31,12 @@ types:
startTime: datetime
attributes: unknown
parentObservationId: optional<string>
+
UpdateSpanRequest:
properties:
spanId: string
endTime: datetime
+
Span:
properties:
id: string
@@ -39,3 +47,23 @@ types:
endTime: optional<datetime>
attributes: unknown
parentObservationId: optional<string>
+
+ CreateLLMSpanRequest:
+ properties:
+ traceId: string
+ name: string
+ startTime: datetime
+ attributes: LLMAttributes
+ parentObservationId: optional<string>
+
+ LLMAttributes:
+ properties:
+ model: unknown
+ prompt: optional<string>
+ completion: optional<string>
+ tokens: LLMTokens
+
+ LLMTokens:
+ properties:
+ prompt_amount: optional<integer>
+ completion_amount: optional<integer>
diff --git a/fern/api/definition/trace.yml b/fern/api/definition/trace.yml
index 768e95666a..d481593d87 100644
--- a/fern/api/definition/trace.yml
+++ b/fern/api/definition/trace.yml
@@ -26,7 +26,7 @@ types:
statusMessage: optional<string>
UpdateTraceRequest:
properties:
- traceId: string
+ id: string
status: string
statusMessage: optional<string>
Trace:
diff --git a/fern/api/generators.yml b/fern/api/generators.yml
index be18a202f9..27c501c8fb 100644
--- a/fern/api/generators.yml
+++ b/fern/api/generators.yml
@@ -8,13 +8,19 @@ groups:
location: npm
url: npm.buildwithfern.com
package-name: "@finto-fern/api-client"
+ config:
+ namespaceExport: Langfuse
- name: fernapi/fern-python-sdk
version: 0.1.0
output:
location: local-file-system
path: ../../generated/python
+ config:
+ namespaceExport: Langfuse
- name: fernapi/fern-openapi
version: 0.0.26
output:
location: local-file-system
path: ../../generated/openapi
+ config:
+ namespaceExport: Langfuse
diff --git a/generated/openapi/openapi.yml b/generated/openapi/openapi.yml
index 91e048b51e..6f04b922e7 100644
--- a/generated/openapi/openapi.yml
+++ b/generated/openapi/openapi.yml
@@ -43,6 +43,26 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/CreateScoreRequest'
+ /api/llm-span:
+ post:
+ description: a
+ operationId: span_createLLMCall
+ tags:
+ - Span
+ parameters: []
+ responses:
+ '200':
+ description: ''
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Span'
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/CreateLLMSpanRequest'
/api/spans:
post:
description: Add a span to the database
@@ -262,6 +282,48 @@ components:
- name
- startTime
- attributes
+ CreateLLMSpanRequest:
+ title: CreateLLMSpanRequest
+ type: object
+ properties:
+ traceId:
+ type: string
+ name:
+ type: string
+ startTime:
+ type: string
+ format: date-time
+ attributes:
+ $ref: '#/components/schemas/LLMAttributes'
+ parentObservationId:
+ type: string
+ required:
+ - traceId
+ - name
+ - startTime
+ - attributes
+ LLMAttributes:
+ title: LLMAttributes
+ type: object
+ properties:
+ model: {}
+ prompt:
+ type: string
+ completion:
+ type: string
+ tokens:
+ $ref: '#/components/schemas/LLMTokens'
+ required:
+ - model
+ - tokens
+ LLMTokens:
+ title: LLMTokens
+ type: object
+ properties:
+ prompt_amount:
+ type: integer
+ completion_amount:
+ type: integer
CreateTraceRequest:
title: CreateTraceRequest
type: object
@@ -281,14 +343,14 @@ components:
title: UpdateTraceRequest
type: object
properties:
- traceId:
+ id:
type: string
status:
type: string
statusMessage:
type: string
required:
- - traceId
+ - id
- status
Trace:
title: Trace
diff --git a/generated/python/__init__.py b/generated/python/__init__.py
index d1d3c1347e..430bafd428 100644
--- a/generated/python/__init__.py
+++ b/generated/python/__init__.py
@@ -2,10 +2,13 @@
from .resources import (
CreateEventRequest,
+ CreateLLMSpanRequest,
CreateScoreRequest,
CreateSpanRequest,
CreateTraceRequest,
Event,
+ LLMAttributes,
+ LLMTokens,
Score,
Span,
Trace,
@@ -19,10 +22,13 @@ from .resources import (
__all__ = [
"CreateEventRequest",
+ "CreateLLMSpanRequest",
"CreateScoreRequest",
"CreateSpanRequest",
"CreateTraceRequest",
"Event",
+ "LLMAttributes",
+ "LLMTokens",
"Score",
"Span",
"Trace",
diff --git a/generated/python/resources/__init__.py b/generated/python/resources/__init__.py
index 16d60c5027..2f7a0ac78f 100644
--- a/generated/python/resources/__init__.py
+++ b/generated/python/resources/__init__.py
@@ -3,15 +3,18 @@
from . import event, score, span, trace
from .event import CreateEventRequest, Event
from .score import CreateScoreRequest, Score
-from .span import CreateSpanRequest, Span, UpdateSpanRequest
+from .span import CreateLLMSpanRequest, CreateSpanRequest, LLMAttributes, LLMTokens, Span, UpdateSpanRequest
from .trace import CreateTraceRequest, Trace, UpdateTraceRequest
__all__ = [
"CreateEventRequest",
+ "CreateLLMSpanRequest",
"CreateScoreRequest",
"CreateSpanRequest",
"CreateTraceRequest",
"Event",
+ "LLMAttributes",
+ "LLMTokens",
"Score",
"Span",
"Trace",
diff --git a/generated/python/resources/span/__init__.py b/generated/python/resources/span/__init__.py
index 5d19538d79..e86b80b7b3 100644
--- a/generated/python/resources/span/__init__.py
+++ b/generated/python/resources/span/__init__.py
@@ -1,5 +1,5 @@
# This file was auto-generated by Fern from our API Definition.
-from .types import CreateSpanRequest, Span, UpdateSpanRequest
+from .types import CreateLLMSpanRequest, CreateSpanRequest, LLMAttributes, LLMTokens, Span, UpdateSpanRequest
-__all__ = ["CreateSpanRequest", "Span", "UpdateSpanRequest"]
+__all__ = ["CreateLLMSpanRequest", "CreateSpanRequest", "LLMAttributes", "LLMTokens", "Span", "UpdateSpanRequest"]
diff --git a/generated/python/resources/span/client.py b/generated/python/resources/span/client.py
index 953ef9a5ff..8b9219e2dd 100644
--- a/generated/python/resources/span/client.py
+++ b/generated/python/resources/span/client.py
@@ -9,6 +9,7 @@ import pydantic
from ...core.api_error import ApiError
from ...core.jsonable_encoder import jsonable_encoder
from ...environment import FintoLangfuseEnvironment
+from .types.create_llm_span_request import CreateLLMSpanRequest
from .types.create_span_request import CreateSpanRequest
from .types.span import Span
from .types.update_span_request import UpdateSpanRequest
@@ -18,6 +19,18 @@ class SpanClient:
def __init__(self, *, environment: FintoLangfuseEnvironment):
self._environment = environment
+ def create_llm_call(self, *, request: CreateLLMSpanRequest) -> Span:
+ _response = httpx.request(
+ "POST", urllib.parse.urljoin(f"{self._environment}/", "api/llm-span"), json=jsonable_encoder(request)
+ )
+ if 200 <= _response.status_code < 300:
+ return pydantic.parse_obj_as(Span, _response.json()) # type: ignore
+ try:
+ _response_json = _response.json()
+ except JSONDecodeError:
+ raise ApiError(status_code=_response.status_code, body=_response.text)
+ raise ApiError(status_code=_response.status_code, body=_response_json)
+
def create(self, *, request: CreateSpanRequest) -> Span:
_response = httpx.request(
"POST", urllib.parse.urljoin(f"{self._environment}/", "api/spans"), json=jsonable_encoder(request)
diff --git a/generated/python/resources/span/types/__init__.py b/generated/python/resources/span/types/__init__.py
index 70d6a595bf..02b40f9531 100644
--- a/generated/python/resources/span/types/__init__.py
+++ b/generated/python/resources/span/types/__init__.py
@@ -1,7 +1,10 @@
# This file was auto-generated by Fern from our API Definition.
+from .create_llm_span_request import CreateLLMSpanRequest
from .create_span_request import CreateSpanRequest
+from .llm_attributes import LLMAttributes
+from .llm_tokens import LLMTokens
from .span import Span
from .update_span_request import UpdateSpanRequest
-__all__ = ["CreateSpanRequest", "Span", "UpdateSpanRequest"]
+__all__ = ["CreateLLMSpanRequest", "CreateSpanRequest", "LLMAttributes", "LLMTokens", "Span", "UpdateSpanRequest"]
diff --git a/generated/python/resources/span/types/create_llm_span_request.py b/generated/python/resources/span/types/create_llm_span_request.py
new file mode 100644
index 0000000000..483e2b48a1
--- /dev/null
+++ b/generated/python/resources/span/types/create_llm_span_request.py
@@ -0,0 +1,30 @@
+# This file was auto-generated by Fern from our API Definition.
+
+import datetime as dt
+import typing
+
+import pydantic
+
+from ....core.datetime_utils import serialize_datetime
+from .llm_attributes import LLMAttributes
+
+
+class CreateLLMSpanRequest(pydantic.BaseModel):
+ trace_id: str = pydantic.Field(alias="traceId")
+ name: str
+ start_time: dt.datetime = pydantic.Field(alias="startTime")
+ attributes: LLMAttributes
+ parent_observation_id: typing.Optional[str] = pydantic.Field(alias="parentObservationId")
+
+ def json(self, **kwargs: typing.Any) -> str:
+ kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
+ return super().json(**kwargs_with_defaults)
+
+ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
+ kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
+ return super().dict(**kwargs_with_defaults)
+
+ class Config:
+ frozen = True
+ allow_population_by_field_name = True
+ json_encoders = {dt.datetime: serialize_datetime}
diff --git a/generated/python/resources/span/types/llm_attributes.py b/generated/python/resources/span/types/llm_attributes.py
new file mode 100644
index 0000000000..f359b6e87a
--- /dev/null
+++ b/generated/python/resources/span/types/llm_attributes.py
@@ -0,0 +1,28 @@
+# This file was auto-generated by Fern from our API Definition.
+
+import datetime as dt
+import typing
+
+import pydantic
+
+from ....core.datetime_utils import serialize_datetime
+from .llm_tokens import LLMTokens
+
+
+class LLMAttributes(pydantic.BaseModel):
+ model: typing.Any
+ prompt: typing.Optional[str]
+ completion: typing.Optional[str]
+ tokens: LLMTokens
+
+ def json(self, **kwargs: typing.Any) -> str:
+ kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
+ return super().json(**kwargs_with_defaults)
+
+ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
+ kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
+ return super().dict(**kwargs_with_defaults)
+
+ class Config:
+ frozen = True
+ json_encoders = {dt.datetime: serialize_datetime}
diff --git a/generated/python/resources/span/types/llm_tokens.py b/generated/python/resources/span/types/llm_tokens.py
new file mode 100644
index 0000000000..376e46b8ca
--- /dev/null
+++ b/generated/python/resources/span/types/llm_tokens.py
@@ -0,0 +1,25 @@
+# This file was auto-generated by Fern from our API Definition.
+
+import datetime as dt
+import typing
+
+import pydantic
+
+from ....core.datetime_utils import serialize_datetime
+
+
+class LLMTokens(pydantic.BaseModel):
+ prompt_amount: typing.Optional[int]
+ completion_amount: typing.Optional[int]
+
+ def json(self, **kwargs: typing.Any) -> str:
+ kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
+ return super().json(**kwargs_with_defaults)
+
+ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
+ kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
+ return super().dict(**kwargs_with_defaults)
+
+ class Config:
+ frozen = True
+ json_encoders = {dt.datetime: serialize_datetime}
diff --git a/generated/python/resources/trace/types/update_trace_request.py b/generated/python/resources/trace/types/update_trace_request.py
index 2f6bb3fca4..d31829a0ca 100644
--- a/generated/python/resources/trace/types/update_trace_request.py
+++ b/generated/python/resources/trace/types/update_trace_request.py
@@ -9,7 +9,7 @@ from ....core.datetime_utils import serialize_datetime
class UpdateTraceRequest(pydantic.BaseModel):
- trace_id: str = pydantic.Field(alias="traceId")
+ id: str
status: str
status_message: typing.Optional[str] = pydantic.Field(alias="statusMessage")
diff --git a/src/pages/api/llm-span.ts b/src/pages/api/llm-span.ts
new file mode 100644
index 0000000000..63ee74e422
--- /dev/null
+++ b/src/pages/api/llm-span.ts
@@ -0,0 +1,67 @@
+import { prisma } from "@/src/server/db";
+import { ObservationType } from "@prisma/client";
+import { type NextApiRequest, type NextApiResponse } from "next";
+import { z } from "zod";
+import { cors, runMiddleware } from "./cors";
+
+const SpanPostSchema = z.object({
+ traceId: z.string(),
+ name: z.string(),
+ startTime: z.string().datetime(),
+ attributes: z.record(z.string(), z.any()),
+ parentObservationId: z.string().optional(),
+});
+
+const LLMSpanCreateSchema = z.object({
+ traceId: z.string(),
+ name: z.string(),
+ startTime: z.string().datetime(),
+ attributes: z.object({
+ prompt: z.string().optional(),
+ completion: z.string().optional(),
+ tokens: z.object({
+ prompt: z.number().optional(),
+ completion: z.number().optional(),
+ }),
+ }),
+ parentObservationId: z.string().optional(),
+});
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ await runMiddleware(req, res, cors);
+
+ if (req.method !== "POST") {
+ return res.status(405).json({ message: "Method not allowed" });
+ }
+
+ try {
+ const { traceId, name, startTime, attributes, parentObservationId } =
+ LLMSpanCreateSchema.parse(req.body);
+
+ const newObservation = await prisma.observation.create({
+ data: {
+ trace: { connect: { id: traceId } },
+ type: ObservationType.LLMCALL,
+ name,
+ startTime: new Date(startTime),
+ attributes,
+ parent: parentObservationId
+ ? { connect: { id: parentObservationId } }
+ : undefined,
+ },
+ });
+
+ res.status(201).json(newObservation);
+ } catch (error: unknown) {
+ const errorMessage =
+ error instanceof Error ? error.message : "An unknown error occurred";
+ res.status(400).json({
+ success: false,
+ message: "Invalid request data",
+ error: errorMessage,
+ });
+ }
+}