-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for formatting LangChain prompts as OpenAI and Anthropic …
…payloads
- Loading branch information
1 parent
727e412
commit 9228b6c
Showing
10 changed files
with
251 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "langsmith", | ||
"version": "0.1.60", | ||
"version": "0.1.61", | ||
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.", | ||
"packageManager": "[email protected]", | ||
"files": [ | ||
|
@@ -53,6 +53,14 @@ | |
"singletons/traceable.js", | ||
"singletons/traceable.d.ts", | ||
"singletons/traceable.d.cts", | ||
"utils/prompts/anthropic.cjs", | ||
"utils/prompts/anthropic.js", | ||
"utils/prompts/anthropic.d.ts", | ||
"utils/prompts/anthropic.d.cts", | ||
"utils/prompts/openai.cjs", | ||
"utils/prompts/openai.js", | ||
"utils/prompts/openai.d.ts", | ||
"utils/prompts/openai.d.cts", | ||
"index.cjs", | ||
"index.js", | ||
"index.d.ts", | ||
|
@@ -106,12 +114,14 @@ | |
}, | ||
"devDependencies": { | ||
"@ai-sdk/openai": "^0.0.40", | ||
"@anthropic-ai/sdk": "^0.27.3", | ||
"@babel/preset-env": "^7.22.4", | ||
"@faker-js/faker": "^8.4.1", | ||
"@jest/globals": "^29.5.0", | ||
"@langchain/anthropic": "^0.3.2", | ||
"@langchain/core": "^0.3.1", | ||
"@langchain/langgraph": "^0.2.3", | ||
"@langchain/openai": "^0.3.0", | ||
"@langchain/openai": "^0.3.1", | ||
"@tsconfig/recommended": "^1.0.2", | ||
"@types/jest": "^29.5.1", | ||
"@typescript-eslint/eslint-plugin": "^5.59.8", | ||
|
@@ -266,6 +276,24 @@ | |
"import": "./singletons/traceable.js", | ||
"require": "./singletons/traceable.cjs" | ||
}, | ||
"./utils/prompts/anthropic": { | ||
"types": { | ||
"import": "./utils/prompts/anthropic.d.ts", | ||
"require": "./utils/prompts/anthropic.d.cts", | ||
"default": "./utils/prompts/anthropic.d.ts" | ||
}, | ||
"import": "./utils/prompts/anthropic.js", | ||
"require": "./utils/prompts/anthropic.cjs" | ||
}, | ||
"./utils/prompts/openai": { | ||
"types": { | ||
"import": "./utils/prompts/openai.d.ts", | ||
"require": "./utils/prompts/openai.d.cts", | ||
"default": "./utils/prompts/openai.d.ts" | ||
}, | ||
"import": "./utils/prompts/openai.js", | ||
"require": "./utils/prompts/openai.cjs" | ||
}, | ||
"./package.json": "./package.json" | ||
} | ||
} |
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-5-sonnet-20240620", | ||
system, | ||
messages: messages, | ||
max_tokens: 1024, | ||
stream: false, | ||
}); | ||
|
||
console.log(anthropicResponse); | ||
}); |
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, | ||
}); | ||
|
||
console.log(openAIResponse); | ||
}); |
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