Skip to content

Commit

Permalink
fix(langchain,core): Add metadata to pulled hub prompts (#7497)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Jan 10, 2025
1 parent 8bb980e commit d727338
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
15 changes: 14 additions & 1 deletion langchain-core/src/prompts/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ export abstract class BasePromptTemplate<

partialVariables: PartialValues<PartialVariableName>;

/**
* Metadata to be used for tracing.
*/
metadata?: Record<string, unknown>;

/** Tags to be used for tracing. */
tags?: string[];

constructor(input: BasePromptTemplateInput) {
super(input);
const { inputVariables } = input;
Expand Down Expand Up @@ -127,10 +135,15 @@ export abstract class BasePromptTemplate<
input: RunInput,
options?: BaseCallbackConfig
): Promise<RunOutput> {
const metadata = {
...this.metadata,
...options?.metadata,
};
const tags = [...(this.tags ?? []), ...(options?.tags ?? [])];
return this._callWithConfig(
(input: RunInput) => this.formatPromptValue(input),
input,
{ ...options, runType: "prompt" }
{ ...options, tags, metadata, runType: "prompt" }
);
}

Expand Down
19 changes: 17 additions & 2 deletions langchain/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,23 @@ export async function pull<T extends Runnable>(
options?: { apiKey?: string; apiUrl?: string; includeModel?: boolean }
) {
const client = new Client(options);
const result = await client._pullPrompt(ownerRepoCommit, {

const promptObject = await client.pullPromptCommit(ownerRepoCommit, {
includeModel: options?.includeModel,
});
return load<T>(result);

if (promptObject.manifest.kwargs?.metadata === undefined) {
promptObject.manifest.kwargs = {
...promptObject.manifest.kwargs,
metadata: {},
};
}

promptObject.manifest.kwargs.metadata = {
...promptObject.manifest.kwargs.metadata,
lc_hub_owner: promptObject.owner,
lc_hub_repo: promptObject.repo,
lc_hub_commit_hash: promptObject.commit_hash,
};
return load<T>(JSON.stringify(promptObject.manifest));
}
8 changes: 4 additions & 4 deletions langchain/src/tests/hub.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-process-env */

import { PromptTemplate } from "@langchain/core/prompts";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import * as hub from "../hub.js";

test("Test LangChain Hub client pushing a new repo", async () => {
const prompt = PromptTemplate.fromTemplate(
const prompt = ChatPromptTemplate.fromTemplate(
`You are a parrot. The current date is ${new Date().toISOString()}\n{input}`
);
const repoName = `${
Expand All @@ -14,7 +14,7 @@ test("Test LangChain Hub client pushing a new repo", async () => {
newRepoIsPublic: false,
});
const pulledPrompt = await hub.pull(repoName);
expect(prompt.invoke({ input: "testing" })).toEqual(
pulledPrompt.invoke({ input: "testing" })
expect(await prompt.invoke({ input: "testing" })).toEqual(
await pulledPrompt.invoke({ input: "testing" })
);
});

0 comments on commit d727338

Please sign in to comment.