42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
import asyncio
|
|
from langchain_core.messages import HumanMessage
|
|
from rag_agent import agent, add_to_knowledge_base, search_knowledge_base
|
|
|
|
async def interactive_loop():
|
|
print("Welcome to the RAG agent. Type /add to add a document, /search to query, /quit to exit.")
|
|
thread_id = "session-1"
|
|
while True:
|
|
user_input = input(">> ").strip()
|
|
if user_input.lower() == "/quit":
|
|
print("Goodbye!")
|
|
break
|
|
elif user_input.lower() == "/add":
|
|
title = input("Title: ").strip()
|
|
print("Enter content (end with a single line containing only 'END'):")
|
|
lines = []
|
|
while True:
|
|
line = input()
|
|
if line.strip() == "END":
|
|
break
|
|
lines.append(line)
|
|
content = "\n".join(lines)
|
|
result = add_to_knowledge_base(content, title)
|
|
print(result)
|
|
elif user_input.lower() == "/search":
|
|
query = input("Query: ").strip()
|
|
result = search_knowledge_base(query)
|
|
print("Search results:")
|
|
print(result)
|
|
else:
|
|
messages = [HumanMessage(content=user_input)]
|
|
response = await agent.ainvoke(
|
|
{"messages": messages},
|
|
{"configurable": {"thread_id": thread_id}},
|
|
)
|
|
print(response["messages"][-1].content)
|
|
|
|
def main():
|
|
asyncio.run(interactive_loop())
|
|
|
|
if __name__ == "__main__":
|
|
main() |