|
| 1 | +import getpass |
| 2 | +import os |
| 3 | +from dotenv import load_dotenv |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from langgraph.prebuilt import create_react_agent |
| 7 | +from langgraph.checkpoint.memory import MemorySaver |
| 8 | +from langchain.chat_models import init_chat_model |
| 9 | + |
| 10 | +from any_chatbot.indexing import index_text_docs |
| 11 | +from any_chatbot.tools import initialize_retrieve_tool |
| 12 | + |
| 13 | +load_dotenv() |
| 14 | + |
| 15 | +BASE = Path(__file__).parent.parent.parent |
| 16 | +DATA = BASE / "data" |
| 17 | +OUTPUTS = BASE / "outputs" |
| 18 | + |
| 19 | +# INDEXING |
| 20 | +embeddings, vector_store = index_text_docs(DATA) |
| 21 | + |
| 22 | +# BUILD LLM |
| 23 | +if not os.environ.get("GOOGLE_API_KEY"): |
| 24 | + os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter API key for Google Gemini: ") |
| 25 | +llm = init_chat_model("gemini-2.0-flash", model_provider="google_genai") |
| 26 | + |
| 27 | +# LOAD TOOLS |
| 28 | +retrieve_tool = initialize_retrieve_tool(vector_store) |
| 29 | + |
| 30 | +# BUILD AGENT |
| 31 | +# build checkpointer |
| 32 | +memory = MemorySaver() |
| 33 | +# build agent |
| 34 | +agent_executor = create_react_agent(llm, [retrieve_tool], checkpointer=memory) |
| 35 | +# save architecture graph image |
| 36 | +png_bytes = agent_executor.get_graph().draw_mermaid_png() |
| 37 | +# save to file |
| 38 | +with open(OUTPUTS / "graph.png", "wb") as f: |
| 39 | + f.write(png_bytes) |
| 40 | +print("Wrote graph.png") |
| 41 | + |
| 42 | +# PROMPT |
| 43 | +# specify an ID for the thread |
| 44 | +import random |
| 45 | +# config = {"configurable": {"thread_id": "abc123"}} |
| 46 | +config = {"configurable": {"thread_id": random.random()}} |
| 47 | + |
| 48 | +input_message = ( |
| 49 | + "First retrieve what the revenue for Nike in 2023 was using the functional call.\n\n" |
| 50 | + "Once you get the answer, do a second retrieve to tell me which distribution centers nike have.\n\n" |
| 51 | + "Once you get the second answer,, tell me how many employees nike has. You can retreive MULTIPLE TIMES\n\n" |
| 52 | + "Base your answers only on the retrieved information thorugh the functional call you have." |
| 53 | +) |
| 54 | + |
| 55 | +for event in agent_executor.stream( |
| 56 | + {"messages": [{"role": "user", "content": input_message}]}, |
| 57 | + stream_mode="values", |
| 58 | + config=config, |
| 59 | +): |
| 60 | + event["messages"][-1].pretty_print() |
| 61 | + |
0 commit comments