-
Notifications
You must be signed in to change notification settings - Fork 0
Decorators fixes latest #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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.
This file contains hidden or 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 contains hidden or 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,147 @@ | ||
| import * as contextApi from "@opentelemetry/api"; | ||
|
|
||
| import { HumanloopRuntimeError } from "./error"; | ||
| import { | ||
| HUMANLOOP_CONTEXT_DECORATOR, | ||
| HUMANLOOP_CONTEXT_EVALUATION, | ||
| HUMANLOOP_CONTEXT_TRACE_ID, | ||
| } from "./otel/constants"; | ||
|
|
||
| export function getTraceId(): string | undefined { | ||
| const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID); | ||
| const value = contextApi.context.active().getValue(key); | ||
| return (value || undefined) as string | undefined; | ||
| } | ||
|
|
||
| export function setTraceId(flowLogId: string): contextApi.Context { | ||
| const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID); | ||
| return contextApi.context.active().setValue(key, flowLogId); | ||
| } | ||
|
|
||
| export type DecoratorContext = { | ||
| path: string; | ||
| type: "prompt" | "tool" | "flow"; | ||
| version: Record<string, unknown>; | ||
| }; | ||
|
|
||
| export function setDecoratorContext( | ||
| decoratorContext: DecoratorContext, | ||
| ): contextApi.Context { | ||
| const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_DECORATOR); | ||
| return contextApi.context.active().setValue(key, decoratorContext); | ||
| } | ||
|
|
||
| export function getDecoratorContext(): DecoratorContext | undefined { | ||
| const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_DECORATOR); | ||
| return (contextApi.context.active().getValue(key) || undefined) as | ||
| | DecoratorContext | ||
| | undefined; | ||
| } | ||
|
|
||
| export class EvaluationContext { | ||
| public sourceDatapointId: string; | ||
| public runId: string; | ||
| public fileId: string; | ||
| public path: string; | ||
| private _logged: boolean; | ||
| private _callback: (log_id: string) => Promise<void>; | ||
|
|
||
| constructor({ | ||
| sourceDatapointId, | ||
| runId, | ||
| evalCallback, | ||
| fileId, | ||
| path, | ||
| }: { | ||
| sourceDatapointId: string; | ||
| runId: string; | ||
| evalCallback: (log_id: string) => Promise<void>; | ||
| fileId: string; | ||
| path: string; | ||
| }) { | ||
| this.sourceDatapointId = sourceDatapointId; | ||
| this.runId = runId; | ||
| this._callback = evalCallback; | ||
| this.fileId = fileId; | ||
| this.path = path; | ||
| this._logged = false; | ||
| } | ||
|
|
||
| public get logged(): boolean { | ||
| return this._logged; | ||
| } | ||
|
|
||
| public logArgsWithContext({ | ||
| logArgs, | ||
| forOtel, | ||
| path, | ||
| fileId, | ||
| }: { | ||
| logArgs: Record<string, any>; | ||
| forOtel: boolean; | ||
| path?: string; | ||
| fileId?: string; | ||
| }): [Record<string, any>, ((log_id: string) => Promise<void>) | null] { | ||
| if (path === undefined && fileId === undefined) { | ||
| throw new HumanloopRuntimeError( | ||
| "Internal error: Evaluation context called without providing a path or file_id", | ||
| ); | ||
| } | ||
|
|
||
| if (this._logged) { | ||
| return [logArgs, null]; | ||
| } | ||
|
|
||
| if (this.path !== undefined && this.path === path) { | ||
| this._logged = true; | ||
| return [ | ||
| forOtel | ||
| ? { | ||
| ...logArgs, | ||
| source_datapoint_id: this.sourceDatapointId, | ||
| run_id: this.runId, | ||
| } | ||
| : { | ||
| ...logArgs, | ||
| sourceDatapointId: this.sourceDatapointId, | ||
| runId: this.runId, | ||
| }, | ||
| this._callback, | ||
| ]; | ||
| } else if (this.fileId !== undefined && this.fileId === fileId) { | ||
| this._logged = true; | ||
| return [ | ||
| forOtel | ||
| ? { | ||
| ...logArgs, | ||
| sourceDatapointId: this.sourceDatapointId, | ||
| runId: this.runId, | ||
| } | ||
| : { | ||
| ...logArgs, | ||
| sourceDatapointId: this.sourceDatapointId, | ||
| runId: this.runId, | ||
| }, | ||
| this._callback, | ||
| ]; | ||
| } else { | ||
| return [logArgs, null]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ... existing code ... | ||
|
|
||
| export function setEvaluationContext( | ||
| evaluationContext: EvaluationContext, | ||
| ): contextApi.Context { | ||
| const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_EVALUATION); | ||
| return contextApi.context.active().setValue(key, evaluationContext); | ||
| } | ||
|
|
||
| export function getEvaluationContext(): EvaluationContext | undefined { | ||
| const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_EVALUATION); | ||
| return (contextApi.context.active().getValue(key) || undefined) as | ||
| | EvaluationContext | ||
| | undefined; | ||
| } | ||
This file contains hidden or 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,151 @@ | ||
| import * as contextApi from "@opentelemetry/api"; | ||
| import { ReadableSpan, Tracer } from "@opentelemetry/sdk-trace-node"; | ||
|
|
||
| import { HumanloopClient } from "../Client"; | ||
| import { ChatMessage, FlowLogRequest, FlowLogResponse } from "../api"; | ||
| import { getTraceId, setDecoratorContext, setTraceId } from "../context"; | ||
| import { jsonifyIfNotString, writeToOpenTelemetrySpan } from "../otel"; | ||
| import { | ||
| HUMANLOOP_FILE_TYPE_KEY, | ||
| HUMANLOOP_FLOW_SPAN_NAME, | ||
| HUMANLOOP_LOG_KEY, | ||
| HUMANLOOP_PATH_KEY, | ||
| } from "../otel/constants"; | ||
|
|
||
| export function flowUtilityFactory<I, O>( | ||
| client: HumanloopClient, | ||
| opentelemetryTracer: Tracer, | ||
| callable: ( | ||
| args: I extends Record<string, unknown> & { | ||
| messages?: ChatMessage[]; | ||
| } | ||
| ? I | ||
| : never, | ||
| ) => O, | ||
| path: string, | ||
| attributes?: Record<string, unknown>, | ||
| ): (args: I) => Promise<O | undefined> & { | ||
| file: { | ||
| type: string; | ||
| version: { attributes?: Record<string, unknown> }; | ||
| callable: (args: I) => Promise<O | undefined>; | ||
| }; | ||
| } { | ||
| const flowKernel = { attributes: attributes || {} }; | ||
| const fileType = "flow"; | ||
|
|
||
| const wrappedFunction = async ( | ||
| inputs: I extends Record<string, unknown> & { | ||
| messages?: ChatMessage[]; | ||
| } | ||
| ? I | ||
| : never, | ||
| ) => { | ||
| return contextApi.context.with( | ||
| setDecoratorContext({ | ||
| path: path, | ||
| type: fileType, | ||
| version: flowKernel, | ||
| }), | ||
| async () => { | ||
| return opentelemetryTracer.startActiveSpan( | ||
| HUMANLOOP_FLOW_SPAN_NAME, | ||
| async (span) => { | ||
| span.setAttribute(HUMANLOOP_PATH_KEY, path); | ||
| span.setAttribute(HUMANLOOP_FILE_TYPE_KEY, fileType); | ||
| const traceId = getTraceId(); | ||
|
|
||
| const logInputs = { ...inputs } as Record<string, unknown>; | ||
| const logMessages = logInputs.messages as | ||
| | ChatMessage[] | ||
| | undefined; | ||
| delete logInputs.messages; | ||
|
|
||
| const initLogInputs: FlowLogRequest = { | ||
| inputs: logInputs, | ||
| messages: logMessages, | ||
| traceParentId: traceId, | ||
| }; | ||
|
|
||
| const flowLogResponse: FlowLogResponse = | ||
| // @ts-ignore | ||
| await client.flows._log({ | ||
| path, | ||
| flow: flowKernel, | ||
| logStatus: "incomplete", | ||
| ...initLogInputs, | ||
| }); | ||
|
|
||
| return await contextApi.context.with( | ||
| setTraceId(flowLogResponse.id), | ||
| async () => { | ||
| let logOutput: string | undefined; | ||
| let outputMessage: ChatMessage | undefined; | ||
| let logError: string | undefined; | ||
| let funcOutput: O | undefined; | ||
|
|
||
| try { | ||
| funcOutput = await callable(inputs); | ||
| if ( | ||
| // @ts-ignore | ||
| funcOutput instanceof Object && | ||
| "role" in funcOutput && | ||
| "content" in funcOutput | ||
| ) { | ||
| outputMessage = | ||
| funcOutput as unknown as ChatMessage; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
| logOutput = undefined; | ||
| } else { | ||
| logOutput = jsonifyIfNotString( | ||
| callable, | ||
| funcOutput, | ||
| ); | ||
| outputMessage = undefined; | ||
| } | ||
| logError = undefined; | ||
| } catch (err: any) { | ||
| console.error( | ||
| `Error calling ${callable.name}:`, | ||
| err, | ||
| ); | ||
| logOutput = undefined; | ||
| outputMessage = undefined; | ||
| logError = err.message || String(err); | ||
| funcOutput = undefined as unknown as O; | ||
| } | ||
|
|
||
| const updatedFlowLog = { | ||
| log_status: "complete", | ||
| output: logOutput, | ||
| error: logError, | ||
| output_message: outputMessage, | ||
| id: flowLogResponse.id, | ||
| }; | ||
|
|
||
| writeToOpenTelemetrySpan( | ||
| span as unknown as ReadableSpan, | ||
| // @ts-ignore | ||
| updatedFlowLog, | ||
| HUMANLOOP_LOG_KEY, | ||
| ); | ||
|
|
||
| span.end(); | ||
| return funcOutput; | ||
| }, | ||
| ); | ||
| }, | ||
| ); | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| // @ts-ignore | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ideally these would be scoped |
||
| return Object.assign(wrappedFunction, { | ||
| file: { | ||
| path: path, | ||
| type: fileType, | ||
| version: flowKernel, | ||
| callable: wrappedFunction, | ||
| }, | ||
| }); | ||
| } | ||
This file contains hidden or 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,2 @@ | ||
| export { flowUtilityFactory } from "./flow"; | ||
| export { toolUtilityFactory } from "./tool"; |
This file contains hidden or 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,35 @@ | ||
| import * as contextApi from "@opentelemetry/api"; | ||
|
|
||
| import { setDecoratorContext } from "../evals"; | ||
|
|
||
| export function promptDecoratorFactory<I, O>(path: string, callable: (inputs: I) => O) { | ||
| const fileType = "prompt"; | ||
|
|
||
| const wrappedFunction = (inputs: I) => { | ||
| return contextApi.context.with( | ||
| setDecoratorContext({ | ||
| path: path, | ||
| type: fileType, | ||
| version: { | ||
| // TODO: Implement reverse lookup of template | ||
| template: undefined, | ||
| }, | ||
| }), | ||
| async () => { | ||
| return await callable(inputs); | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| return Object.assign(wrappedFunction, { | ||
| file: { | ||
| path: path, | ||
| type: fileType, | ||
| version: { | ||
| // TODO: Implement reverse lookup of template | ||
| template: undefined, | ||
| }, | ||
| callable: wrappedFunction, | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i assume this is AI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(is this a sign that any other bits of code need cleanup/looking at?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed