-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): client, annotations (#498)
- Loading branch information
1 parent
9d0f0c0
commit 6e4192e
Showing
11 changed files
with
365 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/traceloop-sdk/src/lib/client/annotation/base-annotation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { TraceloopClient } from "../traceloop-client"; | ||
import { AnnotationCreateOptions } from "../../interfaces/annotations.interface"; | ||
|
||
export type AnnotationFlow = "user_feedback"; | ||
|
||
/** | ||
* Base class for handling annotation operations with the Traceloop API. | ||
* @internal | ||
*/ | ||
export class BaseAnnotation { | ||
constructor( | ||
protected client: TraceloopClient, | ||
protected flow: AnnotationFlow, | ||
) {} | ||
|
||
/** | ||
* Creates a new annotation. | ||
* | ||
* @param options - The annotation creation options | ||
* @returns Promise resolving to the fetch Response | ||
*/ | ||
async create(options: AnnotationCreateOptions) { | ||
return await this.client.post( | ||
`/v2/annotation-tasks/${options.annotationTask}/annotations`, | ||
{ | ||
entity_instance_id: options.entity.id, | ||
tags: options.tags, | ||
source: "sdk", | ||
flow: this.flow, | ||
actor: { | ||
type: "service", | ||
id: this.client.appName, | ||
}, | ||
}, | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/traceloop-sdk/src/lib/client/annotation/user-feedback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { BaseAnnotation } from "./base-annotation"; | ||
import { TraceloopClient } from "../traceloop-client"; | ||
import { AnnotationCreateOptions } from "../../interfaces/annotations.interface"; | ||
|
||
/** | ||
* Handles user feedback annotations with the Traceloop API. | ||
*/ | ||
export class UserFeedback extends BaseAnnotation { | ||
constructor(client: TraceloopClient) { | ||
super(client, "user_feedback"); | ||
} | ||
|
||
/** | ||
* Creates a new annotation for a specific task and entity. | ||
* | ||
* @param options - The options for creating an annotation | ||
* @returns Promise resolving to the fetch Response | ||
* | ||
* @example | ||
* ```typescript | ||
* await client.annotation.create({ | ||
* annotationTask: 'sample-annotation-task', | ||
* entity: { | ||
* id: '123456', | ||
* }, | ||
* tags: { | ||
* sentiment: 'positive', | ||
* score: 0.85, | ||
* tones: ['happy', 'surprised'] | ||
* } | ||
* }); | ||
* ``` | ||
*/ | ||
override async create(options: AnnotationCreateOptions) { | ||
return await super.create(options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { TraceloopClientOptions } from "../interfaces"; | ||
import { version } from "../../../package.json"; | ||
import { UserFeedback } from "./annotation/user-feedback"; | ||
|
||
/** | ||
* The main client for interacting with Traceloop's API. | ||
* This client can be used either directly or through the singleton pattern via configuration. | ||
* | ||
* @example | ||
* // Direct usage | ||
* const client = new TraceloopClient('your-api-key'); | ||
* | ||
* @example | ||
* // Through configuration (recommended) | ||
* initialize({ apiKey: 'your-api-key', appName: 'your-app' }); | ||
* const client = getClient(); | ||
*/ | ||
export class TraceloopClient { | ||
private version: string = version; | ||
public appName: string; | ||
private baseUrl: string; | ||
private apiKey: string; | ||
|
||
/** | ||
* Creates a new instance of the TraceloopClient. | ||
* | ||
* @param options - Configuration options for the client | ||
*/ | ||
constructor(options: TraceloopClientOptions) { | ||
this.apiKey = options.apiKey; | ||
this.appName = options.appName; | ||
this.baseUrl = | ||
options.baseUrl || | ||
process.env.TRACELOOP_BASE_URL || | ||
"https://api.traceloop.com"; | ||
} | ||
|
||
userFeedback = new UserFeedback(this); | ||
|
||
async post(path: string, body: Record<string, unknown>) { | ||
return await fetch(`${this.baseUrl}${path}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.apiKey}`, | ||
"X-Traceloop-SDK-Version": this.version, | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/traceloop-sdk/src/lib/interfaces/annotations.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Represents an entity in the system that can be annotated. | ||
* An entity is typically a unit of content or interaction that needs to be tracked or evaluated. | ||
* It is reported as an association property before the annotation is created. | ||
*/ | ||
export interface Entity { | ||
/** | ||
* Unique identifier for the entity. | ||
* This could be a user ID, conversation ID, or any other unique identifier in your system. | ||
*/ | ||
id: string; | ||
} | ||
|
||
/** | ||
* Configuration options for creating a new annotation. | ||
* Annotations are used to attach metadata, feedback, or evaluation results to specific entities. | ||
*/ | ||
export interface AnnotationCreateOptions { | ||
/** | ||
* The identifier of the annotation task. | ||
* The ID or slug of the annotation task, Can be found at app.traceloop.com/annotation_tasks/:annotationTaskId */ | ||
annotationTask: string; | ||
|
||
/** | ||
* The entity to be annotated. | ||
* Contains the entity's identifier and optional type information. | ||
* Be sure to report the entity instance ID as the association property before | ||
* in order to correctly correlate the annotation to the relevant context. | ||
*/ | ||
entity: Entity; | ||
|
||
/** | ||
* Key-value pairs of annotation data, should match the tags defined in the annotation task | ||
*/ | ||
tags: Record<string, TagValue>; | ||
} | ||
|
||
export type TagValue = string | number | string[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
export * from "./initialize-options.interface"; | ||
export * from "./prompts.interface"; | ||
export * from "./annotations.interface"; | ||
export * from "./traceloop-client.interface"; | ||
|
||
export interface TraceloopClientOptions { | ||
apiKey: string; | ||
appName: string; | ||
baseUrl?: string; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/traceloop-sdk/src/lib/interfaces/traceloop-client.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface TraceloopClientOptions { | ||
apiKey: string; | ||
appName: string; | ||
baseUrl?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.