Skip to content

Commit

Permalink
fix(core): Use a single shared client instance for all runs (#7110)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Nov 5, 2024
1 parent a59a4c8 commit e54c101
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
32 changes: 2 additions & 30 deletions langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,36 +1267,8 @@ export function ensureHandler(
}

/**
* @example
* ```typescript
* const prompt = PromptTemplate.fromTemplate(`What is the answer to {question}?`);
*
* // Example of using LLMChain to process a series of questions
* const chain = new LLMChain({
* llm: new ChatOpenAI({ temperature: 0.9 }),
* prompt,
* });
*
* // Process questions using the chain
* const processQuestions = async (questions) => {
* for (const question of questions) {
* const result = await chain.call({ question });
* console.log(result);
* }
* };
*
* // Example questions
* const questions = [
* "What is your name?",
* "What is your quest?",
* "What is your favorite color?",
* ];
*
* // Run the example
const logFunction = handler.raiseError ? console.error : console.warn;
* processQuestions(questions).catch(consolelogFunction;
*
* ```
* @deprecated Use [`traceable`](https://docs.smith.langchain.com/observability/how_to_guides/tracing/annotate_code)
* from "langsmith" instead.
*/
export class TraceGroup {
private runManager?: CallbackManagerForChainRun;
Expand Down
18 changes: 18 additions & 0 deletions langchain-core/src/singletons/tracer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Client } from "langsmith";
import { getEnvironmentVariable } from "../utils/env.js";

let client: Client;

export const getDefaultLangChainClientSingleton = () => {
if (client === undefined) {
const clientParams =
getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
? {
// LangSmith has its own backgrounding system
blockOnRootRunFinalization: true,
}
: {};
client = new Client(clientParams);
}
return client;
};
10 changes: 2 additions & 8 deletions langchain-core/src/tracers/tracer_langchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { getEnvironmentVariable, getRuntimeEnvironment } from "../utils/env.js";
import { BaseTracer } from "./base.js";
import { BaseCallbackHandlerInput } from "../callbacks/base.js";
import { getDefaultLangChainClientSingleton } from "../singletons/tracer.js";

export interface Run extends BaseRun {
id: string;
Expand Down Expand Up @@ -59,14 +60,7 @@ export class LangChainTracer
getEnvironmentVariable("LANGCHAIN_PROJECT") ??
getEnvironmentVariable("LANGCHAIN_SESSION");
this.exampleId = exampleId;
const clientParams =
getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
? {
// LangSmith has its own backgrounding system
blockOnRootRunFinalization: true,
}
: {};
this.client = client ?? new Client(clientParams);
this.client = client ?? getDefaultLangChainClientSingleton();

const traceableTree = LangChainTracer.getTraceableRunTree();
if (traceableTree) {
Expand Down

0 comments on commit e54c101

Please sign in to comment.