Skip to content

Commit

Permalink
Nc/fix tracer chat output (#1253)
Browse files Browse the repository at this point in the history
* Use the same message format for output as used in input

* Move message conversion logic to toJSON method, which is called by JSON.stringify, this makes it harder for people to use LLMResult

* Fix imports

* Update tests
  • Loading branch information
nfcampos committed May 15, 2023
1 parent 8a5a092 commit 04b956e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 50 deletions.
6 changes: 1 addition & 5 deletions langchain/src/callbacks/handlers/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
RunInputs,
RunOutputs,
} from "../../schema/index.js";
import { mapChatMessagesToStoredMessages } from "../../stores/message/utils.js";
import { BaseCallbackHandler } from "../base.js";

export type RunType = "llm" | "chain" | "tool";
Expand Down Expand Up @@ -121,17 +120,14 @@ export abstract class BaseTracer extends BaseCallbackHandler {
parentRunId?: string
): Promise<void> {
const execution_order = this._getExecutionOrder(parentRunId);
const convertedMessages = messages.map((batch) =>
mapChatMessagesToStoredMessages(batch)
);
const run: Run = {
id: runId,
name: llm.name,
parent_run_id: parentRunId,
start_time: Date.now(),
end_time: 0,
serialized: llm,
inputs: { messages: convertedMessages },
inputs: { messages },
execution_order,
child_runs: [],
child_execution_order: execution_order,
Expand Down
53 changes: 36 additions & 17 deletions langchain/src/callbacks/tests/tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,43 @@ test("Test Chat Message Run", async () => {
await tracer.handleLLMEnd({ generations: [] }, runId);
expect(tracer.runs.length).toBe(1);
const run = tracer.runs[0];
const compareRun: Run = {
id: runId,
name: "test",
start_time: _DATE,
end_time: _DATE,
execution_order: 1,
child_execution_order: 1,
serialized: { name: "test" },
inputs: {
messages: [
[{ type: "human", data: { content: "Avast", role: undefined } }],
],
expect(run).toMatchInlineSnapshot(
{
id: expect.any(String),
},
run_type: "llm",
outputs: { generations: [] },
child_runs: [],
};
expect(run).toEqual(compareRun);
`
{
"child_execution_order": 1,
"child_runs": [],
"end_time": 1620000000000,
"execution_order": 1,
"id": Any<String>,
"inputs": {
"messages": [
[
{
"data": {
"content": "Avast",
"role": undefined,
},
"type": "human",
},
],
],
},
"name": "test",
"outputs": {
"generations": [],
},
"parent_run_id": undefined,
"run_type": "llm",
"serialized": {
"name": "test",
},
"start_time": 1620000000000,
}
`
);
});

test("Test LLM Run no start", async () => {
Expand Down
6 changes: 2 additions & 4 deletions langchain/src/client/langchainplus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import {
LLMResult,
RunInputs,
RunOutputs,
StoredMessage,
} from "../schema/index.js";
import { BaseLanguageModel } from "../base_language/index.js";
import { BaseChain } from "../chains/base.js";
import { BaseLLM } from "../llms/base.js";
import { BaseChatModel } from "../chat_models/base.js";
import {
StoredMessage,
mapStoredMessagesToChatMessages,
} from "../stores/message/utils.js";
import { mapStoredMessagesToChatMessages } from "../stores/message/utils.js";

export interface RunResult extends BaseRun {
name: string;
Expand Down
4 changes: 2 additions & 2 deletions langchain/src/prompts/tests/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ test("Test using partial", async () => {
// partial prompt has only remaining variables
expect(partialPrompt.inputVariables).toEqual(["bar"]);

expect(await partialPrompt.format({ bar: "baz" })).toBe(
'[{"text":"foobaz"}]'
expect(await partialPrompt.format({ bar: "baz" })).toMatchInlineSnapshot(
`"[{"type":"human","data":{"content":"foobaz"}}]"`
);
});
23 changes: 23 additions & 0 deletions langchain/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ export type LLMResult = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[RUN_KEY]?: Record<string, any>;
};

export interface StoredMessageData {
content: string;
role: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
additional_kwargs?: Record<string, any>;
}

export interface StoredMessage {
type: string;
data: StoredMessageData;
}

export type MessageType = "human" | "ai" | "generic" | "system";

export abstract class BaseChatMessage {
Expand All @@ -62,6 +75,16 @@ export abstract class BaseChatMessage {
constructor(text: string) {
this.text = text;
}

toJSON(): StoredMessage {
return {
type: this._getType(),
data: {
content: this.text,
role: "role" in this ? (this.role as string) : undefined,
},
};
}
}

export class HumanChatMessage extends BaseChatMessage {
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/stores/message/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
} from "@aws-sdk/client-dynamodb";

import {
StoredMessage,
BaseChatMessage,
BaseListChatMessageHistory,
} from "../../schema/index.js";
import {
StoredMessage,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "./utils.js";
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/stores/message/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
RedisScripts,
} from "redis";
import {
StoredMessage,
BaseChatMessage,
BaseListChatMessageHistory,
} from "../../schema/index.js";
import {
StoredMessage,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "./utils.js";
Expand Down
22 changes: 2 additions & 20 deletions langchain/src/stores/message/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@ import {
BaseChatMessage,
ChatMessage,
HumanChatMessage,
StoredMessage,
SystemChatMessage,
} from "../../schema/index.js";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AdditionalKwargs = Record<string, any>;
export interface StoredMessageData {
content: string;
role: string | undefined;
additional_kwargs?: AdditionalKwargs;
}

export interface StoredMessage {
type: string;
data: StoredMessageData;
}

interface StoredMessageV1 {
type: string;
role: string | undefined;
Expand Down Expand Up @@ -72,11 +60,5 @@ export function mapStoredMessagesToChatMessages(
export function mapChatMessagesToStoredMessages(
messages: BaseChatMessage[]
): StoredMessage[] {
return messages.map((message) => ({
type: message._getType(),
data: {
content: message.text,
role: "role" in message ? (message.role as string) : undefined,
},
}));
return messages.map((message) => message.toJSON());
}

1 comment on commit 04b956e

@vercel
Copy link

@vercel vercel bot commented on 04b956e May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.