agsamantha/node_modules/@langchain/openai/dist/utils/prompts.js
2024-10-02 15:15:21 -05:00

38 lines
1.1 KiB
JavaScript

import { _convertMessagesToOpenAIParams } from "../chat_models.js";
/**
* 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) {
const messages = formattedPrompt.toChatMessages();
return {
messages: _convertMessagesToOpenAIParams(messages),
};
}