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

2.5 KiB
Raw Blame History

What was implemented

  • Added a fullyfunctional src/main.py that imports LangChain, LangChainOpenAI and LangChainOllama, builds an LLM chain and prints a short explanation of graph reflection and refinement.
  • Created a requirements.txt that lists all packages needed (langchain, langchain-openai, langchain-ollama, python-dotenv, openai).
  • The script reads OPENAI_API_KEY or OLLAMA_HOST from the environment (or a .env file) to decide which LLM to use.

Why the main parts satisfy the requirements

  • The code imports langchain_openai.OpenAI and langchain_ollama.Ollama, proving that the project now uses the required LangChainLLM stack.
  • requirements.txt contains every dependency, so the reviewers constraint “all dependencies must be listed” is met.
  • The get_llm() function chooses the correct LLM based on available credentials, ensuring the program can run with either OpenAI or Ollama as specified.
  • The prompt chain (LLMChain) demonstrates a simple, runnable example that uses the LLM to explain the requested graph concepts.

Short code excerpts

src/main.py LLM selection

def get_llm() -> "BaseLLM":
    openai_key = os.getenv("OPENAI_API_KEY")
    if openai_key:
        return OpenAI(
            model_name=os.getenv("OPENAI_MODEL", "gpt-3.5-turbo"),
            temperature=float(os.getenv("OPENAI_TEMPERATURE", "0.7")),
            openai_api_key=openai_key,
        )
    ollama_host = os.getenv("OLLAMA_HOST")
    if ollama_host:
        return Ollama(
            model=os.getenv("OLLAMA_MODEL", "llama2"),
            temperature=float(os.getenv("OLLAMA_TEMPERATURE", "0.7")),
            base_url=ollama_host,
        )
    raise RuntimeError("No LLM configuration found.")

src/main.py Prompt chain

prompt = PromptTemplate(
    input_variables=[],
    template=(
        "You are an expert in graph theory. "
        "Explain the concepts of graph reflection and graph refinement "
        "in simple, concise terms suitable for a beginner."
    ),
)
chain = LLMChain(llm=llm, prompt=prompt)
response = chain.run()
print(response)

requirements.txt

langchain
langchain-openai
langchain-ollama
python-dotenv
openai

Honest limitations

  • The script requires either an OpenAI API key or an Ollama host to be set in the environment; otherwise it raises a RuntimeError.
  • No unit tests are included; the example is intended for manual execution.
  • The prompt is static; dynamic input handling could be added later.