feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
+91
-13
@@ -1,23 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Entry point for running the LangGraph example.
|
||||
Graph Reflection and Refinement Demo with LangChain LLM Integration.
|
||||
|
||||
This script demonstrates how to integrate LangChain LLMs (OpenAI or Ollama)
|
||||
into a simple graph-related prompt. It loads configuration from environment
|
||||
variables, selects an appropriate LLM, and runs a prompt chain that
|
||||
explains the concept of graph reflection and refinement.
|
||||
|
||||
Requirements:
|
||||
- langchain
|
||||
- langchain-openai
|
||||
- langchain-ollama
|
||||
- python-dotenv
|
||||
- openai
|
||||
"""
|
||||
|
||||
from src.graph import build_graph
|
||||
from src.utils import format_state
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
def main():
|
||||
# Build the graph
|
||||
graph = build_graph()
|
||||
# Load environment variables from a .env file if present
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Create a simple state with a question
|
||||
state = {"question": "What is the capital of France?"}
|
||||
load_dotenv()
|
||||
except ImportError:
|
||||
# dotenv is optional; if not installed, environment variables must be set manually
|
||||
pass
|
||||
|
||||
# Run the graph
|
||||
result = graph.invoke(state)
|
||||
# Import LangChain components
|
||||
try:
|
||||
from langchain import PromptTemplate, LLMChain
|
||||
from langchain_openai import OpenAI
|
||||
from langchain_ollama import Ollama
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Required LangChain packages are missing. "
|
||||
"Please install them via 'pip install -r requirements.txt'."
|
||||
) from exc
|
||||
|
||||
|
||||
def get_llm() -> "BaseLLM":
|
||||
"""
|
||||
Instantiate an LLM based on available environment variables.
|
||||
|
||||
Returns:
|
||||
An instance of a LangChain LLM (OpenAI or Ollama).
|
||||
|
||||
Raises:
|
||||
RuntimeError: If neither OpenAI nor Ollama configuration is found.
|
||||
"""
|
||||
# Prefer OpenAI if API key is available
|
||||
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,
|
||||
)
|
||||
|
||||
# Fallback to Ollama if host is configured
|
||||
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. Set either OPENAI_API_KEY or OLLAMA_HOST "
|
||||
"in your environment."
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""
|
||||
Main entry point: builds a prompt chain and prints the LLM response.
|
||||
"""
|
||||
llm = get_llm()
|
||||
|
||||
# Simple prompt template explaining graph reflection and refinement
|
||||
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)
|
||||
|
||||
# Run the chain and print the result
|
||||
response = chain.run()
|
||||
print("\n=== LLM Response ===\n")
|
||||
print(response)
|
||||
|
||||
# Print the final state
|
||||
print("Final state:")
|
||||
print(format_state(result))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user