Skip to content

Commit

Permalink
feat: remove dependency of langchain
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Nov 30, 2023
1 parent a817072 commit 4d79ae5
Show file tree
Hide file tree
Showing 31 changed files with 5 additions and 2,198 deletions.
176 changes: 0 additions & 176 deletions common/utils/llm_chat/__tests__/traces.test.ts

This file was deleted.

63 changes: 0 additions & 63 deletions common/utils/llm_chat/traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Run } from 'langchain/callbacks';
import { AgentRun } from 'langchain/dist/callbacks/handlers/tracer';
import _ from 'lodash';

export interface AgentFrameworkTrace {
interactionId: string;
parentInteractionId: string;
Expand All @@ -16,62 +12,3 @@ export interface AgentFrameworkTrace {
origin: string;
traceNumber: number;
}

export interface LangchainTrace {
id: Run['id'];
parentRunId?: Run['parent_run_id'];
actions?: AgentRun['actions'];
type: Run['run_type'];
startTime: Run['start_time'];
name: string;
input: string;
output?: string;
}

const getValue = (obj: Record<string, string>, possibleKeys: string[]) => {
for (const key of possibleKeys) {
const value = _.get(obj, key);
if (value) return value;
}
return '';
};

/**
* By default, tool traces have name 'DynamicTool'. Replace name for all tool
* traces with the tool used in parent run actions.
*/
const replaceToolNames = (traces: LangchainTrace[]) => {
return traces.map((trace) => ({
...trace,
...(trace.type === 'tool' && {
name: _.get(
traces.find((t) => t.id === trace.parentRunId),
'actions.0.tool',
trace.name
),
}),
}));
};

const traverse = (runs: Array<Run | AgentRun>, traces: LangchainTrace[] = []) => {
traces.push(
...runs.map((run) => ({
id: run.id,
parentRunId: run.parent_run_id,
type: run.run_type,
startTime: run.start_time,
name: run.name,
input: getValue(run.inputs, ['input', 'question', 'messages.0.0.kwargs.content']),
output: run.outputs && getValue(run.outputs, ['output', 'text', 'generations.0.0.text']),
...('actions' in run && { actions: run.actions }),
}))
);
runs.forEach((run) => {
if (run.child_runs) traverse(run.child_runs, traces);
});
return traces;
};

export const convertToTraces = (runs: Run[]) => {
return replaceToolNames(traverse(runs));
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"csv-parser": "^3.0.0",
"dompurify": "^2.4.1",
"jsdom": "^22.1.0",
"langchain": "^0.0.164",
"postinstall": "^0.7.4",
"web-streams-polyfill": "^3.2.1"
},
Expand Down
100 changes: 0 additions & 100 deletions public/components/traces.tsx

This file was deleted.

Loading

0 comments on commit 4d79ae5

Please sign in to comment.