UNPKG

create-nodex

Version:

CLI tool to create modern Node.js projects with TypeScript, AI capabilities, and more

83 lines (74 loc) 2.68 kB
import { ChatOpenAI } from "@langchain/openai"; // Default import, user will change if needed import { ChatPromptTemplate } from "@langchain/core/prompts"; import { HumanMessage, SystemMessage, BaseMessage, } from "@langchain/core/messages"; // This is a basic setup file for Langchain. // You will need to configure the model and API keys based on your chosen provider. // Example: Using OpenAI (remember to set your OPENAI_API_KEY environment variable) // const model = new ChatOpenAI({ // model: "gpt-4", // Or your preferred model // temperature: 0.7, // }); /** * A simple function to demonstrate a chat completion. * @param modelInstance An instance of a Langchain chat model. * @param language The language to translate into. * @param text The text to translate. * @returns The translated text. */ export async function translateText( modelInstance: any, // Replace 'any' with the specific model type, e.g., ChatOpenAI language: string, text: string ): Promise<string | null> { const systemTemplate = "Translate the following from English into {language}."; const promptTemplate = ChatPromptTemplate.fromMessages([ ["system", systemTemplate], ["user", "{text}"], ]); try { const chain = promptTemplate.pipe(modelInstance); const result = (await chain.invoke({ language, text })) as BaseMessage; return result?.content?.toString() || null; } catch (error) { console.error("Error during translation:", error); return null; } } // You can create more functions here to interact with your chosen LLM. // For example, a function to have a more general conversation: /** * A simple function to send a message to the LLM and get a response. * @param modelInstance An instance of a Langchain chat model. * @param userMessage The message from the user. * @param systemContext Optional system context to guide the LLM. * @returns The LLM's response. */ export async function askLLM( modelInstance: any, // Replace 'any' with the specific model type userMessage: string, systemContext?: string ): Promise<string | null> { const messages = []; if (systemContext) { messages.push(new SystemMessage(systemContext)); } messages.push(new HumanMessage(userMessage)); try { const response = (await modelInstance.invoke(messages)) as BaseMessage; return response?.content?.toString() || null; } catch (error) { console.error("Error interacting with LLM:", error); return null; } } console.log( "Langchain AI setup file loaded. You can import these functions into your project." ); console.log( "Remember to initialize your chosen model and pass it to the functions." );