Skip to content

Commit

Permalink
feat(cost): allow all usage types (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp authored Dec 16, 2024
1 parent 8d4b973 commit 8ddc9aa
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 63 deletions.
6 changes: 3 additions & 3 deletions integration-test/langfuse-integration-error-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("No errors should be thrown by SDKs", () => {
await handler.shutdownAsync();

// expect no errors to be thrown (would kill jest)
expect(global.console.error).toHaveBeenCalledTimes(1);
expect(global.console.error).toHaveBeenCalledTimes(0);
}, 10000);

it("incorrect keys", async () => {
Expand All @@ -109,7 +109,7 @@ describe("No errors should be thrown by SDKs", () => {
await handler.shutdownAsync();

// expect no errors to be thrown (would kill jest)
expect(global.console.error).toHaveBeenCalledTimes(1);
expect(global.console.error).toHaveBeenCalledTimes(0);
}, 10000);
});
});
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("shutdown async behavior", () => {
}

await handler.shutdownAsync();
expect(flushCallback).toHaveBeenCalledTimes(15);
expect(flushCallback).toHaveBeenCalledTimes(8);

const anyCallbackCount = anyCallback.mock.calls.length;

Expand Down
10 changes: 5 additions & 5 deletions integration-test/langfuse-integration-langchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe("Langchain", () => {

it("should execute simple non chat llm call", async () => {
const handler = new CallbackHandler({});
const llm = new OpenAI({ modelName: "gpt-4-1106-preview", maxTokens: 20 });
const llm = new OpenAI({ modelName: "gpt-3.5-turbo-instruct", maxTokens: 20 });
const res = await llm.invoke("Tell me a joke on a non chat api", { callbacks: [handler] });
const traceId = handler.traceId;
await handler.flushAsync();
Expand All @@ -169,7 +169,7 @@ describe("Langchain", () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const singleGeneration = generation![0];

expect(singleGeneration.name).toBe("OpenAIChat");
expect(singleGeneration.name).toBe("OpenAI");
expect(singleGeneration.input).toMatchObject(["Tell me a joke on a non chat api"]);
expect(singleGeneration.usage?.input).toBeDefined();
expect(singleGeneration.usage?.output).toBeDefined();
Expand Down Expand Up @@ -793,9 +793,9 @@ describe("Langchain", () => {
role: "assistant",
},
usage: {
completionTokens: expect.any(Number),
promptTokens: expect.any(Number),
totalTokens: expect.any(Number),
input: expect.any(Number),
output: expect.any(Number),
total: expect.any(Number),
},
version: "1.0.0",
});
Expand Down
121 changes: 113 additions & 8 deletions langfuse-core/openapi-spec/openapi-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,7 @@ components:
type: string
description: >-
The content of the comment. May include markdown. Currently limited
to 500 characters.
to 3000 characters.
authorUserId:
type: string
nullable: true
Expand Down Expand Up @@ -2627,7 +2627,9 @@ components:
usage:
$ref: '#/components/schemas/Usage'
nullable: true
description: The usage data of the observation
description: >-
(Deprecated. Use usageDetails and costDetails instead.) The usage
data of the observation
level:
$ref: '#/components/schemas/ObservationLevel'
description: The level of the observation
Expand All @@ -2643,6 +2645,25 @@ components:
type: string
nullable: true
description: The prompt ID associated with the observation
usageDetails:
type: object
additionalProperties:
type: integer
nullable: true
description: >-
The usage details of the observation. Key is the name of the usage
metric, value is the number of units consumed. The total key is the
sum of all (non-total) usage metrics or the total value ingested.
costDetails:
type: object
additionalProperties:
type: number
format: double
nullable: true
description: >-
The cost details of the observation. Key is the name of the cost
metric, value is the cost in USD. The total key is the sum of all
(non-total) cost metrics or the total value ingested.
required:
- id
- type
Expand Down Expand Up @@ -2683,17 +2704,23 @@ components:
type: number
format: double
nullable: true
description: The calculated cost of the input in USD
description: >-
(Deprecated. Use usageDetails and costDetails instead.) The
calculated cost of the input in USD
calculatedOutputCost:
type: number
format: double
nullable: true
description: The calculated cost of the output in USD
description: >-
(Deprecated. Use usageDetails and costDetails instead.) The
calculated cost of the output in USD
calculatedTotalCost:
type: number
format: double
nullable: true
description: The calculated total cost in USD
description: >-
(Deprecated. Use usageDetails and costDetails instead.) The
calculated total cost in USD
latency:
type: number
format: double
Expand All @@ -2709,7 +2736,9 @@ components:
Usage:
title: Usage
type: object
description: Standard interface for usage and cost
description: >-
(Deprecated. Use usageDetails and costDetails instead.) Standard
interface for usage and cost
properties:
input:
type: integer
Expand Down Expand Up @@ -3608,6 +3637,15 @@ components:
usage:
$ref: '#/components/schemas/IngestionUsage'
nullable: true
usageDetails:
$ref: '#/components/schemas/UsageDetails'
nullable: true
costDetails:
type: object
additionalProperties:
type: number
format: double
nullable: true
promptName:
type: string
nullable: true
Expand Down Expand Up @@ -3638,6 +3676,15 @@ components:
promptName:
type: string
nullable: true
usageDetails:
$ref: '#/components/schemas/UsageDetails'
nullable: true
costDetails:
type: object
additionalProperties:
type: number
format: double
nullable: true
promptVersion:
type: integer
nullable: true
Expand Down Expand Up @@ -3955,6 +4002,37 @@ components:
required:
- successes
- errors
OpenAIUsageSchema:
title: OpenAIUsageSchema
type: object
properties:
prompt_tokens:
type: integer
completion_tokens:
type: integer
total_tokens:
type: integer
prompt_tokens_details:
type: object
additionalProperties:
type: integer
nullable: true
completion_tokens_details:
type: object
additionalProperties:
type: integer
nullable: true
required:
- prompt_tokens
- completion_tokens
- total_tokens
UsageDetails:
title: UsageDetails
oneOf:
- type: object
additionalProperties:
type: integer
- $ref: '#/components/schemas/OpenAIUsageSchema'
GetMediaResponse:
title: GetMediaResponse
type: object
Expand Down Expand Up @@ -4058,8 +4136,35 @@ components:
title: MediaContentType
type: string
enum:
- >-
image/png","image/jpeg","image/jpg","image/webp","image/gif","image/svg+xml","image/tiff","image/bmp","audio/mpeg","audio/mp3","audio/wav","audio/ogg","audio/oga","audio/aac","audio/mp4","audio/flac","video/mp4","video/webm","text/plain","text/html","text/css","text/csv","application/pdf","application/msword","application/vnd.ms-excel","application/zip","application/json","application/xml","application/octet-stream
- image/png
- image/jpeg
- image/jpg
- image/webp
- image/gif
- image/svg+xml
- image/tiff
- image/bmp
- audio/mpeg
- audio/mp3
- audio/wav
- audio/ogg
- audio/oga
- audio/aac
- audio/mp4
- audio/flac
- video/mp4
- video/webm
- text/plain
- text/html
- text/css
- text/csv
- application/pdf
- application/msword
- application/vnd.ms-excel
- application/zip
- application/json
- application/xml
- application/octet-stream
description: The MIME type of the media record
DailyMetrics:
title: DailyMetrics
Expand Down
68 changes: 61 additions & 7 deletions langfuse-core/src/openapi/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export interface components {
objectType: string;
/** @description The id of the object to attach the comment to. If this does not reference a valid existing object, an error will be thrown. */
objectId: string;
/** @description The content of the comment. May include markdown. Currently limited to 500 characters. */
/** @description The content of the comment. May include markdown. Currently limited to 3000 characters. */
content: string;
/** @description The id of the user who created the comment. */
authorUserId?: string | null;
Expand Down Expand Up @@ -657,7 +657,7 @@ export interface components {
metadata?: unknown;
/** @description The output data of the observation */
output?: unknown;
/** @description The usage data of the observation */
/** @description (Deprecated. Use usageDetails and costDetails instead.) The usage data of the observation */
usage?: components["schemas"]["Usage"];
/** @description The level of the observation */
level: components["schemas"]["ObservationLevel"];
Expand All @@ -667,6 +667,14 @@ export interface components {
parentObservationId?: string | null;
/** @description The prompt ID associated with the observation */
promptId?: string | null;
/** @description The usage details of the observation. Key is the name of the usage metric, value is the number of units consumed. The total key is the sum of all (non-total) usage metrics or the total value ingested. */
usageDetails?: {
[key: string]: number;
} | null;
/** @description The cost details of the observation. Key is the name of the cost metric, value is the cost in USD. The total key is the sum of all (non-total) cost metrics or the total value ingested. */
costDetails?: {
[key: string]: number;
} | null;
};
/** ObservationsView */
ObservationsView: {
Expand All @@ -693,17 +701,17 @@ export interface components {
totalPrice?: number | null;
/**
* Format: double
* @description The calculated cost of the input in USD
* @description (Deprecated. Use usageDetails and costDetails instead.) The calculated cost of the input in USD
*/
calculatedInputCost?: number | null;
/**
* Format: double
* @description The calculated cost of the output in USD
* @description (Deprecated. Use usageDetails and costDetails instead.) The calculated cost of the output in USD
*/
calculatedOutputCost?: number | null;
/**
* Format: double
* @description The calculated total cost in USD
* @description (Deprecated. Use usageDetails and costDetails instead.) The calculated total cost in USD
*/
calculatedTotalCost?: number | null;
/**
Expand All @@ -719,7 +727,7 @@ export interface components {
} & components["schemas"]["Observation"];
/**
* Usage
* @description Standard interface for usage and cost
* @description (Deprecated. Use usageDetails and costDetails instead.) Standard interface for usage and cost
*/
Usage: {
/** @description Number of input units (e.g. tokens) */
Expand Down Expand Up @@ -1156,6 +1164,10 @@ export interface components {
[key: string]: components["schemas"]["MapValue"];
} | null;
usage?: components["schemas"]["IngestionUsage"];
usageDetails?: components["schemas"]["UsageDetails"];
costDetails?: {
[key: string]: number;
} | null;
promptName?: string | null;
promptVersion?: number | null;
} & components["schemas"]["CreateSpanBody"];
Expand All @@ -1169,6 +1181,10 @@ export interface components {
} | null;
usage?: components["schemas"]["IngestionUsage"];
promptName?: string | null;
usageDetails?: components["schemas"]["UsageDetails"];
costDetails?: {
[key: string]: number;
} | null;
promptVersion?: number | null;
} & components["schemas"]["UpdateSpanBody"];
/** ObservationBody */
Expand Down Expand Up @@ -1299,6 +1315,24 @@ export interface components {
successes: components["schemas"]["IngestionSuccess"][];
errors: components["schemas"]["IngestionError"][];
};
/** OpenAIUsageSchema */
OpenAIUsageSchema: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
prompt_tokens_details?: {
[key: string]: number;
} | null;
completion_tokens_details?: {
[key: string]: number;
} | null;
};
/** UsageDetails */
UsageDetails:
| {
[key: string]: number;
}
| components["schemas"]["OpenAIUsageSchema"];
/** GetMediaResponse */
GetMediaResponse: {
/** @description The unique langfuse identifier of a media record */
Expand Down Expand Up @@ -1362,11 +1396,31 @@ export interface components {
| "image/jpeg"
| "image/jpg"
| "image/webp"
| "image/gif"
| "image/svg+xml"
| "image/tiff"
| "image/bmp"
| "audio/mpeg"
| "audio/mp3"
| "audio/wav"
| "audio/ogg"
| "audio/oga"
| "audio/aac"
| "audio/mp4"
| "audio/flac"
| "video/mp4"
| "video/webm"
| "text/plain"
| "application/pdf";
| "text/html"
| "text/css"
| "text/csv"
| "application/pdf"
| "application/msword"
| "application/vnd.ms-excel"
| "application/zip"
| "application/json"
| "application/xml"
| "application/octet-stream";
/** DailyMetrics */
DailyMetrics: {
/** @description A list of daily metrics, only days with ingested data are included. */
Expand Down
Loading

0 comments on commit 8ddc9aa

Please sign in to comment.