-
Notifications
You must be signed in to change notification settings - Fork 80
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
feat(js): Add support for formatting LangChain prompts as OpenAI and Anthropic payloads #1038
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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 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 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 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 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 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 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,57 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import type { BasePromptValue } from "@langchain/core/prompt_values"; | ||
import * as langChainAnthropicImports from "@langchain/anthropic"; | ||
import Anthropic from "@anthropic-ai/sdk"; | ||
|
||
/** | ||
* Convert a formatted LangChain prompt (e.g. pulled from the hub) into | ||
* a format expected by Anthropic's JS SDK. | ||
* | ||
* Requires the "@langchain/anthropic" package to be installed in addition | ||
* to the Anthropic SDK. | ||
* | ||
* @example | ||
* ```ts | ||
* import { convertPromptToAnthropic } from "langsmith/utils/hub/anthropic"; | ||
* import { pull } from "langchain/hub"; | ||
* | ||
* import Anthropic from '@anthropic-ai/sdk'; | ||
* | ||
* const prompt = await pull("jacob/joke-generator"); | ||
* const formattedPrompt = await prompt.invoke({ | ||
* topic: "cats", | ||
* }); | ||
* | ||
* const { system, messages } = convertPromptToAnthropic(formattedPrompt); | ||
* | ||
* const anthropicClient = new Anthropic({ | ||
* apiKey: 'your_api_key', | ||
* }); | ||
* | ||
* const anthropicResponse = await anthropicClient.messages.create({ | ||
* model: "claude-3-5-sonnet-20240620", | ||
* max_tokens: 1024, | ||
* stream: false, | ||
* system, | ||
* messages, | ||
* }); | ||
* ``` | ||
* @param formattedPrompt | ||
* @returns A partial Anthropic payload. | ||
*/ | ||
export function convertPromptToAnthropic( | ||
formattedPrompt: BasePromptValue | ||
): Anthropic.Messages.MessageCreateParams { | ||
const messages = formattedPrompt.toChatMessages(); | ||
const { _convertMessagesToAnthropicPayload } = langChainAnthropicImports; | ||
if (typeof _convertMessagesToAnthropicPayload !== "function") { | ||
throw new Error( | ||
`Please update your version of "@langchain/anthropic" to 0.3.2 or higher.` | ||
); | ||
} | ||
const anthropicBody = _convertMessagesToAnthropicPayload(messages); | ||
if (anthropicBody.messages === undefined) { | ||
anthropicBody.messages = []; | ||
} | ||
return anthropicBody; | ||
} |
This file contains 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,52 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import type { BasePromptValue } from "@langchain/core/prompt_values"; | ||
import * as langChainOpenAIImports from "@langchain/openai"; | ||
import type { OpenAI } from "openai"; | ||
|
||
/** | ||
* Convert a formatted LangChain prompt (e.g. pulled from the hub) into | ||
* a format expected by OpenAI's JS SDK. | ||
* | ||
* Requires the "@langchain/openai" package to be installed in addition | ||
* to the OpenAI SDK. | ||
* | ||
* @example | ||
* ```ts | ||
* import { convertPromptToOpenAI } from "langsmith/utils/hub/openai"; | ||
* import { pull } from "langchain/hub"; | ||
* | ||
* import OpenAI from 'openai'; | ||
* | ||
* const prompt = await pull("jacob/joke-generator"); | ||
* const formattedPrompt = await prompt.invoke({ | ||
* topic: "cats", | ||
* }); | ||
* | ||
* const { messages } = convertPromptToOpenAI(formattedPrompt); | ||
* | ||
* const openAIClient = new OpenAI(); | ||
* | ||
* const openaiResponse = await openAIClient.chat.completions.create({ | ||
* model: "gpt-4o", | ||
* messages, | ||
* }); | ||
* ``` | ||
* @param formattedPrompt | ||
* @returns A partial OpenAI payload. | ||
*/ | ||
export function convertPromptToOpenAI(formattedPrompt: BasePromptValue): { | ||
messages: OpenAI.Chat.ChatCompletionMessageParam[]; | ||
} { | ||
const messages = formattedPrompt.toChatMessages(); | ||
const { _convertMessagesToOpenAIParams } = langChainOpenAIImports; | ||
if (typeof _convertMessagesToOpenAIParams !== "function") { | ||
throw new Error( | ||
`Please update your version of "@langchain/openai" to 0.3.1 or higher.` | ||
); | ||
} | ||
return { | ||
messages: _convertMessagesToOpenAIParams( | ||
messages | ||
) as OpenAI.Chat.ChatCompletionMessageParam[], | ||
}; | ||
} |
This file contains 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,25 @@ | ||
import Anthropic from "@anthropic-ai/sdk"; | ||
import { pull } from "langchain/hub"; | ||
|
||
import { convertPromptToAnthropic } from "../anthropic.js"; | ||
|
||
test("basic traceable implementation", async () => { | ||
const prompt = await pull("jacob/joke-generator"); | ||
const formattedPrompt = await prompt.invoke({ | ||
topic: "cats", | ||
}); | ||
|
||
const { system, messages } = convertPromptToAnthropic(formattedPrompt); | ||
|
||
const anthropicClient = new Anthropic(); | ||
|
||
const anthropicResponse = await anthropicClient.messages.create({ | ||
model: "claude-3-haiku-20240307", | ||
system, | ||
messages: messages, | ||
max_tokens: 1024, | ||
stream: false, | ||
}); | ||
|
||
expect(anthropicResponse.content).toBeDefined(); | ||
}); |
This file contains 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,22 @@ | ||
import OpenAI from "openai"; | ||
import { pull } from "langchain/hub"; | ||
|
||
import { convertPromptToOpenAI } from "../openai.js"; | ||
|
||
test("basic traceable implementation", async () => { | ||
const prompt = await pull("jacob/joke-generator"); | ||
const formattedPrompt = await prompt.invoke({ | ||
topic: "cats", | ||
}); | ||
|
||
const { messages } = convertPromptToOpenAI(formattedPrompt); | ||
|
||
const openAIClient = new OpenAI(); | ||
|
||
const openAIResponse = await openAIClient.chat.completions.create({ | ||
model: "gpt-4o-mini", | ||
messages, | ||
}); | ||
|
||
expect(openAIResponse.choices.length).toBeGreaterThan(0); | ||
}); |
This file contains 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 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
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.
maybe silly question, but what's the need to do this in the SDK if we are pulling from langchain/hub anyway?
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.
Optics and consistency with Python?