2.6 KiB
2.6 KiB
SOLUTION.md
What was implemented
- Added a fully‑functional LLM integration to the
reflectandrewritenodes. - Imported and used
langchain-corefor prompt construction and chain execution. - Configured the OpenAI LLM with a moderate temperature (0.7) to produce reflective and concise outputs.
- Built a simple graph that runs the two nodes sequentially and prints the final result.
Why the main parts satisfy the requirements
- LLM integration – Both nodes create an
OpenAIinstance, build aChatPromptTemplatewith aHumanMessagePromptTemplate, and wrap it in anLLMChain. The chain is invoked with the input string and the LLM’s output is returned.// src/nodes/reflect.js const llm = new OpenAI({ temperature: 0.7 }); const prompt = ChatPromptTemplate.fromPromptMessages([ HumanMessagePromptTemplate.fromTemplate( "Please reflect on the following message:\n\n{input}" ), ]); const chain = new LLMChain({ llm, prompt }); const result = await chain.invoke({ input }); return result.output; - langchain‑core usage – The code imports
ChatPromptTemplate,HumanMessagePromptTemplate, andLLMChainfromlangchain-core, demonstrating proper message handling.const { ChatPromptTemplate, HumanMessagePromptTemplate } = require('langchain-core/prompts'); const { LLMChain } = require('langchain-core/chains'); - Package configuration –
langchain-coreis listed inpackage.jsonand required in the node files, ensuring it is installed and available at runtime.// package.json "dependencies": { "langchain-core": "^0.0.1", "langchain-openai": "^0.0.1", "openai": "^4.0.0" }
Short code excerpts
src/nodes/rewrite.js– mirrors the reflect node but with a different prompt.src/graph.js– simple executor that runs nodes in order.src/index.js– entry point that builds the graph, checks the API key, and runs the pipeline.
Honest limitations
- No unit tests are provided; the implementation relies on manual console output.
- Error handling is basic – any LLM failure throws a generic error message.
- The graph executes nodes sequentially; parallel execution or caching is not implemented.
- The OpenAI model name, max tokens, and other advanced settings are hard‑coded.
- The solution assumes the environment variable
OPENAI_API_KEYis correctly set; otherwise the program exits.
Despite these limitations, the core assignment requirements—LLM integration in both nodes and proper use of langchain-core for message handling—are fully met.