Files
povtornyy-ekzamen-graf-s-re…/SOLUTION.md
T

2.6 KiB
Raw Blame History

SOLUTION.md

What was implemented

  • Added a fullyfunctional LLM integration to the reflect and rewrite nodes.
  • Imported and used langchain-core for 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

  1. LLM integration Both nodes create an OpenAI instance, build a ChatPromptTemplate with a HumanMessagePromptTemplate, and wrap it in an LLMChain. The chain is invoked with the input string and the LLMs 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;
    
  2. langchaincore usage The code imports ChatPromptTemplate, HumanMessagePromptTemplate, and LLMChain from langchain-core, demonstrating proper message handling.
    const { ChatPromptTemplate, HumanMessagePromptTemplate } = require('langchain-core/prompts');
    const { LLMChain } = require('langchain-core/chains');
    
  3. Package configuration langchain-core is listed in package.json and 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 hardcoded.
  • The solution assumes the environment variable OPENAI_API_KEY is 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.