Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
85 changes: 85 additions & 0 deletions components/langfuse/actions/add-feedback/add-feedback.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import constants from "../../common/constants.mjs";
import app from "../../langfuse.app.mjs";

export default {
key: "langfuse-add-feedback",
name: "Add Feedback",
description: "Attach user feedback to an existing trace in Langfuse. [See the documentation](https://api.reference.langfuse.com/#tag/comments/POST/api/public/comments).",
version: "0.0.1",
type: "action",
props: {
app,
projectId: {
propDefinition: [
app,
"projectId",
],
},
objectType: {
propDefinition: [
app,
"objectType",
],
},
objectId: {
propDefinition: [
app,
"objectId",
({ objectType }) => ({
objectType,
}),
],
},
content: {
type: "string",
label: "Content",
description: "The content of the comment. May include markdown. Currently limited to 3000 characters.",
},
},
methods: {
addFeedback(args = {}) {
return this.app.post({
path: "/comments",
...args,
});
},
async getObjectId() {
const {
app,
objectType,
objectId,
} = this;
if (objectType == constants.OBJECT_TYPE.PROMPT) {
const prompt = await app.getPrompt({
promptName: objectId,
});
return prompt?.id;
}
return objectId;
},
},
async run({ $ }) {
const {
getObjectId,
addFeedback,
projectId,
objectType,
content,
} = this;

const objectId = await getObjectId();

const response = await addFeedback({
$,
data: {
projectId,
objectType,
objectId,
content,
},
});

$.export("$summary", "Successfully added feedback.");
return response;
},
};
126 changes: 126 additions & 0 deletions components/langfuse/actions/log-trace/log-trace.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { v4 as uuid } from "uuid";
import app from "../../langfuse.app.mjs";
import constants from "../../common/constants.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "langfuse-log-trace",
name: "Log Trace",
description: "Log a new trace in LangFuse with details. [See the documentation](https://api.reference.langfuse.com/#tag/ingestion/POST/api/public/ingestion).",
version: "0.0.1",
type: "action",
props: {
app,
name: {
type: "string",
label: "Name",
description: "The name of the trace",
},
input: {
type: "string",
label: "Input",
description: "The input of the trace",
},
output: {
type: "string",
label: "Output",
description: "The output of the trace",
},
userId: {
type: "string",
label: "User ID",
description: "The ID of the user",
optional: true,
},
sessionId: {
label: "Session ID",
description: "The ID of the session",
optional: true,
propDefinition: [
app,
"objectId",
() => ({
objectType: constants.OBJECT_TYPE.SESSION,
}),
],
},
release: {
type: "string",
label: "Release",
description: "The release of the trace",
optional: true,
},
version: {
type: "string",
label: "Version",
description: "The version of the trace",
optional: true,
},
metadata: {
type: "string",
label: "Metadata",
description: "The metadata of the trace",
optional: true,
},
tags: {
type: "string[]",
label: "Tags",
description: "The tags of the trace",
optional: true,
},
},
methods: {
batchIngestion(args = {}) {
return this.app.post({
path: "/ingestion",
...args,
});
},
},
async run({ $ }) {
const {
batchIngestion,
name,
userId,
input,
output,
sessionId,
release,
version,
metadata,
tags,
} = this;

const timestamp = new Date().toISOString();
const id = uuid();

const response = await batchIngestion({
$,
data: {
batch: [
{
id,
timestamp,
type: constants.INGESTION_TYPE.TRACE_CREATE,
body: {
id,
timestamp,
name,
userId,
input: utils.parseJson(input),
output: utils.parseJson(output),
sessionId,
release,
version,
metadata: utils.parseJson(metadata),
tags,
public: true,
},
},
],
},
});
$.export("$summary", "Successfully logged a new trace");
return response;
},
};
40 changes: 40 additions & 0 deletions components/langfuse/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const REGION_PLACEHOLDER = "{region}";
const BASE_URL = "https://{region}.langfuse.com";
const VERSION_PATH = "/api/public";

const INGESTION_TYPE = {
TRACE_CREATE: "trace-create",
SCORE_CREATE: "score-create",
SPAN_CREATE: "span-create",
SPAN_UPDATE: "span-update",
GENERATION_CREATE: "generation-create",
GENERATION_UPDATE: "generation-update",
EVENT_CREATE: "event-create",
SDK_LOG: "sdk-log",
OBSERVATION_CREATE: "observation-create",
OBSERVATION_UPDATE: "observation-update",
};

const LAST_DATE_AT = "lastDateAt";
const IS_FIRST_RUN = "isFirstRun";
const DEFAULT_LIMIT = 100;
const DEFAULT_MAX = 1000;

const OBJECT_TYPE = {
TRACE: "TRACE",
OBSERVATION: "OBSERVATION",
SESSION: "SESSION",
PROMPT: "PROMPT",
};

export default {
REGION_PLACEHOLDER,
BASE_URL,
VERSION_PATH,
INGESTION_TYPE,
LAST_DATE_AT,
IS_FIRST_RUN,
DEFAULT_LIMIT,
DEFAULT_MAX,
OBJECT_TYPE,
};
41 changes: 41 additions & 0 deletions components/langfuse/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
async function iterate(iterations) {
const items = [];
for await (const item of iterations) {
items.push(item);
}
return items;
}

function getNestedProperty(obj, propertyString) {
const properties = propertyString.split(".");
return properties.reduce((prev, curr) => prev?.[curr], obj);
}

const parseJson = (input) => {
const parse = (value) => {
if (typeof(value) === "string") {
try {
return parseJson(JSON.parse(value));
} catch (e) {
return value;
}
} else if (typeof(value) === "object" && value !== null) {
return Object.entries(value)
.reduce((acc, [
key,
val,
]) => Object.assign(acc, {
[key]: parse(val),
}), {});
}
return value;
};

return parse(input);
};

export default {
iterate,
getNestedProperty,
parseJson,
};
Loading
Loading