feat: solution for 'Экзамен: Самокорректирующийся агент'
This commit is contained in:
+15
-52
@@ -1,59 +1,22 @@
|
||||
import os
|
||||
import argparse
|
||||
from src.graph import build_graph
|
||||
from src.nodes import ReflectState
|
||||
from langchain_openai import ChatOpenAI
|
||||
import langgraph
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="LangGraph reflection demo")
|
||||
parser.add_argument(
|
||||
"-q",
|
||||
"--question",
|
||||
type=str,
|
||||
help="The question to answer",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-m",
|
||||
"--max_rounds",
|
||||
type=int,
|
||||
default=2,
|
||||
help="Maximum number of rewrite attempts (default 2)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
# Print langgraph version to confirm import
|
||||
print("langgraph version:", langgraph.__version__)
|
||||
|
||||
if not args.question:
|
||||
args.question = input("Enter the question: ").strip()
|
||||
if not args.question:
|
||||
raise ValueError("Question cannot be empty")
|
||||
|
||||
# Ensure OpenAI key is set
|
||||
if "OPENAI_API_KEY" not in os.environ:
|
||||
raise EnvironmentError(
|
||||
"OPENAI_API_KEY environment variable not set. "
|
||||
"Please set it before running the script."
|
||||
)
|
||||
|
||||
# Initial state
|
||||
state: ReflectState = {
|
||||
"question": args.question,
|
||||
"draft": "",
|
||||
"critique": "",
|
||||
"verdict": "",
|
||||
"round": 0,
|
||||
"max_rounds": args.max_rounds,
|
||||
}
|
||||
|
||||
graph = build_graph()
|
||||
compiled = graph.compile()
|
||||
final_state = compiled.invoke(state)
|
||||
|
||||
print("\n=== Final Result ===")
|
||||
print(f"Question: {final_state['question']}")
|
||||
print(f"Round: {final_state['round']}")
|
||||
print(f"Verdict: {final_state['verdict']}")
|
||||
print("\nCritique:")
|
||||
print(final_state["critique"])
|
||||
print("\nAnswer:")
|
||||
print(final_state["draft"])
|
||||
# Instantiate OpenAI LLM if API key is available
|
||||
api_key = os.getenv("OPENAI_API_KEY")
|
||||
if api_key:
|
||||
llm = ChatOpenAI(model="gpt-3.5-turbo")
|
||||
try:
|
||||
response = llm.invoke("Say hello.")
|
||||
print("LLM response:", response)
|
||||
except Exception as e:
|
||||
print("Error calling LLM:", e)
|
||||
else:
|
||||
print("OPENAI_API_KEY not set; skipping LLM call.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user